Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/common/locdspnm.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) 2010-2016, International Business Machines Corporation and
6
* others. All Rights Reserved.
7
*******************************************************************************
8
*/
9
10
#include "unicode/utypes.h"
11
12
#if !UCONFIG_NO_FORMATTING
13
14
#include "unicode/locdspnm.h"
15
#include "unicode/simpleformatter.h"
16
#include "unicode/ucasemap.h"
17
#include "unicode/ures.h"
18
#include "unicode/udisplaycontext.h"
19
#include "unicode/brkiter.h"
20
#include "unicode/ucurr.h"
21
#include "cmemory.h"
22
#include "cstring.h"
23
#include "mutex.h"
24
#include "ulocimp.h"
25
#include "umutex.h"
26
#include "ureslocs.h"
27
#include "uresimp.h"
28
29
#include <stdarg.h>
30
31
/**
32
 * Concatenate a number of null-terminated strings to buffer, leaving a
33
 * null-terminated string.  The last argument should be the null pointer.
34
 * Return the length of the string in the buffer, not counting the trailing
35
 * null.  Return -1 if there is an error (buffer is null, or buflen < 1).
36
 */
37
0
static int32_t ncat(char *buffer, uint32_t buflen, ...) {
38
0
  va_list args;
39
0
  char *str;
40
0
  char *p = buffer;
41
0
  const char* e = buffer + buflen - 1;
42
0
43
0
  if (buffer == NULL || buflen < 1) {
44
0
    return -1;
45
0
  }
46
0
47
0
  va_start(args, buflen);
48
0
  while ((str = va_arg(args, char *))) {
49
0
    char c;
50
0
    while (p != e && (c = *str++)) {
51
0
      *p++ = c;
52
0
    }
53
0
  }
54
0
  *p = 0;
55
0
  va_end(args);
56
0
57
0
  return static_cast<int32_t>(p - buffer);
58
0
}
59
60
U_NAMESPACE_BEGIN
61
62
////////////////////////////////////////////////////////////////////////////////////////////////////
63
64
// Access resource data for locale components.
65
// Wrap code in uloc.c for now.
66
class ICUDataTable {
67
    const char* path;
68
    Locale locale;
69
70
public:
71
    ICUDataTable(const char* path, const Locale& locale);
72
    ~ICUDataTable();
73
74
    const Locale& getLocale();
75
76
    UnicodeString& get(const char* tableKey, const char* itemKey,
77
                        UnicodeString& result) const;
78
    UnicodeString& get(const char* tableKey, const char* subTableKey, const char* itemKey,
79
                        UnicodeString& result) const;
80
81
    UnicodeString& getNoFallback(const char* tableKey, const char* itemKey,
82
                                UnicodeString &result) const;
83
    UnicodeString& getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey,
84
                                UnicodeString &result) const;
85
};
86
87
inline UnicodeString &
88
0
ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
89
0
    return get(tableKey, NULL, itemKey, result);
90
0
}
91
92
inline UnicodeString &
93
0
ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
94
0
    return getNoFallback(tableKey, NULL, itemKey, result);
95
0
}
96
97
ICUDataTable::ICUDataTable(const char* path, const Locale& locale)
98
    : path(NULL), locale(Locale::getRoot())
99
0
{
100
0
  if (path) {
101
0
    int32_t len = uprv_strlen(path);
102
0
    this->path = (const char*) uprv_malloc(len + 1);
103
0
    if (this->path) {
104
0
      uprv_strcpy((char *)this->path, path);
105
0
      this->locale = locale;
106
0
    }
107
0
  }
108
0
}
109
110
0
ICUDataTable::~ICUDataTable() {
111
0
  if (path) {
112
0
    uprv_free((void*) path);
113
0
    path = NULL;
114
0
  }
115
0
}
116
117
const Locale&
118
0
ICUDataTable::getLocale() {
119
0
  return locale;
120
0
}
121
122
UnicodeString &
123
ICUDataTable::get(const char* tableKey, const char* subTableKey, const char* itemKey,
124
0
                  UnicodeString &result) const {
125
0
  UErrorCode status = U_ZERO_ERROR;
126
0
  int32_t len = 0;
127
0
128
0
  const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
129
0
                                                   tableKey, subTableKey, itemKey,
130
0
                                                   &len, &status);
131
0
  if (U_SUCCESS(status) && len > 0) {
132
0
    return result.setTo(s, len);
133
0
  }
134
0
  return result.setTo(UnicodeString(itemKey, -1, US_INV));
135
0
}
136
137
UnicodeString &
138
ICUDataTable::getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey,
139
0
                            UnicodeString& result) const {
140
0
  UErrorCode status = U_ZERO_ERROR;
141
0
  int32_t len = 0;
142
0
143
0
  const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
144
0
                                                   tableKey, subTableKey, itemKey,
145
0
                                                   &len, &status);
146
0
  if (U_SUCCESS(status)) {
147
0
    return result.setTo(s, len);
148
0
  }
