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