1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2001-2014, International Business Machines Corporation and *
6 * others. All Rights Reserved. *
7 *******************************************************************************
9 *******************************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_SERVICE
15 #include "unicode/resbund.h"
24 #define UNDERSCORE_CHAR ((UChar)0x005f)
25 #define AT_SIGN_CHAR ((UChar)64)
26 #define PERIOD_CHAR ((UChar)46)
31 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
32 const UnicodeString
* canonicalFallbackID
,
35 return LocaleKey::createWithCanonicalFallback(primaryID
, canonicalFallbackID
, KIND_ANY
, status
);
39 LocaleKey::createWithCanonicalFallback(const UnicodeString
* primaryID
,
40 const UnicodeString
* canonicalFallbackID
,
44 if (primaryID
== NULL
|| U_FAILURE(status
)) {
47 UnicodeString canonicalPrimaryID
;
48 LocaleUtility::canonicalLocaleString(primaryID
, canonicalPrimaryID
);
49 return new LocaleKey(*primaryID
, canonicalPrimaryID
, canonicalFallbackID
, kind
);
52 LocaleKey::LocaleKey(const UnicodeString
& primaryID
,
53 const UnicodeString
& canonicalPrimaryID
,
54 const UnicodeString
* canonicalFallbackID
,
56 : ICUServiceKey(primaryID
)
58 , _primaryID(canonicalPrimaryID
)
62 _fallbackID
.setToBogus();
63 if (_primaryID
.length() != 0) {
64 if (canonicalFallbackID
!= NULL
&& _primaryID
!= *canonicalFallbackID
) {
65 _fallbackID
= *canonicalFallbackID
;
69 _currentID
= _primaryID
;
72 LocaleKey::~LocaleKey() {}
75 LocaleKey::prefix(UnicodeString
& result
) const {
76 if (_kind
!= KIND_ANY
) {
78 uprv_itou(buffer
, 64, _kind
, 10, 0);
79 UnicodeString
temp(buffer
);
86 LocaleKey::kind() const {
91 LocaleKey::canonicalID(UnicodeString
& result
) const {
92 return result
.append(_primaryID
);
96 LocaleKey::currentID(UnicodeString
& result
) const {
97 if (!_currentID
.isBogus()) {
98 result
.append(_currentID
);
104 LocaleKey::currentDescriptor(UnicodeString
& result
) const {
105 if (!_currentID
.isBogus()) {
106 prefix(result
).append(PREFIX_DELIMITER
).append(_currentID
);
114 LocaleKey::canonicalLocale(Locale
& result
) const {
115 return LocaleUtility::initLocaleFromName(_primaryID
, result
);
119 LocaleKey::currentLocale(Locale
& result
) const {
120 return LocaleUtility::initLocaleFromName(_currentID
, result
);
124 LocaleKey::fallback() {
125 if (!_currentID
.isBogus()) {
126 int x
= _currentID
.lastIndexOf(UNDERSCORE_CHAR
);
128 _currentID
.remove(x
); // truncate current or fallback, whichever we're pointing to
132 if (!_fallbackID
.isBogus()) {
133 _currentID
= _fallbackID
;
134 _fallbackID
.setToBogus();
138 if (_currentID
.length() > 0) {
139 _currentID
.remove(0); // completely truncate
143 _currentID
.setToBogus();
150 LocaleKey::isFallbackOf(const UnicodeString
& id
) const {
151 UnicodeString
temp(id
);
153 return temp
.indexOf(_primaryID
) == 0 &&
154 (temp
.length() == _primaryID
.length() ||
155 temp
.charAt(_primaryID
.length()) == UNDERSCORE_CHAR
);
160 LocaleKey::debug(UnicodeString
& result
) const
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
);
175 LocaleKey::debugClass(UnicodeString
& result
) const
177 return result
.append((UnicodeString
)"LocaleKey ");
181 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey
)
185 /* !UCONFIG_NO_SERVICE */