149
0
150
0
  result.setToBogus();
151
0
  return result;
152
0
}
153
154
////////////////////////////////////////////////////////////////////////////////////////////////////
155
156
0
LocaleDisplayNames::~LocaleDisplayNames() {}
157
158
////////////////////////////////////////////////////////////////////////////////////////////////////
159
160
#if 0  // currently unused
161
162
class DefaultLocaleDisplayNames : public LocaleDisplayNames {
163
  UDialectHandling dialectHandling;
164
165
public:
166
  // constructor
167
  DefaultLocaleDisplayNames(UDialectHandling dialectHandling);
168
169
  virtual ~DefaultLocaleDisplayNames();
170
171
  virtual const Locale& getLocale() const;
172
  virtual UDialectHandling getDialectHandling() const;
173
174
  virtual UnicodeString& localeDisplayName(const Locale& locale,
175
                                           UnicodeString& result) const;
176
  virtual UnicodeString& localeDisplayName(const char* localeId,
177
                                           UnicodeString& result) const;
178
  virtual UnicodeString& languageDisplayName(const char* lang,
179
                                             UnicodeString& result) const;
180
  virtual UnicodeString& scriptDisplayName(const char* script,
181
                                           UnicodeString& result) const;
182
  virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
183
                                           UnicodeString& result) const;
184
  virtual UnicodeString& regionDisplayName(const char* region,
185
                                           UnicodeString& result) const;
186
  virtual UnicodeString& variantDisplayName(const char* variant,
187
                                            UnicodeString& result) const;
188
  virtual UnicodeString& keyDisplayName(const char* key,
189
                                        UnicodeString& result) const;
190
  virtual UnicodeString& keyValueDisplayName(const char* key,
191
                                             const char* value,
192
                                             UnicodeString& result) const;
193
};
194
195
DefaultLocaleDisplayNames::DefaultLocaleDisplayNames(UDialectHandling dialectHandling)
196
    : dialectHandling(dialectHandling) {
197
}
198
199
DefaultLocaleDisplayNames::~DefaultLocaleDisplayNames() {
200
}
201
202
const Locale&
203
DefaultLocaleDisplayNames::getLocale() const {
204
  return Locale::getRoot();
205
}
206
207
UDialectHandling
208
DefaultLocaleDisplayNames::getDialectHandling() const {
209
  return dialectHandling;
210
}
211
212
UnicodeString&
213
DefaultLocaleDisplayNames::localeDisplayName(const Locale& locale,
214
                                             UnicodeString& result) const {
215
  return result = UnicodeString(locale.getName(), -1, US_INV);
216
}
217
218
UnicodeString&
219
DefaultLocaleDisplayNames::localeDisplayName(const char* localeId,
220
                                             UnicodeString& result) const {
221
  return result = UnicodeString(localeId, -1, US_INV);
222
}
223
224
UnicodeString&
225
DefaultLocaleDisplayNames::languageDisplayName(const char* lang,
226
                                               UnicodeString& result) const {
227
  return result = UnicodeString(lang, -1, US_INV);
228
}
229
230
UnicodeString&
231
DefaultLocaleDisplayNames::scriptDisplayName(const char* script,
232
                                             UnicodeString& result) const {
233
  return result = UnicodeString(script, -1, US_INV);
234
}
235
236
UnicodeString&
237
DefaultLocaleDisplayNames::scriptDisplayName(UScriptCode scriptCode,
238
                                             UnicodeString& result) const {
239
  const char* name = uscript_getName(scriptCode);
240
  if (name) {
241
    return result = UnicodeString(name, -1, US_INV);
242
  }
243
  return result.remove();
244
}
245
246
UnicodeString&
247
DefaultLocaleDisplayNames::regionDisplayName(const char* region,
248
                                             UnicodeString& result) const {
249
  return result = UnicodeString(region, -1, US_INV);
250
}
251
252
UnicodeString&
253
DefaultLocaleDisplayNames::variantDisplayName(const char* variant,
254
                                              UnicodeString& result) const {
255
  return result = UnicodeString(variant, -1, US_INV);
256
}
257
258
UnicodeString&
259
DefaultLocaleDisplayNames::keyDisplayName(const char* key,
260
                                          UnicodeString& result) const {
261
  return result = UnicodeString(key, -1, US_INV);
262
}
263
264
UnicodeString&
265
DefaultLocaleDisplayNames::keyValueDisplayName(const char* /* key */,
266
                                               const char* value,
267
                                               UnicodeString& result) const {
268
  return result = UnicodeString(value, -1, US_INV);
269
}
270
271
#endif  // currently unused class DefaultLocaleDisplayNames
272
273
////////////////////////////////////////////////////////////////////////////////////////////////////
274
275
class LocaleDisplayNamesImpl : public LocaleDisplayNames {
276
    Locale locale;
277
    UDialectHandling dialectHandling;
278
    ICUDataTable langData;
279
    ICUDataTable regionData;
280
    SimpleFormatter separatorFormat;
281
    SimpleFormatter format;
282
    SimpleFormatter keyTypeFormat;
283
    UDisplayContext capitalizationContext;
284
#if !UCONFIG_NO_BREAK_ITERATION
285
    BreakIterator* capitalizationBrkIter;
286
#else
287
    UObject* capitalizationBrkIter;
288
#endif
289
    static UMutex  capitalizationBrkIterLock;
290
    UnicodeString formatOpenParen;
291
    UnicodeString formatReplaceOpenParen;
292
    UnicodeString formatCloseParen;
293
    UnicodeString formatReplaceCloseParen;
294
    UDisplayContext nameLength;
295
296
    // Constants for capitalization context usage types.
297
    enum CapContextUsage {
298
        kCapContextUsageLanguage,
299
        kCapContextUsageScript,
300
        kCapContextUsageTerritory,
301
        kCapContextUsageVariant,
302
        kCapContextUsageKey,
303
        kCapContextUsageKeyValue,
304
        kCapContextUsageCount
305
    };
306
    // Capitalization transforms. For each usage type, indicates whether to titlecase for
307
    // the context specified in capitalizationContext (which we know at construction time)
308
     UBool fCapitalization[kCapContextUsageCount];
309
310
public:
311
    // constructor
312
    LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling);
