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) 2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: unistr_titlecase_brkiter.cpp |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:2 |
michael@0 | 10 | * |
michael@0 | 11 | * created on: 2011may30 |
michael@0 | 12 | * created by: Markus W. Scherer |
michael@0 | 13 | * |
michael@0 | 14 | * Titlecasing functions that are based on BreakIterator |
michael@0 | 15 | * were moved here to break dependency cycles among parts of the common library. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #include "unicode/utypes.h" |
michael@0 | 19 | |
michael@0 | 20 | #if !UCONFIG_NO_BREAK_ITERATION |
michael@0 | 21 | |
michael@0 | 22 | #include "unicode/brkiter.h" |
michael@0 | 23 | #include "unicode/ubrk.h" |
michael@0 | 24 | #include "unicode/unistr.h" |
michael@0 | 25 | #include "unicode/ustring.h" |
michael@0 | 26 | #include "cmemory.h" |
michael@0 | 27 | #include "ustr_imp.h" |
michael@0 | 28 | |
michael@0 | 29 | static int32_t U_CALLCONV |
michael@0 | 30 | unistr_case_internalToTitle(const UCaseMap *csm, |
michael@0 | 31 | UChar *dest, int32_t destCapacity, |
michael@0 | 32 | const UChar *src, int32_t srcLength, |
michael@0 | 33 | UErrorCode *pErrorCode) { |
michael@0 | 34 | ubrk_setText(csm->iter, src, srcLength, pErrorCode); |
michael@0 | 35 | return ustrcase_internalToTitle(csm, dest, destCapacity, src, srcLength, pErrorCode); |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. |
michael@0 | 40 | * Do this fast because it is called with every function call. |
michael@0 | 41 | */ |
michael@0 | 42 | static inline void |
michael@0 | 43 | setTempCaseMap(UCaseMap *csm, const char *locale) { |
michael@0 | 44 | if(csm->csp==NULL) { |
michael@0 | 45 | csm->csp=ucase_getSingleton(); |
michael@0 | 46 | } |
michael@0 | 47 | if(locale!=NULL && locale[0]==0) { |
michael@0 | 48 | csm->locale[0]=0; |
michael@0 | 49 | } else { |
michael@0 | 50 | ustrcase_setTempCaseMapLocale(csm, locale); |
michael@0 | 51 | } |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | U_NAMESPACE_BEGIN |
michael@0 | 55 | |
michael@0 | 56 | UnicodeString & |
michael@0 | 57 | UnicodeString::toTitle(BreakIterator *titleIter) { |
michael@0 | 58 | return toTitle(titleIter, Locale::getDefault(), 0); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | UnicodeString & |
michael@0 | 62 | UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) { |
michael@0 | 63 | return toTitle(titleIter, locale, 0); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | UnicodeString & |
michael@0 | 67 | UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options) { |
michael@0 | 68 | UCaseMap csm=UCASEMAP_INITIALIZER; |
michael@0 | 69 | csm.options=options; |
michael@0 | 70 | setTempCaseMap(&csm, locale.getName()); |
michael@0 | 71 | BreakIterator *bi=titleIter; |
michael@0 | 72 | if(bi==NULL) { |
michael@0 | 73 | UErrorCode errorCode=U_ZERO_ERROR; |
michael@0 | 74 | bi=BreakIterator::createWordInstance(locale, errorCode); |
michael@0 | 75 | if(U_FAILURE(errorCode)) { |
michael@0 | 76 | setToBogus(); |
michael@0 | 77 | return *this; |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | csm.iter=reinterpret_cast<UBreakIterator *>(bi); |
michael@0 | 81 | caseMap(&csm, unistr_case_internalToTitle); |
michael@0 | 82 | if(titleIter==NULL) { |
michael@0 | 83 | delete bi; |
michael@0 | 84 | } |
michael@0 | 85 | return *this; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | U_NAMESPACE_END |
michael@0 | 89 | |
michael@0 | 90 | #endif // !UCONFIG_NO_BREAK_ITERATION |