/src/icu/source/common/servlk.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | /**  | 
4  |  |  *******************************************************************************  | 
5  |  |  * Copyright (C) 2001-2014, International Business Machines Corporation and    *  | 
6  |  |  * others. All Rights Reserved.                                                *  | 
7  |  |  *******************************************************************************  | 
8  |  |  *  | 
9  |  |  *******************************************************************************  | 
10  |  |  */  | 
11  |  | #include "unicode/utypes.h"  | 
12  |  |  | 
13  |  | #if !UCONFIG_NO_SERVICE  | 
14  |  |  | 
15  |  | #include "unicode/resbund.h"  | 
16  |  | #include "uresimp.h"  | 
17  |  | #include "cmemory.h"  | 
18  |  | #include "servloc.h"  | 
19  |  | #include "ustrfmt.h"  | 
20  |  | #include "uhash.h"  | 
21  |  | #include "charstr.h"  | 
22  |  | #include "uassert.h"  | 
23  |  |  | 
24  | 0  | #define UNDERSCORE_CHAR ((UChar)0x005f)  | 
25  |  | #define AT_SIGN_CHAR    ((UChar)64)  | 
26  |  | #define PERIOD_CHAR     ((UChar)46)  | 
27  |  |  | 
28  |  | U_NAMESPACE_BEGIN  | 
29  |  |  | 
30  |  | LocaleKey*  | 
31  |  | LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID,  | 
32  |  |                                        const UnicodeString* canonicalFallbackID,  | 
33  |  |                                        UErrorCode& status)  | 
34  | 0  | { | 
35  | 0  |     return LocaleKey::createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY, status);  | 
36  | 0  | }  | 
37  |  |  | 
38  |  | LocaleKey*  | 
39  |  | LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID,  | 
40  |  |                                        const UnicodeString* canonicalFallbackID,  | 
41  |  |                                        int32_t kind,  | 
42  |  |                                        UErrorCode& status)  | 
43  | 0  | { | 
44  | 0  |     if (primaryID == NULL || U_FAILURE(status)) { | 
45  | 0  |         return NULL;  | 
46  | 0  |     }  | 
47  | 0  |     UnicodeString canonicalPrimaryID;  | 
48  | 0  |     LocaleUtility::canonicalLocaleString(primaryID, canonicalPrimaryID);  | 
49  | 0  |     return new LocaleKey(*primaryID, canonicalPrimaryID, canonicalFallbackID, kind);  | 
50  | 0  | }  | 
51  |  |  | 
52  |  | LocaleKey::LocaleKey(const UnicodeString& primaryID,  | 
53  |  |                      const UnicodeString& canonicalPrimaryID,  | 
54  |  |                      const UnicodeString* canonicalFallbackID,  | 
55  |  |                      int32_t kind)  | 
56  | 0  |   : ICUServiceKey(primaryID)  | 
57  | 0  |   , _kind(kind)  | 
58  | 0  |   , _primaryID(canonicalPrimaryID)  | 
59  | 0  |   , _fallbackID()  | 
60  | 0  |   , _currentID()  | 
61  | 0  | { | 
62  | 0  |     _fallbackID.setToBogus();  | 
63  | 0  |     if (_primaryID.length() != 0) { | 
64  | 0  |         if (canonicalFallbackID != NULL && _primaryID != *canonicalFallbackID) { | 
65  | 0  |             _fallbackID = *canonicalFallbackID;  | 
66  | 0  |         }  | 
67  | 0  |     }  | 
68  |  | 
  | 
69  | 0  |     _currentID = _primaryID;  | 
70  | 0  | }  | 
71  |  |  | 
72  | 0  | LocaleKey::~LocaleKey() {} | 
73  |  |  | 
74  |  | UnicodeString&  | 
75  | 0  | LocaleKey::prefix(UnicodeString& result) const { | 
76  | 0  |     if (_kind != KIND_ANY) { | 
77  | 0  |         UChar buffer[64];  | 
78  | 0  |         uprv_itou(buffer, 64, _kind, 10, 0);  | 
79  | 0  |         UnicodeString temp(buffer);  | 
80  | 0  |         result.append(temp);  | 
81  | 0  |     }  | 
82  | 0  |     return result;  | 
83  | 0  | }  | 
84  |  |  | 
85  |  | int32_t  | 
86  | 0  | LocaleKey::kind() const { | 
87  | 0  |     return _kind;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | UnicodeString&  | 
91  | 0  | LocaleKey::canonicalID(UnicodeString& result) const { | 
92  | 0  |     return result.append(_primaryID);  | 
93  | 0  | }  | 
94  |  |  | 
95  |  | UnicodeString&  | 
96  | 0  | LocaleKey::currentID(UnicodeString& result) const { | 
97  | 0  |     if (!_currentID.isBogus()) { | 
98  | 0  |         result.append(_currentID);  | 
99  | 0  |     }  | 
100  | 0  |     return result;  | 
101  | 0  | }  | 
102  |  |  | 
103  |  | UnicodeString&  | 
104  | 0  | LocaleKey::currentDescriptor(UnicodeString& result) const { | 
105  | 0  |     if (!_currentID.isBogus()) { | 
106  | 0  |         prefix(result).append(PREFIX_DELIMITER).append(_currentID);  | 
107  | 0  |     } else { | 
108  | 0  |         result.setToBogus();  | 
109  | 0  |     }  | 
110  | 0  |     return result;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | Locale&  | 
114  | 0  | LocaleKey::canonicalLocale(Locale& result) const { | 
115  | 0  |     return LocaleUtility::initLocaleFromName(_primaryID, result);  | 
116  | 0  | }  | 
117  |  |  | 
118  |  | Locale&  | 
119  | 0  | LocaleKey::currentLocale(Locale& result) const { | 
120  | 0  |     return LocaleUtility::initLocaleFromName(_currentID, result);  | 
121  | 0  | }  | 
122  |  |  | 
123  |  | UBool  | 
124  | 0  | LocaleKey::fallback() { | 
125  | 0  |     if (!_currentID.isBogus()) { | 
126  | 0  |         int x = _currentID.lastIndexOf(UNDERSCORE_CHAR);  | 
127  | 0  |         if (x != -1) { | 
128  | 0  |             _currentID.remove(x); // truncate current or fallback, whichever we're pointing to  | 
129  | 0  |             return TRUE;  | 
130  | 0  |         }  | 
131  |  |  | 
132  | 0  |         if (!_fallbackID.isBogus()) { | 
133  | 0  |             _currentID = _fallbackID;  | 
134  | 0  |             _fallbackID.setToBogus();  | 
135  | 0  |             return TRUE;  | 
136  | 0  |         }  | 
137  |  |  | 
138  | 0  |         if (_currentID.length() > 0) { | 
139  | 0  |             _currentID.remove(0); // completely truncate  | 
140  | 0  |             return TRUE;  | 
141  | 0  |         }  | 
142  |  |  | 
143  | 0  |         _currentID.setToBogus();  | 
144  | 0  |     }  | 
145  |  |  | 
146  | 0  |     return FALSE;  | 
147  | 0  | }  | 
148  |  |  | 
149  |  | UBool  | 
150  | 0  | LocaleKey::isFallbackOf(const UnicodeString& id) const { | 
151  | 0  |     UnicodeString temp(id);  | 
152  | 0  |     parseSuffix(temp);  | 
153  | 0  |     return temp.indexOf(_primaryID) == 0 &&  | 
154  | 0  |         (temp.length() == _primaryID.length() ||  | 
155  | 0  |         temp.charAt(_primaryID.length()) == UNDERSCORE_CHAR);  | 
156  | 0  | }  | 
157  |  |  | 
158  |  | #ifdef SERVICE_DEBUG  | 
159  |  | UnicodeString&  | 
160  |  | LocaleKey::debug(UnicodeString& result) const  | 
161  |  | { | 
162  |  |     ICUServiceKey::debug(result);  | 
163  |  |     result.append((UnicodeString)" kind: ");  | 
164  |  |     result.append(_kind);  | 
165  |  |     result.append((UnicodeString)" primaryID: ");  | 
166  |  |     result.append(_primaryID);  | 
167  |  |     result.append((UnicodeString)" fallbackID: ");  | 
168  |  |     result.append(_fallbackID);  | 
169  |  |     result.append((UnicodeString)" currentID: ");  | 
170  |  |     result.append(_currentID);  | 
171  |  |     return result;  | 
172  |  | }  | 
173  |  |  | 
174  |  | UnicodeString&  | 
175  |  | LocaleKey::debugClass(UnicodeString& result) const  | 
176  |  | { | 
177  |  |     return result.append((UnicodeString)"LocaleKey ");  | 
178  |  | }  | 
179  |  | #endif  | 
180  |  |  | 
181  |  | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey)  | 
182  |  |  | 
183  |  | U_NAMESPACE_END  | 
184  |  |  | 
185  |  | /* !UCONFIG_NO_SERVICE */  | 
186  |  | #endif  | 
187  |  |  | 
188  |  |  |