]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 2002-2003, 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 <iostream.h> | |
15 | #include <unicode/brkiter.h> | |
16 | #include <stdlib.h> | |
17 | ||
18 | void printUnicodeString(const UnicodeString &s) { | |
19 | char charBuf[1000]; | |
20 | s.extract(0, s.length(), charBuf, sizeof(charBuf)-1, 0); | |
21 | charBuf[sizeof(charBuf)-1] = 0; | |
22 | cout << charBuf; | |
23 | } | |
24 | ||
25 | void printUChar(UChar32 ch) { | |
26 | char charBuf[1000]; | |
27 | charBuf[sizeof(charBuf)-1] = 0; | |
28 | if(ch < 127) { | |
29 | cout << (char) ch; | |
30 | } else if (ch == CharacterIterator::DONE) { | |
31 | cout << "[CharacterIterator::DONE = 0xFFFF]"; | |
32 | } else { | |
33 | cout << "[" << ch << "]"; | |
34 | } | |
35 | } | |
36 | ||
37 | class Test | |
38 | { | |
39 | public: | |
40 | void TestUChariter(); | |
41 | void TestStringiter(); | |
42 | }; | |
43 | ||
44 | void Test::TestUChariter() { | |
45 | const char testChars[] = "Now is the time for all good men to come " | |
46 | "to the aid of their country."; | |
47 | ||
48 | UnicodeString testString(testChars,""); | |
49 | const UChar *testText = testString.getTerminatedBuffer(); | |
50 | ||
51 | UCharCharacterIterator iter(testText, u_strlen(testText)); | |
52 | UCharCharacterIterator* test2 = (UCharCharacterIterator*)iter.clone(); | |
53 | ||
54 | cout << "testText = " << testChars; | |
55 | ||
56 | if (iter != *test2 ) { | |
57 | printf("clone() or equals() failed: Two clones tested unequal\n"); | |
58 | } | |
59 | ||
60 | UnicodeString result1, result2; | |
61 | // getting and comparing the text within the iterators | |
62 | iter.getText(result1); | |
63 | test2->getText(result2); | |
64 | if (result1 != result2) { | |
65 | printf("iter.getText() != clone.getText()\n"); | |
66 | } | |
67 | ||
68 | cout << endl; | |
69 | // Demonstrates seeking forward using the iterator. | |
70 | cout << "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 | printf("startIndex() or endIndex() failed\n"); | |
78 | } | |
79 | ||
80 | ||
81 | // Testing forward iteration... | |
82 | do { | |
83 | if (c == CharacterIterator::DONE && i != u_strlen(testText)) { | |
84 | printf("Iterator reached end prematurely"); | |
85 | } | |
86 | else if (c != testText[i]) { | |
87 | printf("Character mismatch at position %d\n" + i); | |
88 | } | |
89 | if (iter.current() != c) { | |
90 | printf("current() isn't working right"); | |
91 | } | |
92 | if (iter.getIndex() != i) { | |
93 | printf("getIndex() isn't working right\n"); | |
94 | } | |
95 | if (c != CharacterIterator::DONE) { | |
96 | c = iter.next(); | |
97 | i++; | |
98 | } | |
99 | ||
100 | cout << "|"; | |
101 | printUChar(c); | |
102 | ||
103 | } while (c != CharacterIterator::DONE); | |
104 | ||
105 | delete test2; | |
106 | cout << endl; | |
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 | printf("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 | printf("getText() failed\n"); | |
130 | } | |
131 | ||
132 | cout << "Backwards: "; | |
133 | UChar c = iter.last(); | |
134 | printUChar(c); | |
135 | int32_t i = iter.endIndex(); | |
136 | i--; // already printed out the last char | |
137 | if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) { | |
138 | printf("startIndex() or endIndex() failed\n"); | |
139 | } | |
140 | ||
141 | // Testing backward iteration over a range... | |
142 | do { | |
143 | if (c == CharacterIterator::DONE) { | |
144 | printf("Iterator reached end prematurely\n"); | |
145 | } | |
146 | else if (c != testText[i]) { | |
147 | printf("Character mismatch at position %d\n" + i); | |
148 | } | |
149 | if (iter.current() != c) { | |
150 | printf("current() isn't working right\n"); | |
151 | } | |
152 | if (iter.getIndex() != i) { | |
153 | printf("getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i); | |
154 | } | |
155 | if (c != CharacterIterator::DONE) { | |
156 | c = iter.previous(); | |
157 | i--; | |
158 | } | |
159 | cout << "|"; | |
160 | printUChar(c); | |
161 | } while (c != CharacterIterator::DONE); | |
162 | ||
163 | cout << endl; | |
164 | delete test2; | |
165 | } | |
166 | ||
167 | /* Creating and using text boundaries */ | |
168 | int main( void ) | |
169 | { | |
170 | cout << "ICU Iterator Sample Program (C++)\n\n"; | |
171 | ||
172 | Test t; | |
173 | ||
174 | cout << endl; | |
175 | cout << "Test::TestUCharIter()" << endl; | |
176 | t.TestUChariter(); | |
177 | cout << "-----" << endl; | |
178 | cout << "Test::TestStringchariter()" << endl; | |
179 | t.TestStringiter(); | |
180 | cout << "-----" << endl; | |
181 | ||
182 | return 0; | |
183 | } |