2 *******************************************************************************
3 * Copyright (C) 2001-2014, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 *******************************************************************************
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_SERVICE
13 #include "unicode/resbund.h"
22 #define UNDERSCORE_CHAR ((UChar)0x005f)
23 #define AT_SIGN_CHAR ((UChar)64)
24 #define PERIOD_CHAR ((UChar)46)
29 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
30 const UnicodeString
* canonicalFallbackID
,
33 return LocaleKey::createWithCanonicalFallback(primaryID
, canonicalFallbackID
, KIND_ANY
, status
);
37 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
38 const UnicodeString
* canonicalFallbackID
,
42 if (primaryID
== NULL
|| U_FAILURE(status
)) {
45 UnicodeString canonicalPrimaryID
;
46 LocaleUtility::canonicalLocaleString(primaryID
, canonicalPrimaryID
);
47 return new LocaleKey(*primaryID
, canonicalPrimaryID
, canonicalFallbackID
, kind
);
50 LocaleKey::LocaleKey(const UnicodeString
& primaryID
,
51 const UnicodeString
& canonicalPrimaryID
,
52 const UnicodeString
* canonicalFallbackID
,
54 : ICUServiceKey(primaryID
)
56 , _primaryID(canonicalPrimaryID
)
60 _fallbackID
.setToBogus();
61 if (_primaryID
.length() != 0) {
62 if (canonicalFallbackID
!= NULL
&& _primaryID
!= *canonicalFallbackID
) {
63 _fallbackID
= *canonicalFallbackID
;
67 _currentID
= _primaryID
;
70 LocaleKey::~LocaleKey() {}
73 LocaleKey::prefix(UnicodeString
& result
) const {
74 if (_kind
!= KIND_ANY
) {
76 uprv_itou(buffer
, 64, _kind
, 10, 0);
77 UnicodeString
temp(buffer
);
84 LocaleKey::kind() const {
89 LocaleKey::canonicalID(UnicodeString
& result
) const {
90 return result
.append(_primaryID
);
94 LocaleKey::currentID(UnicodeString
& result
) const {
95 if (!_currentID
.isBogus()) {
96 result
.append(_currentID
);
102 LocaleKey::currentDescriptor(UnicodeString
& result
) const {
103 if (!_currentID
.isBogus()) {
104 prefix(result
).append(PREFIX_DELIMITER
).append(_currentID
);
112 LocaleKey::canonicalLocale(Locale
& result
) const {
113 return LocaleUtility::initLocaleFromName(_primaryID
, result
);
117 LocaleKey::currentLocale(Locale
& result
) const {
118 return LocaleUtility::initLocaleFromName(_currentID
, result
);
122 LocaleKey::fallback() {
123 if (!_currentID
.isBogus()) {
124 int x
= _currentID
.lastIndexOf(UNDERSCORE_CHAR
);
126 _currentID
.remove(x
); // truncate current or fallback, whichever we're pointing to
130 if (!_fallbackID
.isBogus()) {
131 _currentID
= _fallbackID
;
132 _fallbackID
.setToBogus();
136 if (_currentID
.length() > 0) {
137 _currentID
.remove(0); // completely truncate
141 _currentID
.setToBogus();
148 LocaleKey::isFallbackOf(const UnicodeString
& id
) const {
149 UnicodeString
temp(id
);
151 return temp
.indexOf(_primaryID
) == 0 &&
152 (temp
.length() == _primaryID
.length() ||
153 temp
.charAt(_primaryID
.length()) == UNDERSCORE_CHAR
);
158 LocaleKey::debug(UnicodeString
& result
) const
160 ICUServiceKey::debug(result
);
161 result
.append((UnicodeString
)" kind: ");
162 result
.append(_kind
);
163 result
.append((UnicodeString
)" primaryID: ");
164 result
.append(_primaryID
);
165 result
.append((UnicodeString
)" fallbackID: ");
166 result
.append(_fallbackID
);
167 result
.append((UnicodeString
)" currentID: ");
168 result
.append(_currentID
);
173 LocaleKey::debugClass(UnicodeString
& result
) const
175 return result
.append((UnicodeString
)"LocaleKey ");
179 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey
)
183 /* !UCONFIG_NO_SERVICE */