• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 *******************************************************************************
3 *
4 *   Copyright (C) 2002-2003, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 *******************************************************************************
8 */
9 
10 #include <stdio.h>
11 #include <unicode/brkiter.h>
12 #include <stdlib.h>
13 
14 U_CFUNC int c_main(void);
15 
16 
printUnicodeString(const UnicodeString & s)17 void printUnicodeString(const UnicodeString &s) {
18     char charBuf[1000];
19     s.extract(0, s.length(), charBuf, sizeof(charBuf)-1, 0);
20     charBuf[sizeof(charBuf)-1] = 0;
21     printf("%s", charBuf);
22 }
23 
24 
printTextRange(BreakIterator & iterator,int32_t start,int32_t end)25 void printTextRange( BreakIterator& iterator,
26                     int32_t start, int32_t end )
27 {
28     CharacterIterator *strIter = iterator.getText().clone();
29     UnicodeString  s;
30     strIter->getText(s);
31 
32     printf(" %ld %ld\t", (long)start, (long)end);
33     printUnicodeString(UnicodeString(s, 0, start));
34     printf("|");
35     printUnicodeString(UnicodeString(s, start, end-start));
36     printf("|");
37     printUnicodeString(UnicodeString(s, end));
38     puts("");
39     delete strIter;
40 }
41 
42 
43 /* Print each element in order: */
printEachForward(BreakIterator & boundary)44 void printEachForward( BreakIterator& boundary)
45 {
46     int32_t start = boundary.first();
47     for (int32_t end = boundary.next();
48          end != BreakIterator::DONE;
49          start = end, end = boundary.next())
50     {
51         printTextRange( boundary, start, end );
52     }
53 }
54 
55 /* Print each element in reverse order: */
printEachBackward(BreakIterator & boundary)56 void printEachBackward( BreakIterator& boundary)
57 {
58     int32_t end = boundary.last();
59     for (int32_t start = boundary.previous();
60          start != BreakIterator::DONE;
61          end = start, start = boundary.previous())
62     {
63         printTextRange( boundary, start, end );
64     }
65 }
66 
67 /* Print the first element */
printFirst(BreakIterator & boundary)68 void printFirst(BreakIterator& boundary)
69 {
70     int32_t start = boundary.first();
71     int32_t end = boundary.next();
72     printTextRange( boundary, start, end );
73 }
74 
75 /* Print the last element */
printLast(BreakIterator & boundary)76 void printLast(BreakIterator& boundary)
77 {
78     int32_t end = boundary.last();
79     int32_t start = boundary.previous();
80     printTextRange( boundary, start, end );
81 }
82 
83 /* Print the element at a specified position */
printAt(BreakIterator & boundary,int32_t pos)84 void printAt(BreakIterator &boundary, int32_t pos )
85 {
86     int32_t end = boundary.following(pos);
87     int32_t start = boundary.previous();
88     printTextRange( boundary, start, end );
89 }
90 
91 /* Creating and using text boundaries */
main(void)92 int main( void )
93 {
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);
100     puts("");
101 
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));
109         exit(1);
110     }
111 
112     boundary->setText(stringToExamine);
113     puts("\n Sentence Boundaries... ");
114     puts("----- forward: -----------");
115     printEachForward(*boundary);
116     puts("----- backward: ----------");
117     printEachBackward(*boundary);
118     delete boundary;
119 
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);
130     //print last element
131     puts("----- last: --------------");
132     printLast(*boundary);
133     //print word at charpos 10
134     puts("----- at pos 10: ---------");
135     printAt(*boundary, 10 );
136 
137     delete boundary;
138 
139     puts("\nEnd C++ Break Iteration");
140 
141     // Call the C version
142     return c_main();
143 }
144