Home | History | Annotate | Download | only in i18n
      1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html
      3 //
      4 //   Copyright (C) 2012 International Business Machines Corporation
      5 //   and others. All rights reserved.
      6 //
      7 //   file:  regeximp.cpp
      8 //
      9 //           ICU Regular Expressions,
     10 //             miscellaneous implementation functions.
     11 //
     12 
     13 #include "unicode/utypes.h"
     14 
     15 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
     16 #include "regeximp.h"
     17 #include "unicode/utf16.h"
     18 
     19 U_NAMESPACE_BEGIN
     20 
     21 CaseFoldingUTextIterator::CaseFoldingUTextIterator(UText &text) :
     22    fUText(text), fcsp(NULL), fFoldChars(NULL), fFoldLength(0) {
     23    fcsp = ucase_getSingleton();
     24 }
     25 
     26 CaseFoldingUTextIterator::~CaseFoldingUTextIterator() {}
     27 
     28 UChar32 CaseFoldingUTextIterator::next() {
     29     UChar32  foldedC;
     30     UChar32  originalC;
     31     if (fFoldChars == NULL) {
     32         // We are not in a string folding of an earlier character.
     33         // Start handling the next char from the input UText.
     34         originalC = UTEXT_NEXT32(&fUText);
     35         if (originalC == U_SENTINEL) {
     36             return originalC;
     37         }
     38         fFoldLength = ucase_toFullFolding(fcsp, originalC, &fFoldChars, U_FOLD_CASE_DEFAULT);
     39         if (fFoldLength >= UCASE_MAX_STRING_LENGTH || fFoldLength < 0) {
     40             // input code point folds to a single code point, possibly itself.
     41             // See comment in ucase.h for explanation of return values from ucase_toFullFoldings.
     42             if (fFoldLength < 0) {
     43                 fFoldLength = ~fFoldLength;
     44             }
     45             foldedC = (UChar32)fFoldLength;
     46             fFoldChars = NULL;
     47             return foldedC;
     48         }
     49         // String foldings fall through here.
     50         fFoldIndex = 0;
     51     }
     52 
     53     U16_NEXT(fFoldChars, fFoldIndex, fFoldLength, foldedC);
     54     if (fFoldIndex >= fFoldLength) {
     55         fFoldChars = NULL;
     56     }
     57     return foldedC;
     58 }
     59 
     60 
     61 UBool CaseFoldingUTextIterator::inExpansion() {
     62     return fFoldChars != NULL;
     63 }
     64 
     65 
     66 
     67 CaseFoldingUCharIterator::CaseFoldingUCharIterator(const UChar *chars, int64_t start, int64_t limit) :
     68    fChars(chars), fIndex(start), fLimit(limit), fcsp(NULL), fFoldChars(NULL), fFoldLength(0) {
     69    fcsp = ucase_getSingleton();
     70 }
     71 
     72 
     73 CaseFoldingUCharIterator::~CaseFoldingUCharIterator() {}
     74 
     75 
     76 UChar32 CaseFoldingUCharIterator::next() {
     77     UChar32  foldedC;
     78     UChar32  originalC;
     79     if (fFoldChars == NULL) {
     80         // We are not in a string folding of an earlier character.
     81         // Start handling the next char from the input UText.
     82         if (fIndex >= fLimit) {
     83             return U_SENTINEL;
     84         }
     85         U16_NEXT(fChars, fIndex, fLimit, originalC);
     86 
     87         fFoldLength = ucase_toFullFolding(fcsp, originalC, &fFoldChars, U_FOLD_CASE_DEFAULT);
     88         if (fFoldLength >= UCASE_MAX_STRING_LENGTH || fFoldLength < 0) {
     89             // input code point folds to a single code point, possibly itself.
     90             // See comment in ucase.h for explanation of return values from ucase_toFullFoldings.
     91             if (fFoldLength < 0) {
     92                 fFoldLength = ~fFoldLength;
     93             }
     94             foldedC = (UChar32)fFoldLength;
     95             fFoldChars = NULL;
     96             return foldedC;
     97         }
     98         // String foldings fall through here.
     99         fFoldIndex = 0;
    100     }
    101 
    102     U16_NEXT(fFoldChars, fFoldIndex, fFoldLength, foldedC);
    103     if (fFoldIndex >= fFoldLength) {
    104         fFoldChars = NULL;
    105     }
    106     return foldedC;
    107 }
    108 
    109 
    110 UBool CaseFoldingUCharIterator::inExpansion() {
    111     return fFoldChars != NULL;
    112 }
    113 
    114 int64_t CaseFoldingUCharIterator::getIndex() {
    115     return fIndex;
    116 }
    117 
    118 
    119 U_NAMESPACE_END
    120 
    121 #endif
    122 
    123