]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/citer/citer.cpp
   2 ******************************************************************************* 
   4 *     © 2016 and later: Unicode, Inc. and others. 
   5 *     License & terms of use: http://www.unicode.org/copyright.html#License 
   7 ******************************************************************************* 
   8 ******************************************************************************* 
  10 *     Copyright (C) 2002-2011, International Business Machines 
  11 *     Corporation and others.    All Rights Reserved. 
  13 ******************************************************************************* 
  16 #include "unicode/uchriter.h" 
  17 #include "unicode/schriter.h" 
  18 #include "unicode/ustring.h" 
  20 #include <unicode/brkiter.h> 
  21 #include <unicode/ustdio.h> 
  26 using icu::CharacterIterator
; 
  27 using icu::StringCharacterIterator
; 
  28 using icu::UCharCharacterIterator
; 
  29 using icu::UnicodeString
; 
  31 void printUnicodeString(const UnicodeString 
&s
) 
  33     u_fprintf(out
, "%S", &s
); 
  36 void printUChar(UChar32 ch
) 
  39         u_fprintf(out
, "%C", (UChar
) ch
); 
  40     } else if (ch 
== CharacterIterator::DONE
) { 
  41         u_fprintf(out
, "[CharacterIterator::DONE = 0xFFFF]"); 
  43         u_fprintf(out
, "[%X]", ch
); 
  51     void TestStringiter(); 
  54 void Test::TestUChariter() { 
  55     const char testChars
[] = "Now is the time for all good men to come " 
  56         "to the aid of their country."; 
  58     UnicodeString 
testString(testChars
,""); 
  59     const UChar 
*testText 
= testString
.getTerminatedBuffer(); 
  61     UCharCharacterIterator 
iter(testText
, u_strlen(testText
)); 
  62     UCharCharacterIterator
* test2 
= (UCharCharacterIterator
*)iter
.clone(); 
  64     u_fprintf(out
, "testText = %s", testChars
); 
  66     if (iter 
!= *test2 
) { 
  67         u_fprintf(out
, "clone() or equals() failed: Two clones tested unequal\n"); 
  70     UnicodeString result1
, result2
; 
  71     // getting and comparing the text within the iterators 
  72     iter
.getText(result1
); 
  73     test2
->getText(result2
); 
  74     if (result1 
!= result2
) { 
  75         u_fprintf(out
, "iter.getText() != clone.getText()\n"); 
  80     // Demonstrates seeking forward using the iterator. 
  81     u_fprintf(out
, "Forward  = "); 
  83     UChar c 
= iter
.first(); 
  84     printUChar(c
);    // The first char 
  87     if (iter
.startIndex() != 0 || iter
.endIndex() != u_strlen(testText
)) { 
  88         u_fprintf(out
, "startIndex() or endIndex() failed\n"); 
  92     // Testing forward iteration... 
  94         if (c 
== CharacterIterator::DONE 
&& i 
!= u_strlen(testText
)) { 
  95             u_fprintf(out
, "Iterator reached end prematurely"); 
  97         else if (c 
!= testText
[i
]) { 
  98             u_fprintf(out
, "Character mismatch at position %d\n" + i
); 
 100         if (iter
.current() != c
) { 
 101             u_fprintf(out
, "current() isn't working right"); 
 103         if (iter
.getIndex() != i
) { 
 104             u_fprintf(out
, "getIndex() isn't working right\n"); 
 106         if (c 
!= CharacterIterator::DONE
) { 
 114     } while (c 
!= CharacterIterator::DONE
);         
 117     u_fprintf(out
, "\n"); 
 121 void Test::TestStringiter() { 
 122     const char testChars
[] = "Now is the time for all good men to come " 
 123         "to the aid of their country."; 
 125     UnicodeString 
testString(testChars
,""); 
 126     const UChar 
*testText    
= testString
.getTerminatedBuffer(); 
 128     StringCharacterIterator 
iter(testText
, u_strlen(testText
)); 
 129     StringCharacterIterator
* test2 
= (StringCharacterIterator
*)iter
.clone(); 
 131     if (iter 
!= *test2 
) { 
 132         u_fprintf(out
, "clone() or equals() failed: Two clones tested unequal\n"); 
 135     UnicodeString result1
, result2
; 
 136     // getting and comparing the text within the iterators 
 137     iter
.getText(result1
); 
 138     test2
->getText(result2
); 
 139     if (result1 
!= result2
) { 
 140         u_fprintf(out
, "getText() failed\n"); 
 143     u_fprintf(out
, "Backwards: "); 
 145     UChar c 
= iter
.last(); 
 146     int32_t i 
= iter
.endIndex(); 
 149     i
--; // already printed out the last char  
 151     if (iter
.startIndex() != 0 || iter
.endIndex() != u_strlen(testText
)) { 
 152         u_fprintf(out
, "startIndex() or endIndex() failed\n"); 
 155     // Testing backward iteration over a range... 
 157         if (c 
== CharacterIterator::DONE
) { 
 158             u_fprintf(out
, "Iterator reached end prematurely\n"); 
 160         else if (c 
!= testText
[i
]) { 
 161             u_fprintf(out
, "Character mismatch at position %d\n", i
); 
 163         if (iter
.current() != c
) { 
 164             u_fprintf(out
, "current() isn't working right\n"); 
 166         if (iter
.getIndex() != i
) { 
 167             u_fprintf(out
, "getIndex() isn't working right [%d should be %d]\n", iter
.getIndex(), i
); 
 169         if (c 
!= CharacterIterator::DONE
) { 
 176     } while (c 
!= CharacterIterator::DONE
); 
 178     u_fprintf(out
, "\n"); 
 182 /* Creating and using text boundaries */ 
 185     UErrorCode status 
= U_ZERO_ERROR
; 
 187     out 
= u_finit(stdout
, NULL
, NULL
); 
 189     u_fprintf(out
, "ICU Iteration Sample Program (C++)\n\n"); 
 193     u_fprintf(out
, "\n"); 
 194     u_fprintf(out
, "Test::TestUCharIter()\n"); 
 198     u_fprintf(out
, "-----\n"); 
 199     u_fprintf(out
, "Test::TestStringchariter()\n"); 
 203     u_fprintf(out
, "-----\n");