313
    LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length);
314
    virtual ~LocaleDisplayNamesImpl();
315
316
    virtual const Locale& getLocale() const;
317
    virtual UDialectHandling getDialectHandling() const;
318
    virtual UDisplayContext getContext(UDisplayContextType type) const;
319
320
    virtual UnicodeString& localeDisplayName(const Locale& locale,
321
                                                UnicodeString& result) const;
322
    virtual UnicodeString& localeDisplayName(const char* localeId,
323
                                                UnicodeString& result) const;
324
    virtual UnicodeString& languageDisplayName(const char* lang,
325
                                               UnicodeString& result) const;
326
    virtual UnicodeString& scriptDisplayName(const char* script,
327
                                                UnicodeString& result) const;
328
    virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
329
                                                UnicodeString& result) const;
330
    virtual UnicodeString& regionDisplayName(const char* region,
331
                                                UnicodeString& result) const;
332
    virtual UnicodeString& variantDisplayName(const char* variant,
333
                                                UnicodeString& result) const;
334
    virtual UnicodeString& keyDisplayName(const char* key,
335
                                                UnicodeString& result) const;
336
    virtual UnicodeString& keyValueDisplayName(const char* key,
337
                                                const char* value,
338
                                                UnicodeString& result) const;
339
private:
340
    UnicodeString& localeIdName(const char* localeId,
341
                                UnicodeString& result) const;
342
    UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const;
343
    UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const;
344
    UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const;
345
    UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const;
346
    UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const;
347
    UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const;
348
    UnicodeString& keyValueDisplayName(const char* key, const char* value,
349
                                        UnicodeString& result, UBool skipAdjust) const;
350
    void initialize(void);
351
352
    struct CapitalizationContextSink;
353
};
354
355
UMutex LocaleDisplayNamesImpl::capitalizationBrkIterLock = U_MUTEX_INITIALIZER;
356
357
LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
358
                                               UDialectHandling dialectHandling)
359
    : dialectHandling(dialectHandling)
360
    , langData(U_ICUDATA_LANG, locale)
361
    , regionData(U_ICUDATA_REGION, locale)
362
    , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
363
    , capitalizationBrkIter(NULL)
364
    , nameLength(UDISPCTX_LENGTH_FULL)
365
0
{
366
0
    initialize();
367
0
}
368
369
LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
370
                                               UDisplayContext *contexts, int32_t length)
371
    : dialectHandling(ULDN_STANDARD_NAMES)
372
    , langData(U_ICUDATA_LANG, locale)
373
    , regionData(U_ICUDATA_REGION, locale)
374
    , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
375
    , capitalizationBrkIter(NULL)
376
    , nameLength(UDISPCTX_LENGTH_FULL)
377
0
{
378
0
    while (length-- > 0) {
379
0
        UDisplayContext value = *contexts++;
380
0
        UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8);
381
0
        switch (selector) {
382
0
            case UDISPCTX_TYPE_DIALECT_HANDLING:
383
0
                dialectHandling = (UDialectHandling)value;
384
0
                break;
385
0
            case UDISPCTX_TYPE_CAPITALIZATION:
386
0
                capitalizationContext = value;
387
0
                break;
388
0
            case UDISPCTX_TYPE_DISPLAY_LENGTH:
389
0
                nameLength = value;
390
0
                break;
391
0
            default:
392
0
                break;
393
0
        }
394
0
    }
395
0
    initialize();
