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