• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *     © 2016 and later: Unicode, Inc. and others.
5 *     License & terms of use: http://www.unicode.org/copyright.html
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 using icu::CharacterIterator;
27 using icu::StringCharacterIterator;
28 using icu::UCharCharacterIterator;
29 using icu::UnicodeString;
30 
printUnicodeString(const UnicodeString & s)31 void printUnicodeString(const UnicodeString &s)
32 {
33     u_fprintf(out, "%S", &s);
34 }
35 
printUChar(UChar32 ch)36 void printUChar(UChar32 ch)
37 {
38     if(ch < 127) {
39         u_fprintf(out, "%C", (UChar) ch);
40     } else if (ch == CharacterIterator::DONE) {
41         u_fprintf(out, "[CharacterIterator::DONE = 0xFFFF]");
42     } else {
43         u_fprintf(out, "[%X]", ch);
44     }
45 }
46 
47 class Test
48 {
49 public:
50     void TestUChariter();
51     void TestStringiter();
52 };
53 
TestUChariter()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.";
57 
58     UnicodeString testString(testChars,"");
59     const UChar *testText = testString.getTerminatedBuffer();
60 
61     UCharCharacterIterator iter(testText, u_strlen(testText));
62     UCharCharacterIterator* test2 = iter.clone();
63 
64     u_fprintf(out, "testText = %s", testChars);
65 
66     if (iter != *test2 ) {
67         u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
68     }
69 
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");
76     }
77 
78     u_fprintf(out, "\n");
79 
80     // Demonstrates seeking forward using the iterator.
81     u_fprintf(out, "Forward  = ");
82 
83     UChar c = iter.first();
84     printUChar(c);    // The first char
85     int32_t i = 0;
86 
87     if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
88         u_fprintf(out, "startIndex() or endIndex() failed\n");
89     }
90 
91 
92     // Testing forward iteration...
93     do {
94         if (c == CharacterIterator::DONE && i != u_strlen(testText)) {
95             u_fprintf(out, "Iterator reached end prematurely");
96         }
97         else if (c != testText[i]) {
98           u_fprintf(out, "Character mismatch at position %d\n", i);
99         }
100         if (iter.current() != c) {
101             u_fprintf(out, "current() isn't working right");
102         }
103         if (iter.getIndex() != i) {
104             u_fprintf(out, "getIndex() isn't working right\n");
105         }
106         if (c != CharacterIterator::DONE) {
107             c = iter.next();
108             i++;
109         }
110 
111         u_fprintf(out, "|");
112         printUChar(c);
113 
114     } while (c != CharacterIterator::DONE);
115 
116     delete test2;
117     u_fprintf(out, "\n");
118 }
119 
120 
TestStringiter()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.";
124 
125     UnicodeString testString(testChars,"");
126     const UChar *testText    = testString.getTerminatedBuffer();
127 
128     StringCharacterIterator iter(testText, u_strlen(testText));
129     StringCharacterIterator* test2 = iter.clone();
130 
131     if (iter != *test2 ) {
132         u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
133     }
134 
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");
141     }
142 
143     u_fprintf(out, "Backwards: ");
144 
145     UChar c = iter.last();
146     int32_t i = iter.endIndex();
147 
148     printUChar(c);
149     i--; // already printed out the last char
150 
151     if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
152         u_fprintf(out, "startIndex() or endIndex() failed\n");
153     }
154 
155     // Testing backward iteration over a range...
156     do {
157         if (c == CharacterIterator::DONE) {
158             u_fprintf(out, "Iterator reached end prematurely\n");
159         }
160         else if (c != testText[i]) {
161             u_fprintf(out, "Character mismatch at position %d\n", i);
162         }
163         if (iter.current() != c) {
164             u_fprintf(out, "current() isn't working right\n");
165         }
166         if (iter.getIndex() != i) {
167             u_fprintf(out, "getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i);
168         }
169         if (c != CharacterIterator::DONE) {
170             c = iter.previous();
171             i--;
172         }
173 
174         u_fprintf(out, "|");
175         printUChar(c);
176     } while (c != CharacterIterator::DONE);
177 
178     u_fprintf(out, "\n");
179     delete test2;
180 }
181 
182 /* Creating and using text boundaries */
main(void)183 int main( void )
184 {
185     UErrorCode status = U_ZERO_ERROR;
186 
187     out = u_finit(stdout, NULL, NULL);
188 
189     u_fprintf(out, "ICU Iteration Sample Program (C++)\n\n");
190 
191     Test t;
192 
193     u_fprintf(out, "\n");
194     u_fprintf(out, "Test::TestUCharIter()\n");
195 
196     t.TestUChariter();
197 
198     u_fprintf(out, "-----\n");
199     u_fprintf(out, "Test::TestStringchariter()\n");
200 
201     t.TestStringiter();
202 
203     u_fprintf(out, "-----\n");
204 
205     return 0;
206 }
207