Coverage Report

Created: 2025-06-13 06:34

/src/icu/icu4c/source/common/locutil.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2016 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
/*
4
 *******************************************************************************
5
 * Copyright (C) 2002-2014, International Business Machines Corporation and
6
 * others. All Rights Reserved.
7
 *******************************************************************************
8
 */
9
#include "unicode/utypes.h"
10
11
#if !UCONFIG_NO_SERVICE || !UCONFIG_NO_TRANSLITERATION
12
13
#include "unicode/resbund.h"
14
#include "unicode/uenum.h"
15
#include "cmemory.h"
16
#include "ustrfmt.h"
17
#include "locutil.h"
18
#include "charstr.h"
19
#include "ucln_cmn.h"
20
#include "uassert.h"
21
#include "umutex.h"
22
23
// see LocaleUtility::getAvailableLocaleNames
24
static icu::UInitOnce   LocaleUtilityInitOnce {};
25
static icu::Hashtable * LocaleUtility_cache = nullptr;
26
27
0
#define UNDERSCORE_CHAR ((char16_t)0x005f)
28
0
#define AT_SIGN_CHAR    ((char16_t)64)
29
0
#define PERIOD_CHAR     ((char16_t)46)
30
31
/*
32
 ******************************************************************
33
 */
34
35
/**
36
 * Release all static memory held by Locale Utility.  
37
 */
