2 ********************************************************************************
3 * Copyright (C) 1996-2008, 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
,
127 int32_t *pBufferSize
,
130 if (status
== NULL
|| U_FAILURE(*status
)){
133 if (!pBufferSize
|| !bi
){
134 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
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
;
143 return (UBreakIterator
*)(((BreakIterator
*)bi
)->
144 createBufferClone(stackBuffer
, *pBufferSize
, *status
));
149 U_CAPI
void U_EXPORT2
150 ubrk_close(UBreakIterator
*bi
)
152 BreakIterator
*ubi
= (BreakIterator
*) bi
;
154 if (ubi
->isBufferClone()) {
155 ubi
->~BreakIterator();
156 *(uint32_t *)ubi
= 0xdeadbeef;
163 U_CAPI
void U_EXPORT2
164 ubrk_setText(UBreakIterator
* bi
,
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.
179 U_CAPI
void U_EXPORT2
180 ubrk_setUText(UBreakIterator
*bi
,
184 RuleBasedBreakIterator
*brit
= (RuleBasedBreakIterator
*)bi
;
185 brit
->RuleBasedBreakIterator::setText(text
, *status
);
192 U_CAPI
int32_t U_EXPORT2
193 ubrk_current(const UBreakIterator
*bi
)
196 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::current();
199 U_CAPI
int32_t U_EXPORT2
200 ubrk_next(UBreakIterator
*bi
)
203 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::next();
206 U_CAPI
int32_t U_EXPORT2
207 ubrk_previous(UBreakIterator
*bi
)
210 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::previous();
213 U_CAPI
int32_t U_EXPORT2
214 ubrk_first(UBreakIterator
*bi
)
217 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::first();
220 U_CAPI
int32_t U_EXPORT2
221 ubrk_last(UBreakIterator
*bi
)
224 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::last();
227 U_CAPI
int32_t U_EXPORT2
228 ubrk_preceding(UBreakIterator
*bi
,
232 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::preceding(offset
);
235 U_CAPI
int32_t U_EXPORT2
236 ubrk_following(UBreakIterator
*bi
,
240 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::following(offset
);
243 U_CAPI
const char* U_EXPORT2
244 ubrk_getAvailable(int32_t index
)
247 return uloc_getAvailable(index
);
250 U_CAPI
int32_t U_EXPORT2
251 ubrk_countAvailable()
254 return uloc_countAvailable();
258 U_CAPI UBool U_EXPORT2
259 ubrk_isBoundary(UBreakIterator
*bi
, int32_t offset
)
261 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::isBoundary(offset
);
265 U_CAPI
int32_t U_EXPORT2
266 ubrk_getRuleStatus(UBreakIterator
*bi
)
268 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::getRuleStatus();
271 U_CAPI
int32_t U_EXPORT2
272 ubrk_getRuleStatusVec(UBreakIterator
*bi
, int32_t *fillInVec
, int32_t capacity
, UErrorCode
*status
)
274 return ((RuleBasedBreakIterator
*)bi
)->RuleBasedBreakIterator::getRuleStatusVec(fillInVec
, capacity
, *status
);
278 U_CAPI
const char* U_EXPORT2
279 ubrk_getLocaleByType(const UBreakIterator
*bi
,
280 ULocDataLocaleType type
,
284 if (U_SUCCESS(*status
)) {
285 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
289 return ((BreakIterator
*)bi
)->getLocaleID(type
, *status
);
293 #endif /* #if !UCONFIG_NO_BREAK_ITERATION */