intl/icu/source/common/ustr_titlecase_brkiter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ustr_titlecase_brkiter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     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:  ustr_titlecase_brkiter.cpp
    1.10 +*   encoding:   US-ASCII
    1.11 +*   tab size:   8 (not used)
    1.12 +*   indentation:4
    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/ucasemap.h"
    1.28 +#include "cmemory.h"
    1.29 +#include "ucase.h"
    1.30 +#include "ustr_imp.h"
    1.31 +
    1.32 +/* functions available in the common library (for unistr_case.cpp) */
    1.33 +
    1.34 +/*
    1.35 + * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
    1.36 + * Do this fast because it is called with every function call.
    1.37 + * Duplicate of the same function in ustrcase.cpp, to keep it inline.
    1.38 + */
    1.39 +static inline void
    1.40 +setTempCaseMap(UCaseMap *csm, const char *locale) {
    1.41 +    if(csm->csp==NULL) {
    1.42 +        csm->csp=ucase_getSingleton();
    1.43 +    }
    1.44 +    if(locale!=NULL && locale[0]==0) {
    1.45 +        csm->locale[0]=0;
    1.46 +    } else {
    1.47 +        ustrcase_setTempCaseMapLocale(csm, locale);
    1.48 +    }
    1.49 +}
    1.50 +
    1.51 +/* public API functions */
    1.52 +
    1.53 +U_CAPI int32_t U_EXPORT2
    1.54 +u_strToTitle(UChar *dest, int32_t destCapacity,
    1.55 +             const UChar *src, int32_t srcLength,
    1.56 +             UBreakIterator *titleIter,
    1.57 +             const char *locale,
    1.58 +             UErrorCode *pErrorCode) {
    1.59 +    UCaseMap csm=UCASEMAP_INITIALIZER;
    1.60 +    setTempCaseMap(&csm, locale);
    1.61 +    if(titleIter!=NULL) {
    1.62 +        ubrk_setText(csm.iter=titleIter, src, srcLength, pErrorCode);
    1.63 +    } else {
    1.64 +        csm.iter=ubrk_open(UBRK_WORD, csm.locale, src, srcLength, pErrorCode);
    1.65 +    }
    1.66 +    int32_t length=ustrcase_map(
    1.67 +        &csm,
    1.68 +        dest, destCapacity,
    1.69 +        src, srcLength,
    1.70 +        ustrcase_internalToTitle, pErrorCode);
    1.71 +    if(titleIter==NULL && csm.iter!=NULL) {
    1.72 +        ubrk_close(csm.iter);
    1.73 +    }
    1.74 +    return length;
    1.75 +}
    1.76 +
    1.77 +U_CAPI int32_t U_EXPORT2
    1.78 +ucasemap_toTitle(UCaseMap *csm,
    1.79 +                 UChar *dest, int32_t destCapacity,
    1.80 +                 const UChar *src, int32_t srcLength,
    1.81 +                 UErrorCode *pErrorCode) {
    1.82 +    if(csm->iter!=NULL) {
    1.83 +        ubrk_setText(csm->iter, src, srcLength, pErrorCode);
    1.84 +    } else {
    1.85 +        csm->iter=ubrk_open(UBRK_WORD, csm->locale, src, srcLength, pErrorCode);
    1.86 +    }
    1.87 +    return ustrcase_map(
    1.88 +        csm,
    1.89 +        dest, destCapacity,
    1.90 +        src, srcLength,
    1.91 +        ustrcase_internalToTitle, pErrorCode);
    1.92 +}
    1.93 +
    1.94 +#endif  // !UCONFIG_NO_BREAK_ITERATION

mercurial