38
U_CDECL_BEGIN
39
0
static UBool U_CALLCONV service_cleanup() {
40
0
    if (LocaleUtility_cache) {
41
0
        delete LocaleUtility_cache;
42
0
        LocaleUtility_cache = nullptr;
43
0
    }
44
0
    return true;
45
0
}
46
47
48
0
static void U_CALLCONV locale_utility_init(UErrorCode &status) {
49
0
    using namespace icu;
50
0
    U_ASSERT(LocaleUtility_cache == nullptr);
51
0
    ucln_common_registerCleanup(UCLN_COMMON_SERVICE, service_cleanup);
52
0
    LocaleUtility_cache = new Hashtable(status);
53
0
    if (U_FAILURE(status)) {
54
0
        delete LocaleUtility_cache;
55
0
        LocaleUtility_cache = nullptr;
56
0
        return;
57
0
    }
58
0
    if (LocaleUtility_cache == nullptr) {
59
0
        status = U_MEMORY_ALLOCATION_ERROR;
60
0
        return;
61
0
    }
62
0
    LocaleUtility_cache->setValueDeleter(uhash_deleteHashtable);
63
0
}
64
65
U_CDECL_END
66
67
U_NAMESPACE_BEGIN
68
69
UnicodeString&
70
LocaleUtility::canonicalLocaleString(const UnicodeString* id, UnicodeString& result)
71
0
{
72
0
  if (id == nullptr) {
73
0
    result.setToBogus();
74
0
  } else {
75
    // Fix case only (no other changes) up to the first '@' or '.' or
76
    // end of string, whichever comes first.  In 3.0 I changed this to
77
    // stop at first '@' or '.'.  It used to run out to the end of
78
    // string.  My fix makes the tests pass but is probably
79
    // structurally incorrect.  See below.  [alan 3.0]
80
81
    // TODO: Doug, you might want to revise this...
82
0
    result = *id;
83
0
    int32_t i = 0;
84
0
    int32_t end = result.indexOf(AT_SIGN_CHAR);
85
0
    int32_t n = result.indexOf(PERIOD_CHAR);
86
0
    if (n >= 0 && n < end) {
87
0
        end = n;
88
0
    }
89
0
    if (end < 0) {
90
0
        end = result.length();
91
0
    }
92
0
    n = result.indexOf(UNDERSCORE_CHAR);
93
0
    if (n < 0) {
94
0
      n = end;
95
0
    }
96
0
    for (; i < n; ++i) {
97
0
      char16_t c = result.charAt(i);
98
0
      if (c >= 0x0041 && c <= 0x005a) {
99
0
        c += 0x20;
100
0
        result.setCharAt(i, c);
101
0
      }
102
0
    }
103
0
    for (n = end; i < n; ++i) {
104
0
      char16_t c = result.charAt(i);
105
0
      if (c >= 0x0061 && c <= 0x007a) {
106
0
        c -= 0x20;
107
0
        result.setCharAt(i, c);
108
0
      }
109
0
    }
110
0
  }
111
0
  return result;
112
113
#if 0
114
    // This code does a proper full level 2 canonicalization of id.
115
    // It's nasty to go from char16_t to char to char to char16_t -- but
116
    // that's what you have to do to use the uloc_canonicalize
117
    // function on UnicodeStrings.
118
119
    // I ended up doing the alternate fix (see above) not for
120
    // performance reasons, although performance will certainly be
121
    // better, but because doing a full level 2 canonicalization
122
    // causes some tests to fail.  [alan 3.0]
123
124
    // TODO: Doug, you might want to revisit this...
125
    result.setToBogus();
126
    if (id != 0) {
127
        int32_t buflen = id->length() + 8; // space for NUL
128
        char* buf = (char*) uprv_malloc(buflen);
129
        char* canon = (buf == 0) ? 0 : (char*) uprv_malloc(buflen);
130
        if (buf != 0 && canon != 0) {
131
            U_ASSERT(id->extract(0, INT32_MAX, buf, buflen) < buflen);
132
            UErrorCode ec = U_ZERO_ERROR;
133
            uloc_canonicalize(buf, canon, buflen, &ec);
134
            if (U_SUCCESS(ec)) {
135
                result = UnicodeString(canon);
136
            }
137
        }
138
        uprv_free(buf);
139
        uprv_free(canon);
140
    }
141
    return result;
142
#endif
143
0
}
144
145
Locale&
146
LocaleUtility::initLocaleFromName(const UnicodeString& id, Locale& result)
147
0
{
148
0
    if (id.isBogus()) {
149
0
        result.setToBogus();
150
0
    } else {
151
        /*
152
         * We need to convert from a UnicodeString to char * in order to
153
         * create a Locale.
154
         *
155
         * Problem: Locale ID strings may contain '@' which is a variant
156
         * character and cannot be handled by invariant-character conversion.
157
         *
158
         * Hack: Since ICU code can handle locale IDs with multiple encodings
159
         * of '@' (at least for EBCDIC; it's not known to be a problem for
160
         * ASCII-based systems),
161
         * we use regular invariant-character conversion for everything else
162
         * and manually convert U+0040 into a compiler-char-constant '@'.
163
         * While this compilation-time constant may not match the runtime
164
         * encoding of '@', it should be one of the encodings which ICU
165
         * recognizes.
166
         *
167
         * There should be only at most one '@' in a locale ID.
168
         */
169
0
        CharString buffer;
170
0
        int32_t prev, i;
171
0
        prev = 0;
172
0
        UErrorCode status = U_ZERO_ERROR;
173
0
        do {
174
0
            i = id.indexOf(static_cast<char16_t>(0x40), prev);
175
0
            if(i < 0) {
176
                // no @ between prev and the rest of the string
177
0
                buffer.appendInvariantChars(id.tempSubString(prev), status);
178
0
                break; // done
179
0
            } else {
180
                // normal invariant-character conversion for text between @s
181
0
                buffer.appendInvariantChars(id.tempSubString(prev, i - prev), status);
182
                // manually "convert" U+0040 at id[i] into '@' at buffer[i]
183
0
                buffer.append('@', status);
184
0
                prev = i + 1;
185
0
            }
186
0
        } while (U_SUCCESS(status));
187
0
        if (U_FAILURE(status)) {
188
0
            result.setToBogus();
189
0
        } else {
190
0
            result = Locale::createFromName(buffer.data());
191
0
        }
192
0
    }
193
0
    return result;
194
0
}
195
196
UnicodeString&
197
LocaleUtility::initNameFromLocale(const Locale& locale, UnicodeString& result)
198
0
{
199
0
    if (locale.isBogus()) {
200
0
        result.setToBogus();
201
0
    } else {
202
0
        result.append(UnicodeString(locale.getName(), -1, US_INV));
203
0
    }
204
0
    return result;
205
0
}
206
207
const Hashtable*
208
LocaleUtility::getAvailableLocaleNames(const UnicodeString& bundleID)
209
0
{
210
    // LocaleUtility_cache is a hash-of-hashes.  The top-level keys
211
    // are path strings ('bundleID') passed to
212
    // ures_openAvailableLocales.  The top-level values are
213
    // second-level hashes.  The second-level keys are result strings
214
    // from ures_openAvailableLocales.  The second-level values are
215
    // garbage ((void*)1 or other random pointer).
216
217
0
    UErrorCode status = U_ZERO_ERROR;
218
0
    umtx_initOnce(LocaleUtilityInitOnce, locale_utility_init, status);
219
0
    Hashtable *cache = LocaleUtility_cache;
220
0
    if (cache == nullptr) {
221
        // Catastrophic failure.
222
0
        return nullptr;
223
0
    }
224
225
0
    Hashtable* htp;
226
0
    umtx_lock(nullptr);
227
0
    htp = static_cast<Hashtable*>(cache->get(bundleID));
228
0
    umtx_unlock(nullptr);
229
230
0
    if (htp == nullptr) {
231
0
        htp = new Hashtable(status);
232
0
        if (htp && U_SUCCESS(status)) {
233
0
            CharString cbundleID;
234
0
            cbundleID.appendInvariantChars(bundleID, status);
235
0
            const char* path = cbundleID.isEmpty() ? nullptr : cbundleID.data();
236
0
            icu::LocalUEnumerationPointer uenum(ures_openAvailableLocales(path, &status));
237
0
            for (;;) {
238
0
                const char16_t* id = uenum_unext(uenum.getAlias(), nullptr, &status);
239
0
                if (id == nullptr) {
240
0
                    break;
241
0
                }
242
0
                htp->put(UnicodeString(id), (void*)htp, status);
243
0
            }
244
0
            if (U_FAILURE(status)) {
245
0
                delete htp;
246
0
                return nullptr;
247
0
            }
248
0
            umtx_lock(nullptr);
249
0
            Hashtable *t = static_cast<Hashtable *>(cache->get(bundleID));
250
0
            if (t != nullptr) {
251
                // Another thread raced through this code, creating the cache entry first.
252
                // Discard ours and return theirs.
253
0
                umtx_unlock(nullptr);
254
0
                delete htp;
255
0
                htp = t;
256
0
            } else {
257
0
                cache->put(bundleID, (void*)htp, status);
258
0
                umtx_unlock(nullptr);
259
0
            }
260
0
        }
261
0
    }
262
0
    return htp;
263
0
}
264
265
bool
266
LocaleUtility::isFallbackOf(const UnicodeString& root, const UnicodeString& child)
267
0
{
268
0
    return child.indexOf(root) == 0 &&
269
0
      (child.length() == root.length() ||
270
0
       child.charAt(root.length()) == UNDERSCORE_CHAR);
271
0
}
272
273
U_NAMESPACE_END
274
275
/* !UCONFIG_NO_SERVICE */
276
#endif