1 /* 2 ******************************************************************************* 3 * 4 * Copyright (C) 2002-2011, International Business Machines 5 * Corporation and others. All Rights Reserved. 6 * 7 ******************************************************************************* 8 */ 9 10 #include "unicode/uchriter.h" 11 #include "unicode/schriter.h" 12 #include "unicode/ustring.h" 13 #include <stdio.h> 14 #include <unicode/brkiter.h> 15 #include <unicode/ustdio.h> 16 #include <stdlib.h> 17 18 static UFILE *out; 19 20 void printUnicodeString(const UnicodeString &s) 21 { 22 u_fprintf(out, "%S", &s); 23 } 24 25 void printUChar(UChar32 ch) 26 { 27 if(ch < 127) { 28 u_fprintf(out, "%C", (UChar) ch); 29 } else if (ch == CharacterIterator::DONE) { 30 u_fprintf(out, "[CharacterIterator::DONE = 0xFFFF]"); 31 } else { 32 u_fprintf(out, "[%X]", ch); 33 } 34 } 35 36 class Test 37 { 38 public: 39 void TestUChariter(); 40 void TestStringiter(); 41 }; 42 43 void Test::TestUChariter() { 44 const char testChars[] = "Now is the time for all good men to come " 45 "to the aid of their country."; 46 47 UnicodeString testString(testChars,""); 48 const UChar *testText = testString.getTerminatedBuffer(); 49 50 UCharCharacterIterator iter(testText, u_strlen(testText)); 51 UCharCharacterIterator* test2 = (UCharCharacterIterator*)iter.clone(); 52 53 u_fprintf(out, "testText = %s", testChars); 54 55 if (iter != *test2 ) { 56 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n"); 57 } 58 59 UnicodeString result1, result2; 60 // getting and comparing the text within the iterators 61 iter.getText(result1); 62 test2->getText(result2); 63 if (result1 != result2) { 64 u_fprintf(out, "iter.getText() != clone.getText()\n"); 65 } 66 67 u_fprintf(out, "\n"); 68 69 // Demonstrates seeking forward using the iterator. 70 u_fprintf(out, "Forward = "); 71 72 UChar c = iter.first(); 73 printUChar(c); // The first char 74 int32_t i = 0; 75 76 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) { 77 u_fprintf(out, "startIndex() or endIndex() failed\n"); 78 } 79 80 81 // Testing forward iteration... 82 do { 83 if (c == CharacterIterator::DONE && i != u_strlen(testText)) { 84 u_fprintf(out, "Iterator reached end prematurely"); 85 } 86 else if (c != testText[i]) { 87 u_fprintf(out, "Character mismatch at position %d\n" + i); 88 } 89 if (iter.current() != c) { 90 u_fprintf(out, "current() isn't working right"); 91 } 92 if (iter.getIndex() != i) { 93 u_fprintf(out, "getIndex() isn't working right\n"); 94 } 95 if (c != CharacterIterator::DONE) { 96 c = iter.next(); 97 i++; 98 } 99 100 u_fprintf(out, "|"); 101 printUChar(c); 102 103 } while (c != CharacterIterator::DONE); 104 105 delete test2; 106 u_fprintf(out, "\n"); 107 } 108 109 110 void Test::TestStringiter() { 111 const char testChars[] = "Now is the time for all good men to come " 112 "to the aid of their country."; 113 114 UnicodeString testString(testChars,""); 115 const UChar *testText = testString.getTerminatedBuffer(); 116 117 StringCharacterIterator iter(testText, u_strlen(testText)); 118 StringCharacterIterator* test2 = (StringCharacterIterator*)iter.clone(); 119 120 if (iter != *test2 ) { 121 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n"); 122 } 123 124 UnicodeString result1, result2; 125 // getting and comparing the text within the iterators 126 iter.getText(result1); 127 test2->getText(result2); 128 if (result1 != result2) { 129 u_fprintf(out, "getText() failed\n"); 130 } 131 132 u_fprintf(out, "Backwards: "); 133 134 UChar c = iter.last(); 135 int32_t i = iter.endIndex(); 136 137 printUChar(c); 138 i--; // already printed out the last char 139 140 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) { 141 u_fprintf(out, "startIndex() or endIndex() failed\n"); 142 } 143 144 // Testing backward iteration over a range... 145 do { 146 if (c == CharacterIterator::DONE) { 147 u_fprintf(out, "Iterator reached end prematurely\n"); 148 } 149 else if (c != testText[i]) { 150 u_fprintf(out, "Character mismatch at position %d\n", i); 151 } 152 if (iter.current() != c) { 153 u_fprintf(out, "current() isn't working right\n"); 154 } 155 if (iter.getIndex() != i) { 156 u_fprintf(out, "getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i); 157 } 158 if (c != CharacterIterator::DONE) { 159 c = iter.previous(); 160 i--; 161 } 162 163 u_fprintf(out, "|"); 164 printUChar(c); 165 } while (c != CharacterIterator::DONE); 166 167 u_fprintf(out, "\n"); 168 delete test2; 169 } 170 171 /* Creating and using text boundaries */ 172 int main( void ) 173 { 174 UErrorCode status = U_ZERO_ERROR; 175 176 out = u_finit(stdout, NULL, NULL); 177 178 u_fprintf(out, "ICU Iteration Sample Program (C++)\n\n"); 179 180 Test t; 181 182 u_fprintf(out, "\n"); 183 u_fprintf(out, "Test::TestUCharIter()\n"); 184 185 t.TestUChariter(); 186 187 u_fprintf(out, "-----\n"); 188 u_fprintf(out, "Test::TestStringchariter()\n"); 189 190 t.TestStringiter(); 191 192 u_fprintf(out, "-----\n"); 193 194 return 0; 195 } 196