home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / compitr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  4.3 KB  |  130 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   IBM Open Class Library                                                    *
  6. *   (C) Copyright Taligent, Inc.,  1996                                       *
  7. *   (C) Copyright International Business Machines Corporation,  1996-1998     *
  8. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  9. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  10. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  11. *                                                                             *
  12. *******************************************************************************
  13. */
  14.  
  15. #include "dcmpdata.h"
  16.  
  17. #include "compitr.h"
  18.  
  19. #include "normlzr.h"
  20.  
  21. /**
  22.  * Constant that indicates the iteration has completed.
  23.  * {@link #next} returns this value when there are no more composed characters
  24.  * over which to iterate.
  25.  */
  26. const UChar ComposedCharIter::DONE = Normalizer::DONE;
  27.  
  28. /**
  29.  * Construct a new <tt>ComposedCharIter</tt>.  The iterator will return
  30.  * all Unicode characters with canonical decompositions, including Korean
  31.  * Hangul characters.
  32.  */
  33. ComposedCharIter::ComposedCharIter()
  34.   : minDecomp(DecompData::MAX_COMPAT), 
  35.     hangul(FALSE),
  36.     curChar(0),
  37.     nextChar(ComposedCharIter::DONE)
  38. {
  39. }
  40.  
  41.  
  42.   /**
  43.    * Constructs a non-default <tt>ComposedCharIter</tt> with optional behavior.
  44.    * <p>
  45.    * @param compat    <tt>false</tt> for canonical decompositions only;
  46.    *                  <tt>true</tt> for both canonical and compatibility
  47.    *                  decompositions.
  48.    *
  49.    * @param options   Optional decomposition features.  Currently, the only
  50.    *                  supported option is {@link Normalizer#IGNORE_HANGUL}, which
  51.    *                  causes this <tt>ComposedCharIter</tt> not to iterate
  52.    *                  over the Hangul characters and their corresponding
  53.    *                  Jamo decompositions.
  54.    */
  55. ComposedCharIter::ComposedCharIter(bool_t compat, 
  56.                    int32_t options)
  57.   : minDecomp(compat ? 0 : DecompData::MAX_COMPAT),
  58.     hangul((options & Normalizer::IGNORE_HANGUL) == 0),
  59.     curChar(0),
  60.     nextChar(ComposedCharIter::DONE)
  61. {
  62. }
  63.  
  64. /**
  65.  * Determines whether there any precomposed Unicode characters not yet returned
  66.  * by {@link #next}.
  67.  */
  68. bool_t ComposedCharIter::hasNext() const {
  69.     if (nextChar == DONE)  {
  70.         ((ComposedCharIter*)this)->findNextChar();
  71.     }
  72.     return nextChar != DONE;
  73. }
  74.  
  75. /**
  76.  * Returns the next precomposed Unicode character.
  77.  * Repeated calls to <tt>next</tt> return all of the precomposed characters defined
  78.  * by Unicode, in ascending order.  After all precomposed characters have
  79.  * been returned, {@link #hasNext} will return <tt>false</tt> and further calls
  80.  * to <tt>next</tt> will return {@link #DONE}.
  81.  */
  82. UChar ComposedCharIter::next()
  83. {
  84.     if (nextChar == DONE)  {
  85.         findNextChar();
  86.     }
  87.     curChar = nextChar;
  88.     nextChar = DONE;
  89.     return curChar;
  90. }
  91.  
  92. /**
  93.  * Returns the Unicode decomposition of the current character.
  94.  * This method returns the decomposition of the precomposed character most
  95.  * recently returned by {@link #next}.  The resulting decomposition is
  96.  * affected by the settings of the
  97.  * {@link Normalizer#COMPATIBILITY COMPATIBILITY}
  98.  * and {@link Normalizer#NO_HANGUL NO_HANGUL} options passed to the constructor.
  99.  */
  100. void ComposedCharIter::getDecomposition(UnicodeString& result) const
  101. {
  102.     result.truncate(0);
  103.  
  104.     UChar pos = ucmp16_getu(DecompData::offsets, curChar);
  105.     if (pos > minDecomp) {
  106.         Normalizer::doAppend(DecompData::contents, pos, result);
  107.     } 
  108.     else if (hangul && curChar >= Normalizer::HANGUL_BASE && curChar < Normalizer::HANGUL_LIMIT) {
  109.         Normalizer::hangulToJamo(curChar, result, minDecomp);
  110.     } 
  111.     else {
  112.         result += curChar;
  113.     }
  114. }
  115.  
  116. void ComposedCharIter::findNextChar()
  117. {
  118.     if (curChar != DONE) {
  119.         UChar ch = curChar;
  120.         while (++ch < 0xFFFF) {
  121.             UChar offset = ucmp16_getu(DecompData::offsets, ch);
  122.             if (offset > minDecomp
  123.                 || (hangul && ch >= Normalizer::HANGUL_BASE && ch < Normalizer::HANGUL_LIMIT) ) {
  124.                 nextChar = ch;
  125.                 break;
  126.             }
  127.         }
  128.     }
  129. }
  130.