396
0
}
397
398
struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink {
399
    UBool hasCapitalizationUsage;
400
    LocaleDisplayNamesImpl& parent;
401
402
    CapitalizationContextSink(LocaleDisplayNamesImpl& _parent)
403
0
      : hasCapitalizationUsage(FALSE), parent(_parent) {}
404
    virtual ~CapitalizationContextSink();
405
406
    virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
407
0
            UErrorCode &errorCode) {
408
0
        ResourceTable contexts = value.getTable(errorCode);
409
0
        if (U_FAILURE(errorCode)) { return; }
410
0
        for (int i = 0; contexts.getKeyAndValue(i, key, value); ++i) {
411
0
412
0
            CapContextUsage usageEnum;
413
0
            if (uprv_strcmp(key, "key") == 0) {
414
0
                usageEnum = kCapContextUsageKey;
415
0
            } else if (uprv_strcmp(key, "keyValue") == 0) {
416
0
                usageEnum = kCapContextUsageKeyValue;
417
0
            } else if (uprv_strcmp(key, "languages") == 0) {
418
0
                usageEnum = kCapContextUsageLanguage;
419
0
            } else if (uprv_strcmp(key, "script") == 0) {
420
0
                usageEnum = kCapContextUsageScript;
421
0
            } else if (uprv_strcmp(key, "territory") == 0) {
422
0
                usageEnum = kCapContextUsageTerritory;
423
0
            } else if (uprv_strcmp(key, "variant") == 0) {
424
0
                usageEnum = kCapContextUsageVariant;
425
0
            } else {
426
0
                continue;
427
0
            }
428
0
429
0
            int32_t len = 0;
430
0
            const int32_t* intVector = value.getIntVector(len, errorCode);
431
0
            if (U_FAILURE(errorCode)) { return; }
432
0
            if (len < 2) { continue; }
433
0
434
0
            int32_t titlecaseInt = (parent.capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU) ? intVector[0] : intVector[1];
435
0
            if (titlecaseInt == 0) { continue; }
436
0
437
0
            parent.fCapitalization[usageEnum] = TRUE;
438
0
            hasCapitalizationUsage = TRUE;
439
0
        }
440
0
    }
441
};
442
443
// Virtual destructors must be defined out of line.
444
0
LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
445
446
void
447
0
LocaleDisplayNamesImpl::initialize(void) {
448
0
    LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this;
449
0
    nonConstThis->locale = langData.getLocale() == Locale::getRoot()
450
0
        ? regionData.getLocale()
451
0
        : langData.getLocale();
452
0
453
0
    UnicodeString sep;
454
0
    langData.getNoFallback("localeDisplayPattern", "separator", sep);
455
0
    if (sep.isBogus()) {
456
0
        sep = UnicodeString("{0}, {1}", -1, US_INV);
457
0
    }
458
0
    UErrorCode status = U_ZERO_ERROR;
459
0
    separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status);
460
0
461
0
    UnicodeString pattern;
462
0
    langData.getNoFallback("localeDisplayPattern", "pattern", pattern);
463
0
    if (pattern.isBogus()) {
464
0
        pattern = UnicodeString("{0} ({1})", -1, US_INV);
465
0
    }
466
0
    format.applyPatternMinMaxArguments(pattern, 2, 2, status);
467
0
    if (pattern.indexOf((UChar)0xFF08) >= 0) {
468
0
        formatOpenParen.setTo((UChar)0xFF08);         // fullwidth (
469
0
        formatReplaceOpenParen.setTo((UChar)0xFF3B);  // fullwidth [
470
0
        formatCloseParen.setTo((UChar)0xFF09);        // fullwidth )
471
0
        formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ]
472
0
    } else {
473
0
        formatOpenParen.setTo((UChar)0x0028);         // (
474
0
        formatReplaceOpenParen.setTo((UChar)0x005B);  // [
475
0
        formatCloseParen.setTo((UChar)0x0029);        // )
476
0
        formatReplaceCloseParen.setTo((UChar)0x005D); // ]
477
0
    }
478
0
479
0
    UnicodeString ktPattern;
480
0
    langData.get("localeDisplayPattern", "keyTypePattern", ktPattern);
481
0
    if (ktPattern.isBogus()) {
482
0
        ktPattern = UnicodeString("{0}={1}", -1, US_INV);
483
0
    }
484
0
    keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status);
485
0
486
0
    uprv_memset(fCapitalization, 0, sizeof(fCapitalization));
487
0
#if !UCONFIG_NO_BREAK_ITERATION
488
0
    // Only get the context data if we need it! This is a const object so we know now...
489
0
    // Also check whether we will need a break iterator (depends on the data)
490
0
    UBool needBrkIter = FALSE;
491
0
    if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) {
492
0
        LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status));
493
0
        if (U_FAILURE(status)) { return; }
494
0
        CapitalizationContextSink sink(*this);
495
0
        ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status);
496
0
        if (status == U_MISSING_RESOURCE_ERROR) {
497
0
            // Silently ignore.  Not every locale has contextTransforms.
498
0
            status = U_ZERO_ERROR;
499
0
        } else if (U_FAILURE(status)) {
500
0
            return;
501
0
        }
502
0
        needBrkIter = sink.hasCapitalizationUsage;
