]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/ubrk.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / common / ubrk.cpp
CommitLineData
b75a7d8f
A
1/*
2*****************************************************************************************
73c04bcf 3* Copyright (C) 1996-2006, International Business Machines
b75a7d8f
A
4* Corporation and others. All Rights Reserved.
5*****************************************************************************************
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
24//----------------------------------------------------------------------------------------
25//
26// ubrk_open Create a canned type of break iterator based on type (word, line, etc.)
27// and locale.
28//
29//----------------------------------------------------------------------------------------
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
87//----------------------------------------------------------------------------------------
88//
89// ubrk_openRules open a break iterator from a set of break rules.
90// Invokes the rule builder.
91//
92//----------------------------------------------------------------------------------------
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);
107 result = RBBIRuleBuilder::createRuleBasedBreakIterator(ruleString, *parseErr, *status);
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,
126 void *stackBuffer,
127 int32_t *pBufferSize,
128 UErrorCode *status)
129{
130 if (status == NULL || U_FAILURE(*status)){
131 return 0;
132 }
133 if (!pBufferSize || !bi){
134 *status = U_ILLEGAL_ARGUMENT_ERROR;
135 return 0;
136 }
73c04bcf
A
137 // Clear any incoming Safe Clone Allocated warning.
138 // Propagating this through to our return would really
139 // confuse our caller.
140 if (*status==U_SAFECLONE_ALLOCATED_WARNING) {
141 *status = U_ZERO_ERROR;
142 }
b75a7d8f
A
143 return (UBreakIterator *)(((BreakIterator*)bi)->
144 createBufferClone(stackBuffer, *pBufferSize, *status));
145}
146
147
148
149U_CAPI void U_EXPORT2
150ubrk_close(UBreakIterator *bi)
151{
152 BreakIterator *ubi = (BreakIterator*) bi;
153 if (ubi) {
154 if (ubi->isBufferClone()) {
155 ubi->~BreakIterator();
156 *(uint32_t *)ubi = 0xdeadbeef;
157 } else {
158 delete ubi;
159 }
160 }
161}
162
163U_CAPI void U_EXPORT2
164ubrk_setText(UBreakIterator* bi,
165 const UChar* text,
166 int32_t textLength,
167 UErrorCode* status)
168{
73c04bcf
A
169 BreakIterator *brit = (BreakIterator *)bi;
170 UText ut = UTEXT_INITIALIZER;
171 utext_openUChars(&ut, text, textLength, status);
172 brit->setText(&ut, *status);
173 // A stack allocated UText wrapping a UCHar * string
174 // can be dumped without explicitly closing it.
175}
b75a7d8f 176
b75a7d8f 177
b75a7d8f 178
73c04bcf
A
179U_DRAFT void U_EXPORT2
180ubrk_setUText(UBreakIterator *bi,
181 UText *text,
182 UErrorCode *status)
183{
184 RuleBasedBreakIterator *brit = (RuleBasedBreakIterator *)bi;
185 brit->RuleBasedBreakIterator::setText(text, *status);
b75a7d8f
A
186}
187
73c04bcf
A
188
189
190
191
b75a7d8f
A
192U_CAPI int32_t U_EXPORT2
193ubrk_current(const UBreakIterator *bi)
194{
195
73c04bcf 196 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::current();
b75a7d8f
A
197}
198
199U_CAPI int32_t U_EXPORT2
200ubrk_next(UBreakIterator *bi)
201{
202
73c04bcf 203 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::next();
b75a7d8f
A
204}
205
206U_CAPI int32_t U_EXPORT2
207ubrk_previous(UBreakIterator *bi)
208{
209
73c04bcf 210 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::previous();
b75a7d8f
A
211}
212
213U_CAPI int32_t U_EXPORT2
214ubrk_first(UBreakIterator *bi)
215{
216
73c04bcf 217 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::first();
b75a7d8f
A
218}
219
220U_CAPI int32_t U_EXPORT2
221ubrk_last(UBreakIterator *bi)
222{
223
73c04bcf 224 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::last();
b75a7d8f
A
225}
226
227U_CAPI int32_t U_EXPORT2
228ubrk_preceding(UBreakIterator *bi,
229 int32_t offset)
230{
231
73c04bcf 232 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::preceding(offset);
b75a7d8f
A
233}
234
235U_CAPI int32_t U_EXPORT2
236ubrk_following(UBreakIterator *bi,
237 int32_t offset)
238{
239
73c04bcf 240 return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::following(offset);
b75a7d8f
A
241}
242
243U_CAPI const char* U_EXPORT2
244ubrk_getAvailable(int32_t index)
245{
246
247 return uloc_getAvailable(index);
248}
249
250U_CAPI int32_t U_EXPORT2
251ubrk_countAvailable()
252{
253
254 return uloc_countAvailable();
255}
256
257
258U_CAPI UBool U_EXPORT2
259ubrk_isBoundary(UBreakIterator *bi, int32_t offset)
260{
73c04bcf 261 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::isBoundary(offset);
b75a7d8f
A
262}
263
264
265U_CAPI int32_t U_EXPORT2
266ubrk_getRuleStatus(UBreakIterator *bi)
267{
73c04bcf 268 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatus();
b75a7d8f
A
269}
270
374ca955
A
271U_CAPI int32_t U_EXPORT2
272ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status)
273{
73c04bcf 274 return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatusVec(fillInVec, capacity, *status);
374ca955
A
275}
276
277
278U_CAPI const char* U_EXPORT2
73c04bcf
A
279ubrk_getLocaleByType(const UBreakIterator *bi,
280 ULocDataLocaleType type,
374ca955
A
281 UErrorCode* status)
282{
283 if (bi == NULL) {
284 if (U_SUCCESS(*status)) {
285 *status = U_ILLEGAL_ARGUMENT_ERROR;
286 }
287 return NULL;
288 }
289 return ((BreakIterator*)bi)->getLocaleID(type, *status);
290}
291
292
b75a7d8f 293#endif /* #if !UCONFIG_NO_BREAK_ITERATION */