]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/citer/citer.cpp
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / samples / citer / citer.cpp
CommitLineData
374ca955
A
1/*
2*******************************************************************************
3*
4388f060 4* Copyright (C) 2002-2011, International Business Machines
73c04bcf 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{
4388f060 22 u_fprintf(out, "%S", &s);
374ca955
A
23}
24
73c04bcf
A
25void printUChar(UChar32 ch)
26{
27 if(ch < 127) {
46f4442e 28 u_fprintf(out, "%C", (UChar) ch);
73c04bcf 29 } else if (ch == CharacterIterator::DONE) {
46f4442e 30 u_fprintf(out, "[CharacterIterator::DONE = 0xFFFF]");
73c04bcf 31 } else {
46f4442e 32 u_fprintf(out, "[%X]", ch);
73c04bcf 33 }
374ca955
A
34}
35
36class Test
37{
38public:
46f4442e
A
39 void TestUChariter();
40 void TestStringiter();
374ca955
A
41};
42
43void Test::TestUChariter() {
73c04bcf 44 const char testChars[] = "Now is the time for all good men to come "
46f4442e 45 "to the aid of their country.";
73c04bcf
A
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();
46f4442e
A
52
53 u_fprintf(out, "testText = %s", testChars);
54
73c04bcf 55 if (iter != *test2 ) {
46f4442e 56 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
374ca955 57 }
46f4442e 58
73c04bcf
A
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) {
46f4442e 64 u_fprintf(out, "iter.getText() != clone.getText()\n");
73c04bcf 65 }
46f4442e 66
73c04bcf
A
67 u_fprintf(out, "\n");
68
46f4442e 69 // Demonstrates seeking forward using the iterator.
73c04bcf 70 u_fprintf(out, "Forward = ");
46f4442e 71
73c04bcf
A
72 UChar c = iter.first();
73 printUChar(c); // The first char
74 int32_t i = 0;
46f4442e 75
73c04bcf 76 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
46f4442e 77 u_fprintf(out, "startIndex() or endIndex() failed\n");
374ca955 78 }
46f4442e
A
79
80
73c04bcf
A
81 // Testing forward iteration...
82 do {
83 if (c == CharacterIterator::DONE && i != u_strlen(testText)) {
46f4442e 84 u_fprintf(out, "Iterator reached end prematurely");
73c04bcf
A
85 }
86 else if (c != testText[i]) {
46f4442e 87 u_fprintf(out, "Character mismatch at position %d\n" + i);
73c04bcf
A
88 }
89 if (iter.current() != c) {
46f4442e 90 u_fprintf(out, "current() isn't working right");
73c04bcf
A
91 }
92 if (iter.getIndex() != i) {
46f4442e 93 u_fprintf(out, "getIndex() isn't working right\n");
73c04bcf
A
94 }
95 if (c != CharacterIterator::DONE) {
96 c = iter.next();
97 i++;
98 }
46f4442e 99
73c04bcf
A
100 u_fprintf(out, "|");
101 printUChar(c);
46f4442e 102
73c04bcf 103 } while (c != CharacterIterator::DONE);
46f4442e 104
73c04bcf
A
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();
46f4442e 116
73c04bcf
A
117 StringCharacterIterator iter(testText, u_strlen(testText));
118 StringCharacterIterator* test2 = (StringCharacterIterator*)iter.clone();
46f4442e 119
73c04bcf 120 if (iter != *test2 ) {
46f4442e 121 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
374ca955 122 }
46f4442e 123
73c04bcf
A
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) {
46f4442e 129 u_fprintf(out, "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)) {
46f4442e 141 u_fprintf(out, "startIndex() or endIndex() failed\n");
73c04bcf 142 }
46f4442e 143
73c04bcf
A
144 // Testing backward iteration over a range...
145 do {
146 if (c == CharacterIterator::DONE) {
46f4442e 147 u_fprintf(out, "Iterator reached end prematurely\n");
73c04bcf
A
148 }
149 else if (c != testText[i]) {
46f4442e 150 u_fprintf(out, "Character mismatch at position %d\n", i);
73c04bcf
A
151 }
152 if (iter.current() != c) {
46f4442e 153 u_fprintf(out, "current() isn't working right\n");
73c04bcf
A
154 }
155 if (iter.getIndex() != i) {
46f4442e 156 u_fprintf(out, "getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i);
73c04bcf
A
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{
46f4442e 174 UErrorCode status = U_ZERO_ERROR;
73c04bcf 175
46f4442e 176 out = u_finit(stdout, NULL, NULL);
73c04bcf 177
46f4442e 178 u_fprintf(out, "ICU Iteration Sample Program (C++)\n\n");
73c04bcf 179
46f4442e
A
180 Test t;
181
182 u_fprintf(out, "\n");
183 u_fprintf(out, "Test::TestUCharIter()\n");
73c04bcf 184
46f4442e 185 t.TestUChariter();
73c04bcf 186
46f4442e
A
187 u_fprintf(out, "-----\n");
188 u_fprintf(out, "Test::TestStringchariter()\n");
73c04bcf 189
46f4442e 190 t.TestStringiter();
73c04bcf 191
46f4442e 192 u_fprintf(out, "-----\n");
73c04bcf 193
46f4442e 194 return 0;
374ca955 195}