]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/break/break.cpp
2 *******************************************************************************
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2002-2003, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
17 #include <unicode/brkiter.h>
20 U_CFUNC
int c_main(void);
23 void printUnicodeString(const UnicodeString
&s
) {
25 s
.extract(0, s
.length(), charBuf
, sizeof(charBuf
)-1, 0);
26 charBuf
[sizeof(charBuf
)-1] = 0;
27 printf("%s", charBuf
);
31 void printTextRange( BreakIterator
& iterator
,
32 int32_t start
, int32_t end
)
34 CharacterIterator
*strIter
= iterator
.getText().clone();
38 printf(" %ld %ld\t", (long)start
, (long)end
);
39 printUnicodeString(UnicodeString(s
, 0, start
));
41 printUnicodeString(UnicodeString(s
, start
, end
-start
));
43 printUnicodeString(UnicodeString(s
, end
));
49 /* Print each element in order: */
50 void printEachForward( BreakIterator
& boundary
)
52 int32_t start
= boundary
.first();
53 for (int32_t end
= boundary
.next();
54 end
!= BreakIterator::DONE
;
55 start
= end
, end
= boundary
.next())
57 printTextRange( boundary
, start
, end
);
61 /* Print each element in reverse order: */
62 void printEachBackward( BreakIterator
& boundary
)
64 int32_t end
= boundary
.last();
65 for (int32_t start
= boundary
.previous();
66 start
!= BreakIterator::DONE
;
67 end
= start
, start
= boundary
.previous())
69 printTextRange( boundary
, start
, end
);
73 /* Print the first element */
74 void printFirst(BreakIterator
& boundary
)
76 int32_t start
= boundary
.first();
77 int32_t end
= boundary
.next();
78 printTextRange( boundary
, start
, end
);
81 /* Print the last element */
82 void printLast(BreakIterator
& boundary
)
84 int32_t end
= boundary
.last();
85 int32_t start
= boundary
.previous();
86 printTextRange( boundary
, start
, end
);
89 /* Print the element at a specified position */
90 void printAt(BreakIterator
&boundary
, int32_t pos
)
92 int32_t end
= boundary
.following(pos
);
93 int32_t start
= boundary
.previous();
94 printTextRange( boundary
, start
, end
);
97 /* Creating and using text boundaries */
100 puts("ICU Break Iterator Sample Program\n");
101 puts("C++ Break Iteration\n");
102 BreakIterator
* boundary
;
103 UnicodeString
stringToExamine("Aaa bbb ccc. Ddd eee fff.");
104 printf("Examining: ");
105 printUnicodeString(stringToExamine
);
108 //print each sentence in forward and reverse order
109 UErrorCode status
= U_ZERO_ERROR
;
110 boundary
= BreakIterator::createSentenceInstance(
111 Locale::getUS(), status
);
112 if (U_FAILURE(status
)) {
113 printf("failed to create sentence break iterator. status = %s",
114 u_errorName(status
));
118 boundary
->setText(stringToExamine
);
119 puts("\n Sentence Boundaries... ");
120 puts("----- forward: -----------");
121 printEachForward(*boundary
);
122 puts("----- backward: ----------");
123 printEachBackward(*boundary
);
126 //print each word in order
127 printf("\n Word Boundaries... \n");
128 boundary
= BreakIterator::createWordInstance(
129 Locale::getUS(), status
);
130 boundary
->setText(stringToExamine
);
131 puts("----- forward: -----------");
132 printEachForward(*boundary
);
133 //print first element
134 puts("----- first: -------------");
135 printFirst(*boundary
);
137 puts("----- last: --------------");
138 printLast(*boundary
);
139 //print word at charpos 10
140 puts("----- at pos 10: ---------");
141 printAt(*boundary
, 10 );
145 puts("\nEnd C++ Break Iteration");
147 // Call the C version