Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: ustrcase_locale.cpp |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * created on: 2011may31 |
michael@0 | 12 | * created by: Markus W. Scherer |
michael@0 | 13 | * |
michael@0 | 14 | * Locale-sensitive case mapping functions (ones that call uloc_getDefault()) |
michael@0 | 15 | * were moved here to break dependency cycles among parts of the common library. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #include "unicode/utypes.h" |
michael@0 | 19 | #include "unicode/ucasemap.h" |
michael@0 | 20 | #include "unicode/uloc.h" |
michael@0 | 21 | #include "unicode/ustring.h" |
michael@0 | 22 | #include "ucase.h" |
michael@0 | 23 | #include "ustr_imp.h" |
michael@0 | 24 | |
michael@0 | 25 | U_CFUNC void |
michael@0 | 26 | ustrcase_setTempCaseMapLocale(UCaseMap *csm, const char *locale) { |
michael@0 | 27 | /* |
michael@0 | 28 | * We could call ucasemap_setLocale(), but here we really only care about |
michael@0 | 29 | * the initial language subtag, we need not return the real string via |
michael@0 | 30 | * ucasemap_getLocale(), and we don't care about only getting "x" from |
michael@0 | 31 | * "x-some-thing" etc. |
michael@0 | 32 | * |
michael@0 | 33 | * We ignore locales with a longer-than-3 initial subtag. |
michael@0 | 34 | * |
michael@0 | 35 | * We also do not fill in the locCache because it is rarely used, |
michael@0 | 36 | * and not worth setting unless we reuse it for many case mapping operations. |
michael@0 | 37 | * (That's why UCaseMap was created.) |
michael@0 | 38 | */ |
michael@0 | 39 | int i; |
michael@0 | 40 | char c; |
michael@0 | 41 | |
michael@0 | 42 | /* the internal functions require locale!=NULL */ |
michael@0 | 43 | if(locale==NULL) { |
michael@0 | 44 | // Do not call uprv_getDefaultLocaleID() because that does not see |
michael@0 | 45 | // changes to the default locale via uloc_setDefault(). |
michael@0 | 46 | // It would also be inefficient if used frequently because uprv_getDefaultLocaleID() |
michael@0 | 47 | // does not cache the locale ID. |
michael@0 | 48 | // |
michael@0 | 49 | // Unfortunately, uloc_getDefault() has many dependencies. |
michael@0 | 50 | // We only care about a small set of language subtags, |
michael@0 | 51 | // and we do not need the locale ID to be canonicalized. |
michael@0 | 52 | // |
michael@0 | 53 | // Best is to not call case mapping functions with a NULL locale ID. |
michael@0 | 54 | locale=uloc_getDefault(); |
michael@0 | 55 | } |
michael@0 | 56 | for(i=0; i<4 && (c=locale[i])!=0 && c!='-' && c!='_'; ++i) { |
michael@0 | 57 | csm->locale[i]=c; |
michael@0 | 58 | } |
michael@0 | 59 | if(i<=3) { |
michael@0 | 60 | csm->locale[i]=0; /* Up to 3 non-separator characters. */ |
michael@0 | 61 | } else { |
michael@0 | 62 | csm->locale[0]=0; /* Longer-than-3 initial subtag: Ignore. */ |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /* |
michael@0 | 67 | * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. |
michael@0 | 68 | * Do this fast because it is called with every function call. |
michael@0 | 69 | */ |
michael@0 | 70 | static inline void |
michael@0 | 71 | setTempCaseMap(UCaseMap *csm, const char *locale) { |
michael@0 | 72 | if(csm->csp==NULL) { |
michael@0 | 73 | csm->csp=ucase_getSingleton(); |
michael@0 | 74 | } |
michael@0 | 75 | if(locale!=NULL && locale[0]==0) { |
michael@0 | 76 | csm->locale[0]=0; |
michael@0 | 77 | } else { |
michael@0 | 78 | ustrcase_setTempCaseMapLocale(csm, locale); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | /* public API functions */ |
michael@0 | 83 | |
michael@0 | 84 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 85 | u_strToLower(UChar *dest, int32_t destCapacity, |
michael@0 | 86 | const UChar *src, int32_t srcLength, |
michael@0 | 87 | const char *locale, |
michael@0 | 88 | UErrorCode *pErrorCode) { |
michael@0 | 89 | UCaseMap csm=UCASEMAP_INITIALIZER; |
michael@0 | 90 | setTempCaseMap(&csm, locale); |
michael@0 | 91 | return ustrcase_map( |
michael@0 | 92 | &csm, |
michael@0 | 93 | dest, destCapacity, |
michael@0 | 94 | src, srcLength, |
michael@0 | 95 | ustrcase_internalToLower, pErrorCode); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 99 | u_strToUpper(UChar *dest, int32_t destCapacity, |
michael@0 | 100 | const UChar *src, int32_t srcLength, |
michael@0 | 101 | const char *locale, |
michael@0 | 102 | UErrorCode *pErrorCode) { |
michael@0 | 103 | UCaseMap csm=UCASEMAP_INITIALIZER; |
michael@0 | 104 | setTempCaseMap(&csm, locale); |
michael@0 | 105 | return ustrcase_map( |
michael@0 | 106 | &csm, |
michael@0 | 107 | dest, destCapacity, |
michael@0 | 108 | src, srcLength, |
michael@0 | 109 | ustrcase_internalToUpper, pErrorCode); |
michael@0 | 110 | } |