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: ustr_titlecase_brkiter.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 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/ucasemap.h" michael@0: #include "cmemory.h" michael@0: #include "ucase.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: /* functions available in the common library (for unistr_case.cpp) */ 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: * Duplicate of the same function in ustrcase.cpp, to keep it inline. 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: /* public API functions */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_strToTitle(UChar *dest, int32_t destCapacity, michael@0: const UChar *src, int32_t srcLength, michael@0: UBreakIterator *titleIter, michael@0: const char *locale, michael@0: UErrorCode *pErrorCode) { michael@0: UCaseMap csm=UCASEMAP_INITIALIZER; michael@0: setTempCaseMap(&csm, locale); michael@0: if(titleIter!=NULL) { michael@0: ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode); michael@0: } else { michael@0: csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode); michael@0: } michael@0: int32_t length=ustrcase_map( michael@0: &csm, michael@0: dest, destCapacity, michael@0: src, srcLength, michael@0: ustrcase_internalToTitle, pErrorCode); michael@0: if(titleIter==NULL && csm.iter!=NULL) { michael@0: ubrk_close(csm.iter); michael@0: } michael@0: return length; michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: ucasemap_toTitle(UCaseMap *csm, michael@0: UChar *dest, int32_t destCapacity, michael@0: const UChar *src, int32_t srcLength, michael@0: UErrorCode *pErrorCode) { michael@0: if(csm->iter!=NULL) { michael@0: ubrk_setText(csm->iter, src, srcLength, pErrorCode); michael@0: } else { michael@0: csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode); michael@0: } michael@0: return ustrcase_map( michael@0: csm, michael@0: dest, destCapacity, michael@0: src, srcLength, michael@0: ustrcase_internalToTitle, pErrorCode); michael@0: } michael@0: michael@0: #endif // !UCONFIG_NO_BREAK_ITERATION