]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/break/break.cpp
ICU-59117.0.1.tar.gz
[apple/icu.git] / icuSources / samples / break / break.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
3*
f3c0d7a5
A
4* © 2016 and later: Unicode, Inc. and others.
5* License & terms of use: http://www.unicode.org/copyright.html#License
6*
7*******************************************************************************
8*******************************************************************************
9*
b75a7d8f
A
10* Copyright (C) 2002-2003, International Business Machines
11* Corporation and others. All Rights Reserved.
12*
13*******************************************************************************
14*/
15
374ca955 16#include <stdio.h>
b75a7d8f
A
17#include <unicode/brkiter.h>
18#include <stdlib.h>
19
20U_CFUNC int c_main(void);
21
22
23void printUnicodeString(const UnicodeString &s) {
374ca955
A
24 char charBuf[1000];
25 s.extract(0, s.length(), charBuf, sizeof(charBuf)-1, 0);
26 charBuf[sizeof(charBuf)-1] = 0;
27 printf("%s", charBuf);
b75a7d8f
A
28}
29
30
b75a7d8f 31void printTextRange( BreakIterator& iterator,
374ca955 32 int32_t start, int32_t end )
b75a7d8f 33{
374ca955
A
34 CharacterIterator *strIter = iterator.getText().clone();
35 UnicodeString s;
36 strIter->getText(s);
37
38 printf(" %ld %ld\t", (long)start, (long)end);
39 printUnicodeString(UnicodeString(s, 0, start));
40 printf("|");
41 printUnicodeString(UnicodeString(s, start, end-start));
42 printf("|");
43 printUnicodeString(UnicodeString(s, end));
44 puts("");
45 delete strIter;
b75a7d8f
A
46}
47
48
49/* Print each element in order: */
50void printEachForward( BreakIterator& boundary)
51{
374ca955
A
52 int32_t start = boundary.first();
53 for (int32_t end = boundary.next();
54 end != BreakIterator::DONE;
55 start = end, end = boundary.next())
b75a7d8f 56 {
374ca955 57 printTextRange( boundary, start, end );
b75a7d8f
A
58 }
59}
60
61/* Print each element in reverse order: */
62void printEachBackward( BreakIterator& boundary)
63{
374ca955
A
64 int32_t end = boundary.last();
65 for (int32_t start = boundary.previous();
66 start != BreakIterator::DONE;
67 end = start, start = boundary.previous())
b75a7d8f 68 {
374ca955 69 printTextRange( boundary, start, end );
b75a7d8f
A
70 }
71}
72
73/* Print the first element */
74void printFirst(BreakIterator& boundary)
75{
374ca955
A
76 int32_t start = boundary.first();
77 int32_t end = boundary.next();
78 printTextRange( boundary, start, end );
b75a7d8f
A
79}
80
81/* Print the last element */
82void printLast(BreakIterator& boundary)
83{
374ca955
A
84 int32_t end = boundary.last();
85 int32_t start = boundary.previous();
86 printTextRange( boundary, start, end );
b75a7d8f
A
87}
88
89/* Print the element at a specified position */
90void printAt(BreakIterator &boundary, int32_t pos )
91{
374ca955
A
92 int32_t end = boundary.following(pos);
93 int32_t start = boundary.previous();
94 printTextRange( boundary, start, end );
b75a7d8f
A
95}
96
97/* Creating and using text boundaries */
98int main( void )
99{
374ca955
A
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);
106 puts("");
107
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));
115 exit(1);
116 }
117
118 boundary->setText(stringToExamine);
119 puts("\n Sentence Boundaries... ");
120 puts("----- forward: -----------");
121 printEachForward(*boundary);
122 puts("----- backward: ----------");
123 printEachBackward(*boundary);
124 delete boundary;
125
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);
136 //print last element
137 puts("----- last: --------------");
138 printLast(*boundary);
139 //print word at charpos 10
140 puts("----- at pos 10: ---------");
141 printAt(*boundary, 10 );
142
143 delete boundary;
144
145 puts("\nEnd C++ Break Iteration");
146
147 // Call the C version
148 return c_main();
b75a7d8f 149}