intl/icu/source/common/servlk.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial