]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ubrk.cpp
ICU-531.30.tar.gz
[apple/icu.git] / icuSources / common / ubrk.cpp
CommitLineData
b75a7d8f 1/*
46f4442e 2********************************************************************************
57a6839d 3* Copyright (C) 1996-2013, International Business Machines
b75a7d8f 4* Corporation and others. All Rights Reserved.
46f4442e 5********************************************************************************
b75a7d8f
A
6*/
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_BREAK_ITERATION
11
12#include "unicode/ubrk.h"
13
14#include "unicode/brkiter.h"
15#include "unicode/uloc.h"
16#include "unicode/ustring.h"
17#include "unicode/uchriter.h"
18#include "unicode/rbbi.h"
19#include "rbbirb.h"
73c04bcf 20#include "uassert.h"
b75a7d8f
A
21
22U_NAMESPACE_USE
23
46f4442e 24//------------------------------------------------------------------------------
b75a7d8f
A
25//
26// ubrk_open Create a canned type of break iterator based on type (word, line, etc.)
27// and locale.
28//
46f4442e 29//------------------------------------------------------------------------------
b75a7d8f
A
30U_CAPI UBreakIterator* U_EXPORT2
31ubrk_open(UBreakIteratorType type,
32 const char *locale,
33 const UChar *text,
34 int32_t textLength,
35 UErrorCode *status)
36{
37
38 if(U_FAILURE(*status)) return 0;
39
40 BreakIterator *result = 0;
41
42 switch(type) {
43
44 case UBRK_CHARACTER:
45 result = BreakIterator::createCharacterInstance(Locale(locale), *status);
46 break;
47
48 case UBRK_WORD:
49 result = BreakIterator::createWordInstance(Locale(locale), *status);
50 break;
51
52 case UBRK_LINE:
53 result = BreakIterator::createLineInstance(Locale(locale), *status);
54 break;
55
56 case UBRK_SENTENCE:
57 result = BreakIterator::createSentenceInstance(Locale(locale), *status);
58 break;
59
60 case UBRK_TITLE:
61 result = BreakIterator::createTitleInstance(Locale(locale), *status);
62 break;
73c04bcf
A
63
64 default:
65 *status = U_ILLEGAL_ARGUMENT_ERROR;
b75a7d8f
A
66 }
67
68 // check for allocation error
69 if (U_FAILURE(*status)) {
70 return 0;
71 }
72 if(result == 0) {
73 *status = U_MEMORY_ALLOCATION_ERROR;
74 return 0;
75 }
76
b75a7d8f 77
73c04bcf
A
78 UBreakIterator *uBI = (UBreakIterator *)result;
79 if (text != NULL) {
80 ubrk_setText(uBI, text, textLength, status);
81 }
82 return uBI;
b75a7d8f
A
83}
84
85
86
46f4442e 87//------------------------------------------------------------------------------
b75a7d8f
A
88//
89// ubrk_openRules open a break iterator from a set of break rules.
90// Invokes the rule builder.
91//
46f4442e 92//------------------------------------------------------------------------------
b75a7d8f
A
93U_CAPI UBreakIterator* U_EXPORT2
94ubrk_openRules( const UChar *rules,
95 int32_t rulesLength,
96 const UChar *text,
97 int32_t textLength,
98 UParseError *parseErr,
99 UErrorCode *status) {
100
101 if (status == NULL || U_FAILURE(*status)){
102 return 0;
103 }
104
105 BreakIterator *result = 0;
106 UnicodeString ruleString(rules, rulesLength);
46f4442e 107 result = RBBIRuleBuilder::createRuleBasedBreakIterator(ruleString, parseErr, *status);
b75a7d8f
A
108 if(U_FAILURE(*status)) {
109 return 0;
110 }
111
73c04bcf 112 UBreakIterator *uBI = (UBreakIterator *)result;
b75a7d8f 113 if (text != NULL) {
73c04bcf 114 ubrk_setText(uBI, text, textLength, status);
b75a7d8f 115 }
73c04bcf 116 return uBI;
b75a7d8f
A
117}
118
119
120
121
122
123U_CAPI UBreakIterator * U_EXPORT2
124ubrk_safeClone(
125 const UBreakIterator *bi,
57a6839d 126 void * /*stackBuffer*/,
b75a7d8f
A
127 int32_t *pBufferSize,
128 UErrorCode *status)
129{
130 if (status == NULL || U_FAILURE(*status)){
57a6839d 131 return NULL;
b75a7d8f 132 }
57a6839d 133 if (bi == NULL) {
b75a7d8f 134 *status = U_ILLEGAL_ARGUMENT_ERROR;
57a6839d
A
135 return NULL;
136 }
137 if (pBufferSize != NULL) {
138 int32_t inputSize = *pBufferSize;
139 *pBufferSize = 1;
140 if (inputSize == 0) {
141 return NULL; // preflighting for deprecated functionality
142 }
b75a7d8f 143 }
57a6839d
A
144 BreakIterator *newBI = ((BreakIterator *)bi)->clone();
145 if (newBI == NULL) {
146 *status = U_MEMORY_ALLOCATION_ERROR;
147 } else {
148 *status = U_SAFECLONE_ALLOCATED_WARNING;
73c04bcf 149 }
57a6839d 150 return (UBreakIterator *)newBI;
b75a7d8f
A
151}
152
153
154
155U_CAPI void U_EXPORT2
156ubrk_close(UBreakIterator *bi)
157{
57a6839d 158 delete (BreakIterator *)bi;
b75a7d8f
A
159}
160
161U_CAPI void U_EXPORT2
162ubrk_setText(UBreakIterator* bi,
163 const UChar* text,
164 int32_t textLength,
165 UErrorCode* status)
166{
73c04bcf
A
167 BreakIterator *brit = (BreakIterator *)bi;
168 UText ut = UTEXT_INITIALIZER;
169 utext_openUChars(&ut, text, textLength, status);
170 brit->setText(&ut, *status);
57a6839d 171 // A stack allocated UText wrapping a UChar * string
73c04bcf
A
172 // can be dumped without explicitly closing it.
173}
b75a7d8f 174
b75a7d8f 175
b75a7d8f 176
46f4442e 177U_CAPI void U_EXPORT2
73c04bcf
A
178ubrk_setUText(UBreakIterator *bi,
179 UText *text,
180 UErrorCode *status)
181{
182 RuleBasedBreakIterator *brit = (RuleBasedBreakIterator *)bi;
183 brit->RuleBasedBreakIterator::setText(text, *status);
b75a7d8f
A
184}
185
73c04bcf
A
186
187
188
189
b75a7d8f
A
190U_CAPI int32_t U_EXPORT2
191ubrk_current(const UBreakIterator *bi)
192{
193
73c04bcf 194 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::current();
b75a7d8f
A
195}
196
197U_CAPI int32_t U_EXPORT2
198ubrk_next(UBreakIterator *bi)
199{
200
73c04bcf 201 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::next();
b75a7d8f
A
202}
203
204U_CAPI int32_t U_EXPORT2
205ubrk_previous(UBreakIterator *bi)
206{
207
73c04bcf 208 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::previous();
b75a7d8f
A
209}
210
211U_CAPI int32_t U_EXPORT2
212ubrk_first(UBreakIterator *bi)
213{
214
73c04bcf 215 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::first();
b75a7d8f
A
216}
217
218U_CAPI int32_t U_EXPORT2
219ubrk_last(UBreakIterator *bi)
220{
221
73c04bcf 222 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::last();
b75a7d8f
A
223}
224
225U_CAPI int32_t U_EXPORT2
226ubrk_preceding(UBreakIterator *bi,
227 int32_t offset)
228{
229
73c04bcf 230 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::preceding(offset);
b75a7d8f
A
231}
232
233U_CAPI int32_t U_EXPORT2
234ubrk_following(UBreakIterator *bi,
235 int32_t offset)
236{
237
73c04bcf 238 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::following(offset);
b75a7d8f
A
239}
240
241U_CAPI const char* U_EXPORT2
242ubrk_getAvailable(int32_t index)
243{
244
245 return uloc_getAvailable(index);
246}
247
248U_CAPI int32_t U_EXPORT2
249ubrk_countAvailable()
250{
251
252 return uloc_countAvailable();
253}
254
255
256U_CAPI UBool U_EXPORT2
257ubrk_isBoundary(UBreakIterator *bi, int32_t offset)
258{
73c04bcf 259 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::isBoundary(offset);
b75a7d8f
A
260}
261
262
263U_CAPI int32_t U_EXPORT2
264ubrk_getRuleStatus(UBreakIterator *bi)
265{
73c04bcf 266 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatus();
b75a7d8f
A
267}
268
374ca955
A
269U_CAPI int32_t U_EXPORT2
270ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status)
271{
73c04bcf 272 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatusVec(fillInVec, capacity, *status);
374ca955
A
273}
274
275
276U_CAPI const char* U_EXPORT2
73c04bcf
A
277ubrk_getLocaleByType(const UBreakIterator *bi,
278 ULocDataLocaleType type,
374ca955
A
279 UErrorCode* status)
280{
281 if (bi == NULL) {
282 if (U_SUCCESS(*status)) {
283 *status = U_ILLEGAL_ARGUMENT_ERROR;
284 }
285 return NULL;
286 }
287 return ((BreakIterator*)bi)->getLocaleID(type, *status);
288}
289
290
4388f060
A
291void ubrk_refreshUText(UBreakIterator *bi,
292 UText *text,
293 UErrorCode *status)
294{
295 BreakIterator *bii = reinterpret_cast<BreakIterator *>(bi);
296 bii->refreshInputText(text, *status);
297}
298
299
300
b75a7d8f 301#endif /* #if !UCONFIG_NO_BREAK_ITERATION */