2 ********************************************************************************
3 * Copyright (C) 1996-2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_BREAK_ITERATION
12 #include "unicode/ubrk.h"
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"
24 //------------------------------------------------------------------------------
26 // ubrk_open Create a canned type of break iterator based on type (word, line, etc.)
29 //------------------------------------------------------------------------------
30 U_CAPI UBreakIterator
* U_EXPORT2
31 ubrk_open(UBreakIteratorType type
,
38 if(U_FAILURE(*status
)) return 0;
40 BreakIterator
*result
= 0;
45 result
= BreakIterator::createCharacterInstance(Locale(locale
), *status
);
49 result
= BreakIterator::createWordInstance(Locale(locale
), *status
);
53 result
= BreakIterator::createLineInstance(Locale(locale
), *status
);
57 result
= BreakIterator::createSentenceInstance(Locale(locale
), *status
);
61 result
= BreakIterator::createTitleInstance(Locale(locale
), *status
);
65 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
68 // check for allocation error
69 if (U_FAILURE(*status
)) {
73 *status
= U_MEMORY_ALLOCATION_ERROR
;
78 UBreakIterator
*uBI
= (UBreakIterator
*)result
;
80 ubrk_setText(uBI
, text
, textLength
, status
);
87 //------------------------------------------------------------------------------
89 // ubrk_openRules open a break iterator from a set of break rules.
90 // Invokes the rule builder.
92 //------------------------------------------------------------------------------
93 U_CAPI UBreakIterator
* U_EXPORT2
94 ubrk_openRules( const UChar
*rules
,
98 UParseError
*parseErr
,
101 if (status
== NULL
|| U_FAILURE(*status
)){
105 BreakIterator
*result
= 0;
106 UnicodeString
ruleString(rules
, rulesLength
);
107 result
= RBBIRuleBuilder::createRuleBasedBreakIterator(ruleString
, parseErr
, *status
);
108 if(U_FAILURE(*status
)) {
112 UBreakIterator
*uBI
= (UBreakIterator
*)result
;
114 ubrk_setText(uBI
, text
, textLength
, status
);
123 U_CAPI UBreakIterator
* U_EXPORT2
125 const UBreakIterator
*bi
,
126 void * /*stackBuffer*/,
127 int32_t *pBufferSize
,
130 if (status
== NULL
|| U_FAILURE(*status
)){
134 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
137 if (pBufferSize
!= NULL
) {
138 int32_t inputSize
= *pBufferSize
;
140 if (inputSize
== 0) {
141 return NULL
; // preflighting for deprecated functionality
144 BreakIterator
*newBI
= ((BreakIterator
*)bi
)->clone();
146 *status
= U_MEMORY_ALLOCATION_ERROR
;
148 *status
= U_SAFECLONE_ALLOCATED_WARNING
;
150 return (UBreakIterator
*)newBI
;
155 U_CAPI
void U_EXPORT2
156 ubrk_close(UBreakIterator
*bi
)
158 delete (BreakIterator
*)bi
;
161 U_CAPI
void U_EXPORT2
162 ubrk_setText(UBreakIterator
* bi
,
167 UText ut
= UTEXT_INITIALIZER
;
168 utext_openUChars(&ut
, text
, textLength
, status
);
169 ((BreakIterator
*)bi
)->setText(&ut
, *status
);
170 // A stack allocated UText wrapping a UChar * string
171 // can be dumped without explicitly closing it.
176 U_CAPI
void U_EXPORT2
177 ubrk_setUText(UBreakIterator
*bi
,
181 ((BreakIterator
*)bi
)->setText(text
, *status
);
188 U_CAPI
int32_t U_EXPORT2
189 ubrk_current(const UBreakIterator
*bi
)
192 return ((BreakIterator
*)bi
)->current();
195 U_CAPI
int32_t U_EXPORT2
196 ubrk_next(UBreakIterator
*bi
)
199 return ((BreakIterator
*)bi
)->next();
202 U_CAPI
int32_t U_EXPORT2
203 ubrk_previous(UBreakIterator
*bi
)
206 return ((BreakIterator
*)bi
)->previous();
209 U_CAPI
int32_t U_EXPORT2
210 ubrk_first(UBreakIterator
*bi
)
213 return ((BreakIterator
*)bi
)->first();
216 U_CAPI
int32_t U_EXPORT2
217 ubrk_last(UBreakIterator
*bi
)
220 return ((BreakIterator
*)bi
)->last();
223 U_CAPI
int32_t U_EXPORT2
224 ubrk_preceding(UBreakIterator
*bi
,
228 return ((BreakIterator
*)bi
)->preceding(offset
);
231 U_CAPI
int32_t U_EXPORT2
232 ubrk_following(UBreakIterator
*bi
,
236 return ((BreakIterator
*)bi
)->following(offset
);
239 U_CAPI
const char* U_EXPORT2
240 ubrk_getAvailable(int32_t index
)
243 return uloc_getAvailable(index
);
246 U_CAPI
int32_t U_EXPORT2
247 ubrk_countAvailable()
250 return uloc_countAvailable();
254 U_CAPI UBool U_EXPORT2
255 ubrk_isBoundary(UBreakIterator
*bi
, int32_t offset
)
257 return ((BreakIterator
*)bi
)->isBoundary(offset
);
261 U_CAPI
int32_t U_EXPORT2
262 ubrk_getRuleStatus(UBreakIterator
*bi
)
264 return ((BreakIterator
*)bi
)->getRuleStatus();
267 U_CAPI
int32_t U_EXPORT2
268 ubrk_getRuleStatusVec(UBreakIterator
*bi
, int32_t *fillInVec
, int32_t capacity
, UErrorCode
*status
)
270 return ((BreakIterator
*)bi
)->getRuleStatusVec(fillInVec
, capacity
, *status
);
274 U_CAPI
const char* U_EXPORT2
275 ubrk_getLocaleByType(const UBreakIterator
*bi
,
276 ULocDataLocaleType type
,
280 if (U_SUCCESS(*status
)) {
281 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
285 return ((BreakIterator
*)bi
)->getLocaleID(type
, *status
);
289 void ubrk_refreshUText(UBreakIterator
*bi
,
293 BreakIterator
*bii
= reinterpret_cast<BreakIterator
*>(bi
);
294 bii
->refreshInputText(text
, *status
);
299 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */