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