]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/break/break.cpp
ICU-66108.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
0f5d89e8 20using namespace icu;
b75a7d8f 21
0f5d89e8 22U_CFUNC int c_main(void);
b75a7d8f
A
23
24void printUnicodeString(const UnicodeString &s) {
374ca955
A
25 char charBuf[1000];
26 s.extract(0, s.length(), charBuf, sizeof(charBuf)-1, 0);
27 charBuf[sizeof(charBuf)-1] = 0;
28 printf("%s", charBuf);
b75a7d8f
A
29}
30
31
b75a7d8f 32void printTextRange( BreakIterator& iterator,
374ca955 33 int32_t start, int32_t end )
b75a7d8f 34{
374ca955
A
35 CharacterIterator *strIter = iterator.getText().clone();
36 UnicodeString s;
37 strIter->getText(s);
38
39 printf(" %ld %ld\t", (long)start, (long)end);
40 printUnicodeString(UnicodeString(s, 0, start));
41 printf("|");
42 printUnicodeString(UnicodeString(s, start, end-start));
43 printf("|");
44 printUnicodeString(UnicodeString(s, end));
45 puts("");
46 delete strIter;
b75a7d8f
A
47}
48
49
50/* Print each element in order: */
51void printEachForward( BreakIterator& boundary)
52{
374ca955
A
53 int32_t start = boundary.first();
54 for (int32_t end = boundary.next();
55 end != BreakIterator::DONE;
56 start = end, end = boundary.next())
b75a7d8f 57 {
374ca955 58 printTextRange( boundary, start, end );
b75a7d8f
A
59 }
60}
61
62/* Print each element in reverse order: */
63void printEachBackward( BreakIterator& boundary)
64{
374ca955
A
65 int32_t end = boundary.last();
66 for (int32_t start = boundary.previous();
67 start != BreakIterator::DONE;
68 end = start, start = boundary.previous())
b75a7d8f 69 {
374ca955 70 printTextRange( boundary, start, end );
b75a7d8f
A
71 }
72}
73
74/* Print the first element */
75void printFirst(BreakIterator& boundary)
76{
374ca955
A
77 int32_t start = boundary.first();
78 int32_t end = boundary.next();
79 printTextRange( boundary, start, end );
b75a7d8f
A
80}
81
82/* Print the last element */
83void printLast(BreakIterator& boundary)
84{
374ca955
A
85 int32_t end = boundary.last();
86 int32_t start = boundary.previous();
87 printTextRange( boundary, start, end );
b75a7d8f
A
88}
89
90/* Print the element at a specified position */
91void printAt(BreakIterator &boundary, int32_t pos )
92{
374ca955
A
93 int32_t end = boundary.following(pos);
94 int32_t start = boundary.previous();
95 printTextRange( boundary, start, end );
b75a7d8f
A
96}
97
98/* Creating and using text boundaries */
99int main( void )
100{
374ca955
A
101 puts("ICU Break Iterator Sample Program\n");
102 puts("C++ Break Iteration\n");
103 BreakIterator* boundary;
104 UnicodeString stringToExamine("Aaa bbb ccc. Ddd eee fff.");
105 printf("Examining: ");
106 printUnicodeString(stringToExamine);
107 puts("");
108
109 //print each sentence in forward and reverse order
110 UErrorCode status = U_ZERO_ERROR;
111 boundary = BreakIterator::createSentenceInstance(
112 Locale::getUS(), status );
113 if (U_FAILURE(status)) {
114 printf("failed to create sentence break iterator. status = %s",
115 u_errorName(status));
116 exit(1);
117 }
118
119 boundary->setText(stringToExamine);
120 puts("\n Sentence Boundaries... ");
121 puts("----- forward: -----------");
122 printEachForward(*boundary);
123 puts("----- backward: ----------");
124 printEachBackward(*boundary);
125 delete boundary;
126
127 //print each word in order
128 printf("\n Word Boundaries... \n");
129 boundary = BreakIterator::createWordInstance(
130 Locale::getUS(), status);
131 boundary->setText(stringToExamine);
132 puts("----- forward: -----------");
133 printEachForward(*boundary);
134 //print first element
135 puts("----- first: -------------");
136 printFirst(*boundary);
137 //print last element
138 puts("----- last: --------------");
139 printLast(*boundary);
140 //print word at charpos 10
141 puts("----- at pos 10: ---------");
142 printAt(*boundary, 10 );
143
144 delete boundary;
145
146 puts("\nEnd C++ Break Iteration");
147
148 // Call the C version
149 return c_main();
b75a7d8f 150}