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