Jungshik Shin | 87232d8 | 2017-05-13 21:10:13 -0700 | [diff] [blame] | 1 | // © 2016 and later: Unicode, Inc. and others. |
Jungshik Shin | 5feb9ad | 2016-10-21 12:52:48 -0700 | [diff] [blame] | 2 | // License & terms of use: http://www.unicode.org/copyright.html |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 3 | /** |
| 4 | ******************************************************************************* |
Jungshik Shin (jungshik at google) | 0f8746a | 2015-01-08 15:46:45 -0800 | [diff] [blame] | 5 | * Copyright (C) 2001-2014, International Business Machines Corporation and * |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 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" |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 22 | #include "uassert.h" |
| 23 | |
| 24 | #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 | { |
| 35 | return LocaleKey::createWithCanonicalFallback(primaryID, canonicalFallbackID, KIND_ANY, status); |
| 36 | } |
| 37 | |
| 38 | LocaleKey* |
| 39 | LocaleKey::createWithCanonicalFallback(const UnicodeString* primaryID, |
| 40 | const UnicodeString* canonicalFallbackID, |
| 41 | int32_t kind, |
| 42 | UErrorCode& status) |
| 43 | { |
| 44 | if (primaryID == NULL || U_FAILURE(status)) { |
| 45 | return NULL; |
| 46 | } |
| 47 | UnicodeString canonicalPrimaryID; |
| 48 | LocaleUtility::canonicalLocaleString(primaryID, canonicalPrimaryID); |
| 49 | return new LocaleKey(*primaryID, canonicalPrimaryID, canonicalFallbackID, kind); |
| 50 | } |
| 51 | |
| 52 | LocaleKey::LocaleKey(const UnicodeString& primaryID, |
| 53 | const UnicodeString& canonicalPrimaryID, |
| 54 | const UnicodeString* canonicalFallbackID, |
| 55 | int32_t kind) |
| 56 | : ICUServiceKey(primaryID) |
| 57 | , _kind(kind) |
| 58 | , _primaryID(canonicalPrimaryID) |
| 59 | , _fallbackID() |
| 60 | , _currentID() |
| 61 | { |
| 62 | _fallbackID.setToBogus(); |
| 63 | if (_primaryID.length() != 0) { |
| 64 | if (canonicalFallbackID != NULL && _primaryID != *canonicalFallbackID) { |
| 65 | _fallbackID = *canonicalFallbackID; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | _currentID = _primaryID; |
| 70 | } |
| 71 | |
| 72 | LocaleKey::~LocaleKey() {} |
| 73 | |
| 74 | UnicodeString& |
| 75 | LocaleKey::prefix(UnicodeString& result) const { |
| 76 | if (_kind != KIND_ANY) { |
| 77 | UChar buffer[64]; |
| 78 | uprv_itou(buffer, 64, _kind, 10, 0); |
| 79 | UnicodeString temp(buffer); |
| 80 | result.append(temp); |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
| 85 | int32_t |
| 86 | LocaleKey::kind() const { |
| 87 | return _kind; |
| 88 | } |
| 89 | |
| 90 | UnicodeString& |
| 91 | LocaleKey::canonicalID(UnicodeString& result) const { |
| 92 | return result.append(_primaryID); |
| 93 | } |
| 94 | |
| 95 | UnicodeString& |
| 96 | LocaleKey::currentID(UnicodeString& result) const { |
| 97 | if (!_currentID.isBogus()) { |
| 98 | result.append(_currentID); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | UnicodeString& |
| 104 | LocaleKey::currentDescriptor(UnicodeString& result) const { |
| 105 | if (!_currentID.isBogus()) { |
| 106 | prefix(result).append(PREFIX_DELIMITER).append(_currentID); |
| 107 | } else { |
| 108 | result.setToBogus(); |
| 109 | } |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | Locale& |
| 114 | LocaleKey::canonicalLocale(Locale& result) const { |
| 115 | return LocaleUtility::initLocaleFromName(_primaryID, result); |
| 116 | } |
| 117 | |
| 118 | Locale& |
| 119 | LocaleKey::currentLocale(Locale& result) const { |
| 120 | return LocaleUtility::initLocaleFromName(_currentID, result); |
| 121 | } |
| 122 | |
| 123 | UBool |
| 124 | LocaleKey::fallback() { |
| 125 | if (!_currentID.isBogus()) { |
| 126 | int x = _currentID.lastIndexOf(UNDERSCORE_CHAR); |
| 127 | if (x != -1) { |
| 128 | _currentID.remove(x); // truncate current or fallback, whichever we're pointing to |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 129 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | if (!_fallbackID.isBogus()) { |
| 133 | _currentID = _fallbackID; |
| 134 | _fallbackID.setToBogus(); |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 135 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | if (_currentID.length() > 0) { |
| 139 | _currentID.remove(0); // completely truncate |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 140 | return true; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | _currentID.setToBogus(); |
| 144 | } |
| 145 | |
Frank Tang | 1f164ee | 2022-11-08 12:31:27 -0800 | [diff] [blame^] | 146 | return false; |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | UBool |
| 150 | LocaleKey::isFallbackOf(const UnicodeString& id) const { |
| 151 | UnicodeString temp(id); |
| 152 | parseSuffix(temp); |
| 153 | return temp.indexOf(_primaryID) == 0 && |
| 154 | (temp.length() == _primaryID.length() || |
| 155 | temp.charAt(_primaryID.length()) == UNDERSCORE_CHAR); |
| 156 | } |
| 157 | |
| 158 | #ifdef SERVICE_DEBUG |
| 159 | UnicodeString& |
| 160 | LocaleKey::debug(UnicodeString& result) const |
| 161 | { |
| 162 | ICUServiceKey::debug(result); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 163 | result.append((UnicodeString)" kind: "); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 164 | result.append(_kind); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 165 | result.append((UnicodeString)" primaryID: "); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 166 | result.append(_primaryID); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 167 | result.append((UnicodeString)" fallbackID: "); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 168 | result.append(_fallbackID); |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 169 | result.append((UnicodeString)" currentID: "); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 170 | result.append(_currentID); |
| 171 | return result; |
| 172 | } |
| 173 | |
| 174 | UnicodeString& |
| 175 | LocaleKey::debugClass(UnicodeString& result) const |
| 176 | { |
Jungshik Shin | 70f8250 | 2016-01-29 00:32:36 -0800 | [diff] [blame] | 177 | return result.append((UnicodeString)"LocaleKey "); |
jshin@chromium.org | 6f31ac3 | 2014-03-26 22:15:14 +0000 | [diff] [blame] | 178 | } |
| 179 | #endif |
| 180 | |
| 181 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocaleKey) |
| 182 | |
| 183 | U_NAMESPACE_END |
| 184 | |
| 185 | /* !UCONFIG_NO_SERVICE */ |
| 186 | #endif |
| 187 | |
| 188 | |