503
0
    }
504
0
    // Get a sentence break iterator if we will need it
505
0
    if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
506
0
        status = U_ZERO_ERROR;
507
0
        capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status);
508
0
        if (U_FAILURE(status)) {
509
0
            delete capitalizationBrkIter;
510
0
            capitalizationBrkIter = NULL;
511
0
        }
512
0
    }
513
0
#endif
514
0
}
515
516
0
LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
517
0
#if !UCONFIG_NO_BREAK_ITERATION
518
0
    delete capitalizationBrkIter;
519
0
#endif
520
0
}
521
522
const Locale&
523
0
LocaleDisplayNamesImpl::getLocale() const {
524
0
    return locale;
525
0
}
526
527
UDialectHandling
528
0
LocaleDisplayNamesImpl::getDialectHandling() const {
529
0
    return dialectHandling;
530
0
}
531
532
UDisplayContext
533
0
LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const {
534
0
    switch (type) {
535
0
        case UDISPCTX_TYPE_DIALECT_HANDLING:
536
0
            return (UDisplayContext)dialectHandling;
537
0
        case UDISPCTX_TYPE_CAPITALIZATION:
538
0
            return capitalizationContext;
539
0
        case UDISPCTX_TYPE_DISPLAY_LENGTH:
540
0
            return nameLength;
541
0
        default:
542
0
            break;
543
0
    }
544
0
    return (UDisplayContext)0;
545
0
}
546
547
UnicodeString&
548
LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
549
0
                                                UnicodeString& result) const {
550
0
#if !UCONFIG_NO_BREAK_ITERATION
551
0
    // check to see whether we need to titlecase result
552
0
    if ( result.length() > 0 && u_islower(result.char32At(0)) && capitalizationBrkIter!= NULL &&
553
0
          ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || fCapitalization[usage] ) ) {
554
0
        // note fCapitalization[usage] won't be set unless capitalizationContext is UI_LIST_OR_MENU or STANDALONE
555
0
        Mutex lock(&capitalizationBrkIterLock);
556
0
        result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
557
0
    }
558
0
#endif
559
0
    return result;
560
0
}
561
562
UnicodeString&
563
LocaleDisplayNamesImpl::localeDisplayName(const Locale& locale,
564
0
                                          UnicodeString& result) const {
565
0
  if (locale.isBogus()) {
566
0
    result.setToBogus();
567
0
    return result;
568
0
  }
569
0
  UnicodeString resultName;
570
0
571
0
  const char* lang = locale.getLanguage();
572
0
  if (uprv_strlen(lang) == 0) {
573
0
    lang = "root";
574
0
  }
575
0
  const char* script = locale.getScript();
576
0
  const char* country = locale.getCountry();
577
0
  const char* variant = locale.getVariant();
578
0
579
0
  UBool hasScript = uprv_strlen(script) > 0;
580
0
  UBool hasCountry = uprv_strlen(country) > 0;
581
0
  UBool hasVariant = uprv_strlen(variant) > 0;
582
0
583
0
  if (dialectHandling == ULDN_DIALECT_NAMES) {
584
0
    char buffer[ULOC_FULLNAME_CAPACITY];
585
0
    do { // loop construct is so we can break early out of search
586
0
      if (hasScript && hasCountry) {
587
0
        ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0);
588
0
        localeIdName(buffer, resultName);
589
0
        if (!resultName.isBogus()) {
590
0
          hasScript = FALSE;
591
0
          hasCountry = FALSE;
592
0
          break;
593
0
        }
594
0
      }
595
0
      if (hasScript) {
596
0
        ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0);
597
0
        localeIdName(buffer, resultName);
598
0
        if (!resultName.isBogus()) {
599
0
          hasScript = FALSE;
600
0
          break;
601
0
        }
602
0
      }
603
0
      if (hasCountry) {
604
0
        ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0);
605
0
        localeIdName(buffer, resultName);
606
0
        if (!resultName.isBogus()) {
607
0
          hasCountry = FALSE;
608
0
          break;
609
0
        }
610
0
      }
611
0
    } while (FALSE);
612
0
  }
613
0
  if (resultName.isBogus() || resultName.isEmpty()) {
614
0
    localeIdName(lang, resultName);
615
0
  }
616
0
617
0
  UnicodeString resultRemainder;
618
0
  UnicodeString temp;
619
0
  UErrorCode status = U_ZERO_ERROR;
620
0
621
0
  if (hasScript) {
622
0
    resultRemainder.append(scriptDisplayName(script, temp, TRUE));
623
0
  }
624
0
  if (hasCountry) {
625
0
    appendWithSep(resultRemainder, regionDisplayName(country, temp, TRUE));
626
0
  }
627
0
  if (hasVariant) {
628
0
    appendWithSep(resultRemainder, variantDisplayName(variant, temp, TRUE));
629
0
  }
630
0
  resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen);
631
0
  resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen);
632
0
633
0
  LocalPointer<StringEnumeration> e(locale.createKeywords(status));
