1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/unistr_titlecase_brkiter.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 2011, International Business Machines 1.7 +* Corporation and others. All Rights Reserved. 1.8 +******************************************************************************* 1.9 +* file name: unistr_titlecase_brkiter.cpp 1.10 +* encoding: US-ASCII 1.11 +* tab size: 8 (not used) 1.12 +* indentation:2 1.13 +* 1.14 +* created on: 2011may30 1.15 +* created by: Markus W. Scherer 1.16 +* 1.17 +* Titlecasing functions that are based on BreakIterator 1.18 +* were moved here to break dependency cycles among parts of the common library. 1.19 +*/ 1.20 + 1.21 +#include "unicode/utypes.h" 1.22 + 1.23 +#if !UCONFIG_NO_BREAK_ITERATION 1.24 + 1.25 +#include "unicode/brkiter.h" 1.26 +#include "unicode/ubrk.h" 1.27 +#include "unicode/unistr.h" 1.28 +#include "unicode/ustring.h" 1.29 +#include "cmemory.h" 1.30 +#include "ustr_imp.h" 1.31 + 1.32 +static int32_t U_CALLCONV 1.33 +unistr_case_internalToTitle(const UCaseMap *csm, 1.34 + UChar *dest, int32_t destCapacity, 1.35 + const UChar *src, int32_t srcLength, 1.36 + UErrorCode *pErrorCode) { 1.37 + ubrk_setText(csm->iter, src, srcLength, pErrorCode); 1.38 + return ustrcase_internalToTitle(csm, dest, destCapacity, src, srcLength, pErrorCode); 1.39 +} 1.40 + 1.41 +/* 1.42 + * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. 1.43 + * Do this fast because it is called with every function call. 1.44 + */ 1.45 +static inline void 1.46 +setTempCaseMap(UCaseMap *csm, const char *locale) { 1.47 + if(csm->csp==NULL) { 1.48 + csm->csp=ucase_getSingleton(); 1.49 + } 1.50 + if(locale!=NULL && locale[0]==0) { 1.51 + csm->locale[0]=0; 1.52 + } else { 1.53 + ustrcase_setTempCaseMapLocale(csm, locale); 1.54 + } 1.55 +} 1.56 + 1.57 +U_NAMESPACE_BEGIN 1.58 + 1.59 +UnicodeString & 1.60 +UnicodeString::toTitle(BreakIterator *titleIter) { 1.61 + return toTitle(titleIter, Locale::getDefault(), 0); 1.62 +} 1.63 + 1.64 +UnicodeString & 1.65 +UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) { 1.66 + return toTitle(titleIter, locale, 0); 1.67 +} 1.68 + 1.69 +UnicodeString & 1.70 +UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options) { 1.71 + UCaseMap csm=UCASEMAP_INITIALIZER; 1.72 + csm.options=options; 1.73 + setTempCaseMap(&csm, locale.getName()); 1.74 + BreakIterator *bi=titleIter; 1.75 + if(bi==NULL) { 1.76 + UErrorCode errorCode=U_ZERO_ERROR; 1.77 + bi=BreakIterator::createWordInstance(locale, errorCode); 1.78 + if(U_FAILURE(errorCode)) { 1.79 + setToBogus(); 1.80 + return *this; 1.81 + } 1.82 + } 1.83 + csm.iter=reinterpret_cast<UBreakIterator *>(bi); 1.84 + caseMap(&csm, unistr_case_internalToTitle); 1.85 + if(titleIter==NULL) { 1.86 + delete bi; 1.87 + } 1.88 + return *this; 1.89 +} 1.90 + 1.91 +U_NAMESPACE_END 1.92 + 1.93 +#endif // !UCONFIG_NO_BREAK_ITERATION