Imported Upstream version 58.1
[platform/upstream/icu.git] / source / common / unistr_case_locale.cpp
1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *   Copyright (C) 2011, International Business Machines
6 *   Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 *   file name:  unistr_case_locale.cpp
9 *   encoding:   US-ASCII
10 *   tab size:   8 (not used)
11 *   indentation:4
12 *
13 *   created on: 2011may31
14 *   created by: Markus W. Scherer
15 *
16 *   Locale-sensitive case mapping functions (ones that call uloc_getDefault())
17 *   were moved here to break dependency cycles among parts of the common library.
18 */
19
20 #include "unicode/utypes.h"
21 #include "unicode/locid.h"
22 #include "unicode/unistr.h"
23 #include "cmemory.h"
24 #include "ustr_imp.h"
25
26 U_NAMESPACE_BEGIN
27
28 //========================================
29 // Write implementation
30 //========================================
31
32 /*
33  * Set parameters on an empty UCaseMap, for UCaseMap-less API functions.
34  * Do this fast because it is called with every function call.
35  */
36 static inline void
37 setTempCaseMap(UCaseMap *csm, const char *locale) {
38     if(csm->csp==NULL) {
39         csm->csp=ucase_getSingleton();
40     }
41     if(locale!=NULL && locale[0]==0) {
42         csm->locale[0]=0;
43     } else {
44         ustrcase_setTempCaseMapLocale(csm, locale);
45     }
46 }
47
48 UnicodeString &
49 UnicodeString::toLower() {
50   return toLower(Locale::getDefault());
51 }
52
53 UnicodeString &
54 UnicodeString::toLower(const Locale &locale) {
55   UCaseMap csm=UCASEMAP_INITIALIZER;
56   setTempCaseMap(&csm, locale.getName());
57   return caseMap(&csm, ustrcase_internalToLower);
58 }
59
60 UnicodeString &
61 UnicodeString::toUpper() {
62   return toUpper(Locale::getDefault());
63 }
64
65 UnicodeString &
66 UnicodeString::toUpper(const Locale &locale) {
67   UCaseMap csm=UCASEMAP_INITIALIZER;
68   setTempCaseMap(&csm, locale.getName());
69   return caseMap(&csm, ustrcase_internalToUpper);
70 }
71
72 U_NAMESPACE_END