Home | History | Annotate | Download | only in common
      1 /*
      2 *******************************************************************************
      3 *   Copyright (C) 2011, International Business Machines
      4 *   Corporation and others.  All Rights Reserved.
      5 *******************************************************************************
      6 *   file name:  ustr_titlecase_brkiter.cpp
      7 *   encoding:   US-ASCII
      8 *   tab size:   8 (not used)
      9 *   indentation:4
     10 *
     11 *   created on: 2011may30
     12 *   created by: Markus W. Scherer
     13 *
     14 *   Titlecasing functions that are based on BreakIterator
     15 *   were moved here to break dependency cycles among parts of the common library.
     16 */
     17 
     18 #include "unicode/utypes.h"
     19 
     20 #if !UCONFIG_NO_BREAK_ITERATION
     21 
     22 #include "unicode/brkiter.h"
     23 #include "unicode/ubrk.h"
     24 #include "unicode/ucasemap.h"
     25 #include "cmemory.h"
     26 #include "ucase.h"
     27 #include "ustr_imp.h"
     28 
     29 /* functions available in the common library (for unistr_case.cpp) */
     30 
     31 /*
     32  * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
     33  * Do this fast because it is called with every function call.
     34  * Duplicate of the same function in ustrcase.cpp, to keep it inline.
     35  */
     36 static inline void
     37 setTempCaseMap(UCaseMap *csm, const char *locale) {
     38     if(csm->csp==NULL) {
     39         csm->csp=ucase_getSingleton();
     40     }
     41     if(locale!=NULL && locale[0]==0) {
     42         csm->locale[0]=0;
     43     } else {
     44         ustrcase_setTempCaseMapLocale(csm, locale);
     45     }
     46 }
     47 
     48 /* public API functions */
     49 
     50 U_CAPI int32_t U_EXPORT2
     51 u_strToTitle(UChar *dest, int32_t destCapacity,
     52              const UChar *src, int32_t srcLength,
     53              UBreakIterator *titleIter,
     54              const char *locale,
     55              UErrorCode *pErrorCode) {
     56     UCaseMap csm=UCASEMAP_INITIALIZER;
     57     setTempCaseMap(&csm, locale);
     58     if(titleIter!=NULL) {
     59         ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode);
     60     } else {
     61         csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode);
     62     }
     63     int32_t length=ustrcase_map(
     64         &csm,
     65         dest, destCapacity,
     66         src, srcLength,
     67         ustrcase_internalToTitle, pErrorCode);
     68     if(titleIter==NULL && csm.iter!=NULL) {
     69         ubrk_close(csm.iter);
     70     }
     71     return length;
     72 }
     73 
     74 U_CAPI int32_t U_EXPORT2
     75 ucasemap_toTitle(UCaseMap *csm,
     76                  UChar *dest, int32_t destCapacity,
     77                  const UChar *src, int32_t srcLength,
     78                  UErrorCode *pErrorCode) {
     79     if(csm->iter!=NULL) {
     80         ubrk_setText(csm->iter, src, srcLength, pErrorCode);
     81     } else {
     82         csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode);
     83     }
     84     return ustrcase_map(
     85         csm,
     86         dest, destCapacity,
     87         src, srcLength,
     88         ustrcase_internalToTitle, pErrorCode);
     89 }
     90 
     91 #endif  // !UCONFIG_NO_BREAK_ITERATION
     92