]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ubrk.cpp
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / common / ubrk.cpp
CommitLineData
b75a7d8f 1/*
46f4442e 2********************************************************************************
2ca993e8 3* Copyright (C) 1996-2015, 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 UText ut = UTEXT_INITIALIZER;
168 utext_openUChars(&ut, text, textLength, status);
b331163b 169 ((BreakIterator*)bi)->setText(&ut, *status);
57a6839d 170 // A stack allocated UText wrapping a UChar * string
73c04bcf
A
171 // can be dumped without explicitly closing it.
172}
b75a7d8f 173
b75a7d8f 174
b75a7d8f 175
46f4442e 176U_CAPI void U_EXPORT2
73c04bcf
A
177ubrk_setUText(UBreakIterator *bi,
178 UText *text,
179 UErrorCode *status)
180{
b331163b 181 ((BreakIterator*)bi)->setText(text, *status);
b75a7d8f
A
182}
183
73c04bcf
A
184
185
186
187
b75a7d8f
A
188U_CAPI int32_t U_EXPORT2
189ubrk_current(const UBreakIterator *bi)
190{
191
b331163b 192 return ((BreakIterator*)bi)->current();
b75a7d8f
A
193}
194
195U_CAPI int32_t U_EXPORT2
196ubrk_next(UBreakIterator *bi)
197{
198
b331163b 199 return ((BreakIterator*)bi)->next();
b75a7d8f
A
200}
201
202U_CAPI int32_t U_EXPORT2
203ubrk_previous(UBreakIterator *bi)
204{
205
b331163b 206 return ((BreakIterator*)bi)->previous();
b75a7d8f
A
207}
208
209U_CAPI int32_t U_EXPORT2
210ubrk_first(UBreakIterator *bi)
211{
212
b331163b 213 return ((BreakIterator*)bi)->first();
b75a7d8f
A
214}
215
216U_CAPI int32_t U_EXPORT2
217ubrk_last(UBreakIterator *bi)
218{
219
b331163b 220 return ((BreakIterator*)bi)->last();
b75a7d8f
A
221}
222
223U_CAPI int32_t U_EXPORT2
224ubrk_preceding(UBreakIterator *bi,
225 int32_t offset)
226{
227
b331163b 228 return ((BreakIterator*)bi)->preceding(offset);
b75a7d8f
A
229}
230
231U_CAPI int32_t U_EXPORT2
232ubrk_following(UBreakIterator *bi,
233 int32_t offset)
234{
235
b331163b 236 return ((BreakIterator*)bi)->following(offset);
b75a7d8f
A
237}
238
239U_CAPI const char* U_EXPORT2
240ubrk_getAvailable(int32_t index)
241{
242
243 return uloc_getAvailable(index);
244}
245
246U_CAPI int32_t U_EXPORT2
247ubrk_countAvailable()
248{
249
250 return uloc_countAvailable();
251}
252
253
254U_CAPI UBool U_EXPORT2
255ubrk_isBoundary(UBreakIterator *bi, int32_t offset)
256{
b331163b 257 return ((BreakIterator*)bi)->isBoundary(offset);
b75a7d8f
A
258}
259
260
261U_CAPI int32_t U_EXPORT2
262ubrk_getRuleStatus(UBreakIterator *bi)
263{
b331163b 264 return ((BreakIterator*)bi)->getRuleStatus();
b75a7d8f
A
265}
266
374ca955
A
267U_CAPI int32_t U_EXPORT2
268ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status)
269{
b331163b 270 return ((BreakIterator*)bi)->getRuleStatusVec(fillInVec, capacity, *status);
374ca955
A
271}
272
273
274U_CAPI const char* U_EXPORT2
73c04bcf
A
275ubrk_getLocaleByType(const UBreakIterator *bi,
276 ULocDataLocaleType type,
374ca955
A
277 UErrorCode* status)
278{
279 if (bi == NULL) {
280 if (U_SUCCESS(*status)) {
281 *status = U_ILLEGAL_ARGUMENT_ERROR;
282 }
283 return NULL;
284 }
285 return ((BreakIterator*)bi)->getLocaleID(type, *status);
286}
287
288
4388f060
A
289void ubrk_refreshUText(UBreakIterator *bi,
290 UText *text,
291 UErrorCode *status)
292{
293 BreakIterator *bii = reinterpret_cast<BreakIterator *>(bi);
294 bii->refreshInputText(text, *status);
295}
296
297
298
b75a7d8f 299#endif /* #if !UCONFIG_NO_BREAK_ITERATION */