]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/citer/citer.cpp
2 *******************************************************************************
4 * Copyright (C) 2002-2011, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/uchriter.h"
11 #include "unicode/schriter.h"
12 #include "unicode/ustring.h"
14 #include <unicode/brkiter.h>
15 #include <unicode/ustdio.h>
20 void printUnicodeString(const UnicodeString
&s
)
22 u_fprintf(out
, "%S", &s
);
25 void printUChar(UChar32 ch
)
28 u_fprintf(out
, "%C", (UChar
) ch
);
29 } else if (ch
== CharacterIterator::DONE
) {
30 u_fprintf(out
, "[CharacterIterator::DONE = 0xFFFF]");
32 u_fprintf(out
, "[%X]", ch
);
40 void TestStringiter();
43 void Test::TestUChariter() {
44 const char testChars
[] = "Now is the time for all good men to come "
45 "to the aid of their country.";
47 UnicodeString
testString(testChars
,"");
48 const UChar
*testText
= testString
.getTerminatedBuffer();
50 UCharCharacterIterator
iter(testText
, u_strlen(testText
));
51 UCharCharacterIterator
* test2
= (UCharCharacterIterator
*)iter
.clone();
53 u_fprintf(out
, "testText = %s", testChars
);
55 if (iter
!= *test2
) {
56 u_fprintf(out
, "clone() or equals() failed: Two clones tested unequal\n");
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 u_fprintf(out
, "iter.getText() != clone.getText()\n");
69 // Demonstrates seeking forward using the iterator.
70 u_fprintf(out
, "Forward = ");
72 UChar c
= iter
.first();
73 printUChar(c
); // The first char
76 if (iter
.startIndex() != 0 || iter
.endIndex() != u_strlen(testText
)) {
77 u_fprintf(out
, "startIndex() or endIndex() failed\n");
81 // Testing forward iteration...
83 if (c
== CharacterIterator::DONE
&& i
!= u_strlen(testText
)) {
84 u_fprintf(out
, "Iterator reached end prematurely");
86 else if (c
!= testText
[i
]) {
87 u_fprintf(out
, "Character mismatch at position %d\n" + i
);
89 if (iter
.current() != c
) {
90 u_fprintf(out
, "current() isn't working right");
92 if (iter
.getIndex() != i
) {
93 u_fprintf(out
, "getIndex() isn't working right\n");
95 if (c
!= CharacterIterator::DONE
) {
103 } while (c
!= CharacterIterator::DONE
);
106 u_fprintf(out
, "\n");
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.";
114 UnicodeString
testString(testChars
,"");
115 const UChar
*testText
= testString
.getTerminatedBuffer();
117 StringCharacterIterator
iter(testText
, u_strlen(testText
));
118 StringCharacterIterator
* test2
= (StringCharacterIterator
*)iter
.clone();
120 if (iter
!= *test2
) {
121 u_fprintf(out
, "clone() or equals() failed: Two clones tested unequal\n");
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 u_fprintf(out
, "getText() failed\n");
132 u_fprintf(out
, "Backwards: ");
134 UChar c
= iter
.last();
135 int32_t i
= iter
.endIndex();
138 i
--; // already printed out the last char
140 if (iter
.startIndex() != 0 || iter
.endIndex() != u_strlen(testText
)) {
141 u_fprintf(out
, "startIndex() or endIndex() failed\n");
144 // Testing backward iteration over a range...
146 if (c
== CharacterIterator::DONE
) {
147 u_fprintf(out
, "Iterator reached end prematurely\n");
149 else if (c
!= testText
[i
]) {
150 u_fprintf(out
, "Character mismatch at position %d\n", i
);
152 if (iter
.current() != c
) {
153 u_fprintf(out
, "current() isn't working right\n");
155 if (iter
.getIndex() != i
) {
156 u_fprintf(out
, "getIndex() isn't working right [%d should be %d]\n", iter
.getIndex(), i
);
158 if (c
!= CharacterIterator::DONE
) {
165 } while (c
!= CharacterIterator::DONE
);
167 u_fprintf(out
, "\n");
171 /* Creating and using text boundaries */
174 UErrorCode status
= U_ZERO_ERROR
;
176 out
= u_finit(stdout
, NULL
, NULL
);
178 u_fprintf(out
, "ICU Iteration Sample Program (C++)\n\n");
182 u_fprintf(out
, "\n");
183 u_fprintf(out
, "Test::TestUCharIter()\n");
187 u_fprintf(out
, "-----\n");
188 u_fprintf(out
, "Test::TestStringchariter()\n");
192 u_fprintf(out
, "-----\n");