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