634
0
  if (e.isValid() && U_SUCCESS(status)) {
635
0
    UnicodeString temp2;
636
0
    char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY
637
0
    const char* key;
638
0
    while ((key = e->next((int32_t *)0, status)) != NULL) {
639
0
      value[0] = 0;
640
0
      locale.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status);
641
0
      if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
642
0
        return result;
643
0
      }
644
0
      keyDisplayName(key, temp, TRUE);
645
0
      temp.findAndReplace(formatOpenParen, formatReplaceOpenParen);
646
0
      temp.findAndReplace(formatCloseParen, formatReplaceCloseParen);
647
0
      keyValueDisplayName(key, value, temp2, TRUE);
648
0
      temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen);
649
0
      temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen);
650
0
      if (temp2 != UnicodeString(value, -1, US_INV)) {
651
0
        appendWithSep(resultRemainder, temp2);
652
0
      } else if (temp != UnicodeString(key, -1, US_INV)) {
653
0
        UnicodeString temp3;
654
0
        keyTypeFormat.format(temp, temp2, temp3, status);
655
0
        appendWithSep(resultRemainder, temp3);
656
0
      } else {
657
0
        appendWithSep(resultRemainder, temp)
658
0
          .append((UChar)0x3d /* = */)
659
0
          .append(temp2);
660
0
      }
661
0
    }
662
0
  }
663
0
664
0
  if (!resultRemainder.isEmpty()) {
665
0
    format.format(resultName, resultRemainder, result.remove(), status);
666
0
    return adjustForUsageAndContext(kCapContextUsageLanguage, result);
667
0
  }
668
0
669
0
  result = resultName;
670
0
  return adjustForUsageAndContext(kCapContextUsageLanguage, result);
671
0
}
672
673
UnicodeString&
674
0
LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const {
675
0
    if (buffer.isEmpty()) {
676
0
        buffer.setTo(src);
677
0
    } else {
678
0
        const UnicodeString *values[2] = { &buffer, &src };
679
0
        UErrorCode status = U_ZERO_ERROR;
680
0
        separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status);
681
0
    }
682
0
    return buffer;
683
0
}
684
685
UnicodeString&
686
LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
687
0
                                          UnicodeString& result) const {
688
0
    return localeDisplayName(Locale(localeId), result);
689
0
}
690
691
// private
692
UnicodeString&
693
LocaleDisplayNamesImpl::localeIdName(const char* localeId,
694
0
                                     UnicodeString& result) const {
695
0
    if (nameLength == UDISPCTX_LENGTH_SHORT) {
696
0
        langData.getNoFallback("Languages%short", localeId, result);
697
0
        if (!result.isBogus()) {
698
0
            return result;
699
0
        }
700
0
    }
701
0
    return langData.getNoFallback("Languages", localeId, result);
702
0
}
703
704
UnicodeString&
705
LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
706
0
                                            UnicodeString& result) const {
707
0
    if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
708
0
        return result = UnicodeString(lang, -1, US_INV);
709
0
    }
710
0
    if (nameLength == UDISPCTX_LENGTH_SHORT) {
711
0
        langData.get("Languages%short", lang, result);
712
0
        if (!result.isBogus()) {
713
0
            return adjustForUsageAndContext(kCapContextUsageLanguage, result);
714
0
        }
715
0
    }
716
0
    langData.get("Languages", lang, result);
717
0
    return adjustForUsageAndContext(kCapContextUsageLanguage, result);
718
0
}
719
720
UnicodeString&
721
LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
722
                                          UnicodeString& result,
723
0
                                          UBool skipAdjust) const {
724
0
    if (nameLength == UDISPCTX_LENGTH_SHORT) {
725
0
        langData.get("Scripts%short", script, result);
726
0
        if (!result.isBogus()) {
727
0
            return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
728
0
        }
729
0
    }
730
0
    langData.get("Scripts", script, result);
731
0
    return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
732
0
}
733
734
UnicodeString&
735
LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
736
0
                                          UnicodeString& result) const {
737
0
    return scriptDisplayName(script, result, FALSE);
738
0
}
739
740
UnicodeString&
741
LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
742
0
                                          UnicodeString& result) const {
743
0
    return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
744
0
}
745
746
UnicodeString&
747
LocaleDisplayNamesImpl::regionDisplayName(const char* region,
748
                                          UnicodeString& result,
749
0
                                          UBool skipAdjust) const {
750
0
    if (nameLength == UDISPCTX_LENGTH_SHORT) {
751
0
        regionData.get("Countries%short", region, result);
752
0
        if (!result.isBogus()) {
753
0
            return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
754
0
        }
755
0
    }
756
0
    regionData.get("Countries", region, result);
757
0
    return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
758
0
}
759
760
UnicodeString&
761
LocaleDisplayNamesImpl::regionDisplayName(const char* region,
762
0
                                          UnicodeString& result) const {
763
0
    return regionDisplayName(region, result, FALSE);
764
0
}
765
766
767
UnicodeString&
768
LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
769
                                           UnicodeString& result,
