intl/icu/source/common/ustrcase_locale.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ustrcase_locale.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     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:  ustrcase_locale.cpp
    1.10 +*   encoding:   US-ASCII
    1.11 +*   tab size:   8 (not used)
    1.12 +*   indentation:4
    1.13 +*
    1.14 +*   created on: 2011may31
    1.15 +*   created by: Markus W. Scherer
    1.16 +*
    1.17 +*   Locale-sensitive case mapping functions (ones that call uloc_getDefault())
    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 +#include "unicode/ucasemap.h"
    1.23 +#include "unicode/uloc.h"
    1.24 +#include "unicode/ustring.h"
    1.25 +#include "ucase.h"
    1.26 +#include "ustr_imp.h"
    1.27 +
    1.28 +U_CFUNC void
    1.29 +ustrcase_setTempCaseMapLocale(UCaseMap *csm, const char *locale) {
    1.30 +    /*
    1.31 +     * We could call ucasemap_setLocale(), but here we really only care about
    1.32 +     * the initial language subtag, we need not return the real string via
    1.33 +     * ucasemap_getLocale(), and we don't care about only getting "x" from
    1.34 +     * "x-some-thing" etc.
    1.35 +     *
    1.36 +     * We ignore locales with a longer-than-3 initial subtag.
    1.37 +     *
    1.38 +     * We also do not fill in the locCache because it is rarely used,
    1.39 +     * and not worth setting unless we reuse it for many case mapping operations.
    1.40 +     * (That's why UCaseMap was created.)
    1.41 +     */
    1.42 +    int i;
    1.43 +    char c;
    1.44 +
    1.45 +    /* the internal functions require locale!=NULL */
    1.46 +    if(locale==NULL) {
    1.47 +        // Do not call uprv_getDefaultLocaleID() because that does not see
    1.48 +        // changes to the default locale via uloc_setDefault().
    1.49 +        // It would also be inefficient if used frequently because uprv_getDefaultLocaleID()
    1.50 +        // does not cache the locale ID.
    1.51 +        //
    1.52 +        // Unfortunately, uloc_getDefault() has many dependencies.
    1.53 +        // We only care about a small set of language subtags,
    1.54 +        // and we do not need the locale ID to be canonicalized.
    1.55 +        //
    1.56 +        // Best is to not call case mapping functions with a NULL locale ID.
    1.57 +        locale=uloc_getDefault();
    1.58 +    }
    1.59 +    for(i=0; i<4 && (c=locale[i])!=0 && c!='-' && c!='_'; ++i) {
    1.60 +        csm->locale[i]=c;
    1.61 +    }
    1.62 +    if(i<=3) {
    1.63 +        csm->locale[i]=0;  /* Up to 3 non-separator characters. */
    1.64 +    } else {
    1.65 +        csm->locale[0]=0;  /* Longer-than-3 initial subtag: Ignore. */
    1.66 +    }
    1.67 +}
    1.68 +
    1.69 +/*
    1.70 + * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
    1.71 + * Do this fast because it is called with every function call.
    1.72 + */
    1.73 +static inline void
    1.74 +setTempCaseMap(UCaseMap *csm, const char *locale) {
    1.75 +    if(csm->csp==NULL) {
    1.76 +        csm->csp=ucase_getSingleton();
    1.77 +    }
    1.78 +    if(locale!=NULL && locale[0]==0) {
    1.79 +        csm->locale[0]=0;
    1.80 +    } else {
    1.81 +        ustrcase_setTempCaseMapLocale(csm, locale);
    1.82 +    }
    1.83 +}
    1.84 +
    1.85 +/* public API functions */
    1.86 +
    1.87 +U_CAPI int32_t U_EXPORT2
    1.88 +u_strToLower(UChar *dest, int32_t destCapacity,
    1.89 +             const UChar *src, int32_t srcLength,
    1.90 +             const char *locale,
    1.91 +             UErrorCode *pErrorCode) {
    1.92 +    UCaseMap csm=UCASEMAP_INITIALIZER;
    1.93 +    setTempCaseMap(&csm, locale);
    1.94 +    return ustrcase_map(
    1.95 +        &csm,
    1.96 +        dest, destCapacity,
    1.97 +        src, srcLength,
    1.98 +        ustrcase_internalToLower, pErrorCode);
    1.99 +}
   1.100 +
   1.101 +U_CAPI int32_t U_EXPORT2
   1.102 +u_strToUpper(UChar *dest, int32_t destCapacity,
   1.103 +             const UChar *src, int32_t srcLength,
   1.104 +             const char *locale,
   1.105 +             UErrorCode *pErrorCode) {
   1.106 +    UCaseMap csm=UCASEMAP_INITIALIZER;
   1.107 +    setTempCaseMap(&csm, locale);
   1.108 +    return ustrcase_map(
   1.109 +        &csm,
   1.110 +        dest, destCapacity,
   1.111 +        src, srcLength,
   1.112 +        ustrcase_internalToUpper, pErrorCode);
   1.113 +}

mercurial