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