770
0
                                           UBool skipAdjust) const {
771
0
    // don't have a resource for short variant names
772
0
    langData.get("Variants", variant, result);
773
0
    return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
774
0
}
775
776
UnicodeString&
777
LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
778
0
                                           UnicodeString& result) const {
779
0
    return variantDisplayName(variant, result, FALSE);
780
0
}
781
782
UnicodeString&
783
LocaleDisplayNamesImpl::keyDisplayName(const char* key,
784
                                       UnicodeString& result,
785
0
                                       UBool skipAdjust) const {
786
0
    // don't have a resource for short key names
787
0
    langData.get("Keys", key, result);
788
0
    return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
789
0
}
790
791
UnicodeString&
792
LocaleDisplayNamesImpl::keyDisplayName(const char* key,
793
0
                                       UnicodeString& result) const {
794
0
    return keyDisplayName(key, result, FALSE);
795
0
}
796
797
UnicodeString&
798
LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
799
                                            const char* value,
800
                                            UnicodeString& result,
801
0
                                            UBool skipAdjust) const {
802
0
    if (uprv_strcmp(key, "currency") == 0) {
803
0
        // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
804
0
        UErrorCode sts = U_ZERO_ERROR;
805
0
        UnicodeString ustrValue(value, -1, US_INV);
806
0
        int32_t len;
807
0
        UBool isChoice = FALSE;
808
0
        const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
809
0
            locale.getBaseName(), UCURR_LONG_NAME, &isChoice, &len, &sts);
810
0
        if (U_FAILURE(sts)) {
811
0
            // Return the value as is on failure
812
0
            result = ustrValue;
813
0
            return result;
814
0
        }
815
0
        result.setTo(currencyName, len);
816
0
        return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
817
0
    }
818
0
819
0
    if (nameLength == UDISPCTX_LENGTH_SHORT) {
820
0
        langData.get("Types%short", key, value, result);
821
0
        if (!result.isBogus()) {
822
0
            return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
823
0
        }
824
0
    }
825
0
    langData.get("Types", key, value, result);
826
0
    return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
827
0
}
828
829
UnicodeString&
830
LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
831
                                            const char* value,
832
0
                                            UnicodeString& result) const {
833
0
    return keyValueDisplayName(key, value, result, FALSE);
834
0
}
835
836
////////////////////////////////////////////////////////////////////////////////////////////////////
837
838
LocaleDisplayNames*
839
LocaleDisplayNames::createInstance(const Locale& locale,
840
0
                                   UDialectHandling dialectHandling) {
841
0
    return new LocaleDisplayNamesImpl(locale, dialectHandling);
842
0
}
843
844
LocaleDisplayNames*
845
LocaleDisplayNames::createInstance(const Locale& locale,
846
0
                                   UDisplayContext *contexts, int32_t length) {
847
0
    if (contexts == NULL) {
848
0
        length = 0;
849
0
    }
850
0
    return new LocaleDisplayNamesImpl(locale, contexts, length);
851
0
}
852
853
U_NAMESPACE_END
854
855
////////////////////////////////////////////////////////////////////////////////////////////////////
856
857
U_NAMESPACE_USE
858
859
U_CAPI ULocaleDisplayNames * U_EXPORT2
860
uldn_open(const char * locale,
861
          UDialectHandling dialectHandling,
862
0
          UErrorCode *pErrorCode) {
863
0
  if (U_FAILURE(*pErrorCode)) {
864
0
    return 0;
865
0
  }
866
0
  if (locale == NULL) {
867
0
    locale = uloc_getDefault();
868
0
  }
869
0
  return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
870
0
}
871
872
U_CAPI ULocaleDisplayNames * U_EXPORT2
873
uldn_openForContext(const char * locale,
874
                    UDisplayContext *contexts, int32_t length,
875
0
                    UErrorCode *pErrorCode) {
876
0
  if (U_FAILURE(*pErrorCode)) {
877
0
    return 0;
878
0
  }
879
0
  if (locale == NULL) {
880
0
    locale = uloc_getDefault();
881
0
  }
882
0
  return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
883
0
}
884
885
886
U_CAPI void U_EXPORT2
887
0
uldn_close(ULocaleDisplayNames *ldn) {
888
0
  delete (LocaleDisplayNames *)ldn;
889
0
}
890
891
U_CAPI const char * U_EXPORT2
892
0
uldn_getLocale(const ULocaleDisplayNames *ldn) {
893
0
  if (ldn) {
894
0
    return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
895
0
  }
896
0
  return NULL;
897
0
}
898
899
U_CAPI UDialectHandling U_EXPORT2
900
0
uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
901
0
  if (ldn) {
902
0
    return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
903
0
  }
904
0
  return ULDN_STANDARD_NAMES;
