]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/citer/citer.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / samples / citer / citer.cpp
CommitLineData
374ca955
A
1/*
2*******************************************************************************
3*
73c04bcf
A
4* Copyright (C) 2002-2005, International Business Machines
5* Corporation and others. All Rights Reserved.
374ca955
A
6*
7*******************************************************************************
8*/
9
10#include "unicode/uchriter.h"
11#include "unicode/schriter.h"
12#include "unicode/ustring.h"
13#include <stdio.h>
374ca955 14#include <unicode/brkiter.h>
73c04bcf 15#include <unicode/ustdio.h>
374ca955
A
16#include <stdlib.h>
17
73c04bcf
A
18static UFILE *out;
19
20void printUnicodeString(const UnicodeString &s)
21{
22 u_fprintf(out, "%S", s);
374ca955
A
23}
24
73c04bcf
A
25void 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 }
374ca955
A
34}
35
36class Test
37{
38public:
73c04bcf
A
39 void TestUChariter();
40 void TestStringiter();
374ca955
A
41};
42
43void Test::TestUChariter() {
73c04bcf
A
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 printf("clone() or equals() failed: Two clones tested unequal\n");
374ca955 57 }
73c04bcf
A
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 printf("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 printf("startIndex() or endIndex() failed\n");
374ca955
A
78 }
79
73c04bcf
A
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 }
374ca955 99
73c04bcf
A
100 u_fprintf(out, "|");
101 printUChar(c);
102
103 } while (c != CharacterIterator::DONE);
104
105 delete test2;
106 u_fprintf(out, "\n");
374ca955
A
107}
108
109
110void Test::TestStringiter() {
73c04bcf
A
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");
374ca955 122 }
73c04bcf
A
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");
374ca955 130 }
73c04bcf
A
131
132 u_fprintf(out, "Backwards: ");
133
134 UChar c = iter.last();
135 int32_t i = iter.endIndex();
136
374ca955 137 printUChar(c);
73c04bcf
A
138 i--; // already printed out the last char
139
140 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
141 printf("startIndex() or endIndex() failed\n");
142 }
143
144 // Testing backward iteration over a range...
145 do {
146 if (c == CharacterIterator::DONE) {
147 printf("Iterator reached end prematurely\n");
148 }
149 else if (c != testText[i]) {
150 printf("Character mismatch at position %d\n" + i);
151 }
152 if (iter.current() != c) {
153 printf("current() isn't working right\n");
154 }
155 if (iter.getIndex() != i) {
156 printf("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 }
374ca955 162
73c04bcf
A
163 u_fprintf(out, "|");
164 printUChar(c);
165 } while (c != CharacterIterator::DONE);
166
167 u_fprintf(out, "\n");
168 delete test2;
374ca955
A
169}
170
171/* Creating and using text boundaries */
172int main( void )
173{
73c04bcf
A
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;
374ca955 195}