X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/4388f060552cc537e71e957d32f35e9d75a61233..f3c0d7a59d99c2a94c6b8822291f0e42be3773c9:/icuSources/common/ustr_titlecase_brkiter.cpp diff --git a/icuSources/common/ustr_titlecase_brkiter.cpp b/icuSources/common/ustr_titlecase_brkiter.cpp index 4a2352ee..0b2ba020 100644 --- a/icuSources/common/ustr_titlecase_brkiter.cpp +++ b/icuSources/common/ustr_titlecase_brkiter.cpp @@ -1,10 +1,12 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * Copyright (C) 2011, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * file name: ustr_titlecase_brkiter.cpp -* encoding: US-ASCII +* encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * @@ -20,30 +22,17 @@ #if !UCONFIG_NO_BREAK_ITERATION #include "unicode/brkiter.h" +#include "unicode/casemap.h" +#include "unicode/localpointer.h" #include "unicode/ubrk.h" #include "unicode/ucasemap.h" #include "cmemory.h" #include "ucase.h" -#include "ustr_imp.h" +#include "ucasemap_imp.h" -/* functions available in the common library (for unistr_case.cpp) */ +U_NAMESPACE_USE -/* - * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. - * Do this fast because it is called with every function call. - * Duplicate of the same function in ustrcase.cpp, to keep it inline. - */ -static inline void -setTempCaseMap(UCaseMap *csm, const char *locale) { - if(csm->csp==NULL) { - csm->csp=ucase_getSingleton(); - } - if(locale!=NULL && locale[0]==0) { - csm->locale[0]=0; - } else { - ustrcase_setTempCaseMapLocale(csm, locale); - } -} +/* functions available in the common library (for unistr_case.cpp) */ /* public API functions */ @@ -53,39 +42,73 @@ u_strToTitle(UChar *dest, int32_t destCapacity, UBreakIterator *titleIter, const char *locale, UErrorCode *pErrorCode) { - UCaseMap csm=UCASEMAP_INITIALIZER; - setTempCaseMap(&csm, locale); + LocalPointer ownedIter; + BreakIterator *iter; if(titleIter!=NULL) { - ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode); + iter=reinterpret_cast(titleIter); } else { - csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode); + iter=BreakIterator::createWordInstance(Locale(locale), *pErrorCode); + ownedIter.adoptInstead(iter); + } + if(U_FAILURE(*pErrorCode)) { + return 0; } - int32_t length=ustrcase_map( - &csm, + UnicodeString s(srcLength<0, src, srcLength); + iter->setText(s); + return ustrcase_mapWithOverlap( + ustrcase_getCaseLocale(locale), 0, iter, dest, destCapacity, src, srcLength, - ustrcase_internalToTitle, pErrorCode); - if(titleIter==NULL && csm.iter!=NULL) { - ubrk_close(csm.iter); + ustrcase_internalToTitle, *pErrorCode); +} + +U_NAMESPACE_BEGIN + +int32_t CaseMap::toTitle( + const char *locale, uint32_t options, BreakIterator *iter, + const UChar *src, int32_t srcLength, + UChar *dest, int32_t destCapacity, Edits *edits, + UErrorCode &errorCode) { + LocalPointer ownedIter; + if(iter==NULL) { + iter=BreakIterator::createWordInstance(Locale(locale), errorCode); + ownedIter.adoptInstead(iter); + } + if(U_FAILURE(errorCode)) { + return 0; } - return length; + UnicodeString s(srcLength<0, src, srcLength); + iter->setText(s); + return ustrcase_map( + ustrcase_getCaseLocale(locale), options, iter, + dest, destCapacity, + src, srcLength, + ustrcase_internalToTitle, edits, errorCode); } +U_NAMESPACE_END + U_CAPI int32_t U_EXPORT2 ucasemap_toTitle(UCaseMap *csm, UChar *dest, int32_t destCapacity, const UChar *src, int32_t srcLength, UErrorCode *pErrorCode) { - if(csm->iter!=NULL) { - ubrk_setText(csm->iter, src, srcLength, pErrorCode); - } else { - csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode); + if (U_FAILURE(*pErrorCode)) { + return 0; + } + if (csm->iter == NULL) { + csm->iter = BreakIterator::createWordInstance(Locale(csm->locale), *pErrorCode); + } + if (U_FAILURE(*pErrorCode)) { + return 0; } + UnicodeString s(srcLength<0, src, srcLength); + csm->iter->setText(s); return ustrcase_map( - csm, + csm->caseLocale, csm->options, csm->iter, dest, destCapacity, src, srcLength, - ustrcase_internalToTitle, pErrorCode); + ustrcase_internalToTitle, NULL, *pErrorCode); } #endif // !UCONFIG_NO_BREAK_ITERATION