michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: unistr_titlecase_brkiter.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:2 michael@0: * michael@0: * created on: 2011may30 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Titlecasing functions that are based on BreakIterator michael@0: * were moved here to break dependency cycles among parts of the common library. 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/brkiter.h" michael@0: #include "unicode/ubrk.h" michael@0: #include "unicode/unistr.h" michael@0: #include "unicode/ustring.h" michael@0: #include "cmemory.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: static int32_t U_CALLCONV michael@0: unistr_case_internalToTitle(const UCaseMap *csm, michael@0: UChar *dest, int32_t destCapacity, michael@0: const UChar *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: ubrk_setText(csm->iter, src, srcLength, pErrorCode); michael@0: return ustrcase_internalToTitle(csm, dest, destCapacity, src, srcLength, pErrorCode); michael@0: } michael@0: michael@0: /* michael@0: * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. michael@0: * Do this fast because it is called with every function call. michael@0: */ michael@0: static inline void michael@0: setTempCaseMap(UCaseMap *csm, const char *locale) { michael@0: if(csm->csp==NULL) { michael@0: csm->csp=ucase_getSingleton(); michael@0: } michael@0: if(locale!=NULL && locale[0]==0) { michael@0: csm->locale[0]=0; michael@0: } else { michael@0: ustrcase_setTempCaseMapLocale(csm, locale); michael@0: } michael@0: } michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: UnicodeString & michael@0: UnicodeString::toTitle(BreakIterator *titleIter) { michael@0: return toTitle(titleIter, Locale::getDefault(), 0); michael@0: } michael@0: michael@0: UnicodeString & michael@0: UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale) { michael@0: return toTitle(titleIter, locale, 0); michael@0: } michael@0: michael@0: UnicodeString & michael@0: UnicodeString::toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options) { michael@0: UCaseMap csm=UCASEMAP_INITIALIZER; michael@0: csm.options=options; michael@0: setTempCaseMap(&csm, locale.getName()); michael@0: BreakIterator *bi=titleIter; michael@0: if(bi==NULL) { michael@0: UErrorCode errorCode=U_ZERO_ERROR; michael@0: bi=BreakIterator::createWordInstance(locale, errorCode); michael@0: if(U_FAILURE(errorCode)) { michael@0: setToBogus(); michael@0: return *this; michael@0: } michael@0: } michael@0: csm.iter=reinterpret_cast(bi); michael@0: caseMap(&csm, unistr_case_internalToTitle); michael@0: if(titleIter==NULL) { michael@0: delete bi; michael@0: } michael@0: return *this; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // !UCONFIG_NO_BREAK_ITERATION