]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/citer/citer.cpp
2 *******************************************************************************
4 * Copyright (C) 2002-2003, 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"
15 #include <unicode/brkiter.h>
18 void printUnicodeString(const UnicodeString
&s
) {
20 s
.extract(0, s
.length(), charBuf
, sizeof(charBuf
)-1, 0);
21 charBuf
[sizeof(charBuf
)-1] = 0;
25 void printUChar(UChar32 ch
) {
27 charBuf
[sizeof(charBuf
)-1] = 0;
30 } else if (ch
== CharacterIterator::DONE
) {
31 cout
<< "[CharacterIterator::DONE = 0xFFFF]";
33 cout
<< "[" << ch
<< "]";
41 void TestStringiter();
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.";
48 UnicodeString
testString(testChars
,"");
49 const UChar
*testText
= testString
.getTerminatedBuffer();
51 UCharCharacterIterator
iter(testText
, u_strlen(testText
));
52 UCharCharacterIterator
* test2
= (UCharCharacterIterator
*)iter
.clone();
54 cout
<< "testText = " << testChars
;
56 if (iter
!= *test2
) {
57 printf("clone() or equals() failed: Two clones tested unequal\n");
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");
69 // Demonstrates seeking forward using the iterator.
72 UChar c
= iter
.first();
73 printUChar(c
); // The first char
76 if (iter
.startIndex() != 0 || iter
.endIndex() != u_strlen(testText
)) {
77 printf("startIndex() or endIndex() failed\n");
81 // Testing forward iteration...
83 if (c
== CharacterIterator::DONE
&& i
!= u_strlen(testText
)) {
84 printf("Iterator reached end prematurely");
86 else if (c
!= testText
[i
]) {
87 printf("Character mismatch at position %d\n" + i
);
89 if (iter
.current() != c
) {
90 printf("current() isn't working right");
92 if (iter
.getIndex() != i
) {
93 printf("getIndex() isn't working right\n");
95 if (c
!= CharacterIterator::DONE
) {
103 } while (c
!= CharacterIterator::DONE
);
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 printf("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 printf("getText() failed\n");
132 cout
<< "Backwards: ";
133 UChar c
= iter
.last();
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");
141 // Testing backward iteration over a range...
143 if (c
== CharacterIterator::DONE
) {
144 printf("Iterator reached end prematurely\n");
146 else if (c
!= testText
[i
]) {
147 printf("Character mismatch at position %d\n" + i
);
149 if (iter
.current() != c
) {
150 printf("current() isn't working right\n");
152 if (iter
.getIndex() != i
) {
153 printf("getIndex() isn't working right [%d should be %d]\n", iter
.getIndex(), i
);
155 if (c
!= CharacterIterator::DONE
) {
161 } while (c
!= CharacterIterator::DONE
);
167 /* Creating and using text boundaries */
170 cout
<< "ICU Iterator Sample Program (C++)\n\n";
175 cout
<< "Test::TestUCharIter()" << endl
;
177 cout
<< "-----" << endl
;
178 cout
<< "Test::TestStringchariter()" << endl
;
180 cout
<< "-----" << endl
;