905
0
}
906
907
U_CAPI UDisplayContext U_EXPORT2
908
uldn_getContext(const ULocaleDisplayNames *ldn,
909
              UDisplayContextType type,
910
0
              UErrorCode *pErrorCode) {
911
0
  if (U_FAILURE(*pErrorCode)) {
912
0
    return (UDisplayContext)0;
913
0
  }
914
0
  return ((const LocaleDisplayNames *)ldn)->getContext(type);
915
0
}
916
917
U_CAPI int32_t U_EXPORT2
918
uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
919
                       const char *locale,
920
                       UChar *result,
921
                       int32_t maxResultSize,
922
0
                       UErrorCode *pErrorCode) {
923
0
  if (U_FAILURE(*pErrorCode)) {
924
0
    return 0;
925
0
  }
926
0
  if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
927
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
928
0
    return 0;
929
0
  }
930
0
  UnicodeString temp(result, 0, maxResultSize);
931
0
  ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
932
0
  if (temp.isBogus()) {
933
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
934
0
    return 0;
935
0
  }
936
0
  return temp.extract(result, maxResultSize, *pErrorCode);
937
0
}
938
939
U_CAPI int32_t U_EXPORT2
940
uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
941
                         const char *lang,
942
                         UChar *result,
943
                         int32_t maxResultSize,
944
0
                         UErrorCode *pErrorCode) {
945
0
  if (U_FAILURE(*pErrorCode)) {
946
0
    return 0;
947
0
  }
948
0
  if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
949
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
950
0
    return 0;
951
0
  }
952
0
  UnicodeString temp(result, 0, maxResultSize);
953
0
  ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
954
0
  return temp.extract(result, maxResultSize, *pErrorCode);
955
0
}
956
957
U_CAPI int32_t U_EXPORT2
958
uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
959
                       const char *script,
960
                       UChar *result,
961
                       int32_t maxResultSize,
962
0
                       UErrorCode *pErrorCode) {
963
0
  if (U_FAILURE(*pErrorCode)) {
964
0
    return 0;
965
0
  }
966
0
  if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
967
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
968
0
    return 0;
969
0
  }
970
0
  UnicodeString temp(result, 0, maxResultSize);
971
0
  ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
972
0
  return temp.extract(result, maxResultSize, *pErrorCode);
973
0
}
974
975
U_CAPI int32_t U_EXPORT2
976
uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
977
                           UScriptCode scriptCode,
978
                           UChar *result,
979
                           int32_t maxResultSize,
980
0
                           UErrorCode *pErrorCode) {
981
0
  return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
982
0
}
983
984
U_CAPI int32_t U_EXPORT2
985
uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
986
                       const char *region,
987
                       UChar *result,
988
                       int32_t maxResultSize,
989
0
                       UErrorCode *pErrorCode) {
990
0
  if (U_FAILURE(*pErrorCode)) {
991
0
    return 0;
992
0
  }
993
0
  if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
994
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
995
0
    return 0;
996
0
  }
997
0
  UnicodeString temp(result, 0, maxResultSize);
998
0
  ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
999
0
  return temp.extract(result, maxResultSize, *pErrorCode);
1000
0
}
1001
1002
U_CAPI int32_t U_EXPORT2
1003
uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1004
                        const char *variant,
1005
                        UChar *result,
1006
                        int32_t maxResultSize,
1007
0
                        UErrorCode *pErrorCode) {
1008
0
  if (U_FAILURE(*pErrorCode)) {
1009
0
    return 0;
1010
0
  }
1011
0
  if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1012
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1013
0
    return 0;
1014
0
  }
1015
0
  UnicodeString temp(result, 0, maxResultSize);
1016
0
  ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1017
0
  return temp.extract(result, maxResultSize, *pErrorCode);
1018
0
}
1019
1020
U_CAPI int32_t U_EXPORT2
1021
uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1022
                    const char *key,
1023
                    UChar *result,
1024
                    int32_t maxResultSize,
1025
0
                    UErrorCode *pErrorCode) {
1026
0
  if (U_FAILURE(*pErrorCode)) {
1027
0
    return 0;
1028
0
  }
1029
0
  if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1030
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1031
0
    return 0;
1032
0
  }
1033
0
  UnicodeString temp(result, 0, maxResultSize);
1034
0
  ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1035
0
  return temp.extract(result, maxResultSize, *pErrorCode);
1036
0
}
1037
1038
U_CAPI int32_t U_EXPORT2
1039
uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1040
                         const char *key,
1041
                         const char *value,
1042
                         UChar *result,
1043
                         int32_t maxResultSize,
1044
0
                         UErrorCode *pErrorCode) {
1045
0
  if (U_FAILURE(*pErrorCode)) {
1046
0
    return 0;
1047
0
  }
1048
0
  if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1049
0
      || maxResultSize < 0) {
1050
0
    *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1051
0
    return 0;
1052
0
  }
1053
0
  UnicodeString temp(result, 0, maxResultSize);
1054
0
  ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1055
0
  return temp.extract(result, maxResultSize, *pErrorCode);
1056
0
}
1057
1058
#endif