michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2011, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * file name: ustrcase_locale.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2011may31 michael@0: * created by: Markus W. Scherer michael@0: * michael@0: * Locale-sensitive case mapping functions (ones that call uloc_getDefault()) michael@0: * were moved here to break dependency cycles among parts of the common library. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/ucasemap.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ustring.h" michael@0: #include "ucase.h" michael@0: #include "ustr_imp.h" michael@0: michael@0: U_CFUNC void michael@0: ustrcase_setTempCaseMapLocale(UCaseMap *csm, const char *locale) { michael@0: /* michael@0: * We could call ucasemap_setLocale(), but here we really only care about michael@0: * the initial language subtag, we need not return the real string via michael@0: * ucasemap_getLocale(), and we don't care about only getting "x" from michael@0: * "x-some-thing" etc. michael@0: * michael@0: * We ignore locales with a longer-than-3 initial subtag. michael@0: * michael@0: * We also do not fill in the locCache because it is rarely used, michael@0: * and not worth setting unless we reuse it for many case mapping operations. michael@0: * (That's why UCaseMap was created.) michael@0: */ michael@0: int i; michael@0: char c; michael@0: michael@0: /* the internal functions require locale!=NULL */ michael@0: if(locale==NULL) { michael@0: // Do not call uprv_getDefaultLocaleID() because that does not see michael@0: // changes to the default locale via uloc_setDefault(). michael@0: // It would also be inefficient if used frequently because uprv_getDefaultLocaleID() michael@0: // does not cache the locale ID. michael@0: // michael@0: // Unfortunately, uloc_getDefault() has many dependencies. michael@0: // We only care about a small set of language subtags, michael@0: // and we do not need the locale ID to be canonicalized. michael@0: // michael@0: // Best is to not call case mapping functions with a NULL locale ID. michael@0: locale=uloc_getDefault(); michael@0: } michael@0: for(i=0; i<4 && (c=locale[i])!=0 && c!='-' && c!='_'; ++i) { michael@0: csm->locale[i]=c; michael@0: } michael@0: if(i<=3) { michael@0: csm->locale[i]=0; /* Up to 3 non-separator characters. */ michael@0: } else { michael@0: csm->locale[0]=0; /* Longer-than-3 initial subtag: Ignore. */ michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Set parameters on an empty UCaseMap, for UCaseMap-less API functions. michael@0: * Do this fast because it is called with every function call. michael@0: */ michael@0: static inline void michael@0: setTempCaseMap(UCaseMap *csm, const char *locale) { michael@0: if(csm->csp==NULL) { michael@0: csm->csp=ucase_getSingleton(); michael@0: } michael@0: if(locale!=NULL && locale[0]==0) { michael@0: csm->locale[0]=0; michael@0: } else { michael@0: ustrcase_setTempCaseMapLocale(csm, locale); michael@0: } michael@0: } michael@0: michael@0: /* public API functions */ michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_strToLower(UChar *dest, int32_t destCapacity, michael@0: const UChar *src, int32_t srcLength, michael@0: const char *locale, michael@0: UErrorCode *pErrorCode) { michael@0: UCaseMap csm=UCASEMAP_INITIALIZER; michael@0: setTempCaseMap(&csm, locale); michael@0: return ustrcase_map( michael@0: &csm, michael@0: dest, destCapacity, michael@0: src, srcLength, michael@0: ustrcase_internalToLower, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: u_strToUpper(UChar *dest, int32_t destCapacity, michael@0: const UChar *src, int32_t srcLength, michael@0: const char *locale, michael@0: UErrorCode *pErrorCode) { michael@0: UCaseMap csm=UCASEMAP_INITIALIZER; michael@0: setTempCaseMap(&csm, locale); michael@0: return ustrcase_map( michael@0: &csm, michael@0: dest, destCapacity, michael@0: src, srcLength, michael@0: ustrcase_internalToUpper, pErrorCode); michael@0: }