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