michael@0: /* michael@0: ******************************************************************************** michael@0: * Copyright (C) 1996-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************** michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_BREAK_ITERATION michael@0: michael@0: #include "unicode/ubrk.h" michael@0: michael@0: #include "unicode/brkiter.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/uchriter.h" michael@0: #include "unicode/rbbi.h" michael@0: #include "rbbirb.h" michael@0: #include "uassert.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // ubrk_open Create a canned type of break iterator based on type (word, line, etc.) michael@0: // and locale. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: U_CAPI UBreakIterator* U_EXPORT2 michael@0: ubrk_open(UBreakIteratorType type, michael@0: const char *locale, michael@0: const UChar *text, michael@0: int32_t textLength, michael@0: UErrorCode *status) michael@0: { michael@0: michael@0: if(U_FAILURE(*status)) return 0; michael@0: michael@0: BreakIterator *result = 0; michael@0: michael@0: switch(type) { michael@0: michael@0: case UBRK_CHARACTER: michael@0: result = BreakIterator::createCharacterInstance(Locale(locale), *status); michael@0: break; michael@0: michael@0: case UBRK_WORD: michael@0: result = BreakIterator::createWordInstance(Locale(locale), *status); michael@0: break; michael@0: michael@0: case UBRK_LINE: michael@0: result = BreakIterator::createLineInstance(Locale(locale), *status); michael@0: break; michael@0: michael@0: case UBRK_SENTENCE: michael@0: result = BreakIterator::createSentenceInstance(Locale(locale), *status); michael@0: break; michael@0: michael@0: case UBRK_TITLE: michael@0: result = BreakIterator::createTitleInstance(Locale(locale), *status); michael@0: break; michael@0: michael@0: default: michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: michael@0: // check for allocation error michael@0: if (U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: if(result == 0) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: UBreakIterator *uBI = (UBreakIterator *)result; michael@0: if (text != NULL) { michael@0: ubrk_setText(uBI, text, textLength, status); michael@0: } michael@0: return uBI; michael@0: } michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------ michael@0: // michael@0: // ubrk_openRules open a break iterator from a set of break rules. michael@0: // Invokes the rule builder. michael@0: // michael@0: //------------------------------------------------------------------------------ michael@0: U_CAPI UBreakIterator* U_EXPORT2 michael@0: ubrk_openRules( const UChar *rules, michael@0: int32_t rulesLength, michael@0: const UChar *text, michael@0: int32_t textLength, michael@0: UParseError *parseErr, michael@0: UErrorCode *status) { michael@0: michael@0: if (status == NULL || U_FAILURE(*status)){ michael@0: return 0; michael@0: } michael@0: michael@0: BreakIterator *result = 0; michael@0: UnicodeString ruleString(rules, rulesLength); michael@0: result = RBBIRuleBuilder::createRuleBasedBreakIterator(ruleString, parseErr, *status); michael@0: if(U_FAILURE(*status)) { michael@0: return 0; michael@0: } michael@0: michael@0: UBreakIterator *uBI = (UBreakIterator *)result; michael@0: if (text != NULL) { michael@0: ubrk_setText(uBI, text, textLength, status); michael@0: } michael@0: return uBI; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: U_CAPI UBreakIterator * U_EXPORT2 michael@0: ubrk_safeClone( michael@0: const UBreakIterator *bi, michael@0: void * /*stackBuffer*/, michael@0: int32_t *pBufferSize, michael@0: UErrorCode *status) michael@0: { michael@0: if (status == NULL || U_FAILURE(*status)){ michael@0: return NULL; michael@0: } michael@0: if (bi == NULL) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: return NULL; michael@0: } michael@0: if (pBufferSize != NULL) { michael@0: int32_t inputSize = *pBufferSize; michael@0: *pBufferSize = 1; michael@0: if (inputSize == 0) { michael@0: return NULL; // preflighting for deprecated functionality michael@0: } michael@0: } michael@0: BreakIterator *newBI = ((BreakIterator *)bi)->clone(); michael@0: if (newBI == NULL) { michael@0: *status = U_MEMORY_ALLOCATION_ERROR; michael@0: } else { michael@0: *status = U_SAFECLONE_ALLOCATED_WARNING; michael@0: } michael@0: return (UBreakIterator *)newBI; michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ubrk_close(UBreakIterator *bi) michael@0: { michael@0: delete (BreakIterator *)bi; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ubrk_setText(UBreakIterator* bi, michael@0: const UChar* text, michael@0: int32_t textLength, michael@0: UErrorCode* status) michael@0: { michael@0: BreakIterator *brit = (BreakIterator *)bi; michael@0: UText ut = UTEXT_INITIALIZER; michael@0: utext_openUChars(&ut, text, textLength, status); michael@0: brit->setText(&ut, *status); michael@0: // A stack allocated UText wrapping a UChar * string michael@0: // can be dumped without explicitly closing it. michael@0: } michael@0: michael@0: michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: ubrk_setUText(UBreakIterator *bi, michael@0: UText *text, michael@0: UErrorCode *status) michael@0: { michael@0: RuleBasedBreakIterator *brit = (RuleBasedBreakIterator *)bi; michael@0: brit->RuleBasedBreakIterator::setText(text, *status); michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_current(const UBreakIterator *bi) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::current(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_next(UBreakIterator *bi) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::next(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_previous(UBreakIterator *bi) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::previous(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_first(UBreakIterator *bi) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::first(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_last(UBreakIterator *bi) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::last(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_preceding(UBreakIterator *bi, michael@0: int32_t offset) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::preceding(offset); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_following(UBreakIterator *bi, michael@0: int32_t offset) michael@0: { michael@0: michael@0: return ((RuleBasedBreakIterator*)bi)->RuleBasedBreakIterator::following(offset); michael@0: } michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ubrk_getAvailable(int32_t index) michael@0: { michael@0: michael@0: return uloc_getAvailable(index); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_countAvailable() michael@0: { michael@0: michael@0: return uloc_countAvailable(); michael@0: } michael@0: michael@0: michael@0: U_CAPI UBool U_EXPORT2 michael@0: ubrk_isBoundary(UBreakIterator *bi, int32_t offset) michael@0: { michael@0: return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::isBoundary(offset); michael@0: } michael@0: michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_getRuleStatus(UBreakIterator *bi) michael@0: { michael@0: return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatus(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ubrk_getRuleStatusVec(UBreakIterator *bi, int32_t *fillInVec, int32_t capacity, UErrorCode *status) michael@0: { michael@0: return ((RuleBasedBreakIterator *)bi)->RuleBasedBreakIterator::getRuleStatusVec(fillInVec, capacity, *status); michael@0: } michael@0: michael@0: michael@0: U_CAPI const char* U_EXPORT2 michael@0: ubrk_getLocaleByType(const UBreakIterator *bi, michael@0: ULocDataLocaleType type, michael@0: UErrorCode* status) michael@0: { michael@0: if (bi == NULL) { michael@0: if (U_SUCCESS(*status)) { michael@0: *status = U_ILLEGAL_ARGUMENT_ERROR; michael@0: } michael@0: return NULL; michael@0: } michael@0: return ((BreakIterator*)bi)->getLocaleID(type, *status); michael@0: } michael@0: michael@0: michael@0: void ubrk_refreshUText(UBreakIterator *bi, michael@0: UText *text, michael@0: UErrorCode *status) michael@0: { michael@0: BreakIterator *bii = reinterpret_cast(bi); michael@0: bii->refreshInputText(text, *status); michael@0: } michael@0: michael@0: michael@0: michael@0: #endif /* #if !UCONFIG_NO_BREAK_ITERATION */