]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/break/break.cpp
2 *******************************************************************************
4 * Copyright (C) 2002-2003, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
11 #include <unicode/brkiter.h>
14 U_CFUNC
int c_main(void);
17 void printUnicodeString(const UnicodeString
&s
) {
19 s
.extract(0, s
.length(), charBuf
, sizeof(charBuf
)-1, 0);
20 charBuf
[sizeof(charBuf
)-1] = 0;
21 printf("%s", charBuf
);
25 void printTextRange( BreakIterator
& iterator
,
26 int32_t start
, int32_t end
)
28 CharacterIterator
*strIter
= iterator
.getText().clone();
32 printf(" %ld %ld\t", (long)start
, (long)end
);
33 printUnicodeString(UnicodeString(s
, 0, start
));
35 printUnicodeString(UnicodeString(s
, start
, end
-start
));
37 printUnicodeString(UnicodeString(s
, end
));
43 /* Print each element in order: */
44 void printEachForward( BreakIterator
& boundary
)
46 int32_t start
= boundary
.first();
47 for (int32_t end
= boundary
.next();
48 end
!= BreakIterator::DONE
;
49 start
= end
, end
= boundary
.next())
51 printTextRange( boundary
, start
, end
);
55 /* Print each element in reverse order: */
56 void printEachBackward( BreakIterator
& boundary
)
58 int32_t end
= boundary
.last();
59 for (int32_t start
= boundary
.previous();
60 start
!= BreakIterator::DONE
;
61 end
= start
, start
= boundary
.previous())
63 printTextRange( boundary
, start
, end
);
67 /* Print the first element */
68 void printFirst(BreakIterator
& boundary
)
70 int32_t start
= boundary
.first();
71 int32_t end
= boundary
.next();
72 printTextRange( boundary
, start
, end
);
75 /* Print the last element */
76 void printLast(BreakIterator
& boundary
)
78 int32_t end
= boundary
.last();
79 int32_t start
= boundary
.previous();
80 printTextRange( boundary
, start
, end
);
83 /* Print the element at a specified position */
84 void printAt(BreakIterator
&boundary
, int32_t pos
)
86 int32_t end
= boundary
.following(pos
);
87 int32_t start
= boundary
.previous();
88 printTextRange( boundary
, start
, end
);
91 /* Creating and using text boundaries */
94 puts("ICU Break Iterator Sample Program\n");
95 puts("C++ Break Iteration\n");
96 BreakIterator
* boundary
;
97 UnicodeString
stringToExamine("Aaa bbb ccc. Ddd eee fff.");
98 printf("Examining: ");
99 printUnicodeString(stringToExamine
);
102 //print each sentence in forward and reverse order
103 UErrorCode status
= U_ZERO_ERROR
;
104 boundary
= BreakIterator::createSentenceInstance(
105 Locale::getUS(), status
);
106 if (U_FAILURE(status
)) {
107 printf("failed to create sentence break iterator. status = %s",
108 u_errorName(status
));
112 boundary
->setText(stringToExamine
);
113 puts("\n Sentence Boundaries... ");
114 puts("----- forward: -----------");
115 printEachForward(*boundary
);
116 puts("----- backward: ----------");
117 printEachBackward(*boundary
);
120 //print each word in order
121 printf("\n Word Boundaries... \n");
122 boundary
= BreakIterator::createWordInstance(
123 Locale::getUS(), status
);
124 boundary
->setText(stringToExamine
);
125 puts("----- forward: -----------");
126 printEachForward(*boundary
);
127 //print first element
128 puts("----- first: -------------");
129 printFirst(*boundary
);
131 puts("----- last: --------------");
132 printLast(*boundary
);
133 //print word at charpos 10
134 puts("----- at pos 10: ---------");
135 printAt(*boundary
, 10 );
139 puts("\nEnd C++ Break Iteration");
141 // Call the C version