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  */
ncat(char * buffer,uint32_t buflen,...)37 static int32_t ncat(char *buffer, uint32_t buflen, ...) {
38   va_list args;
39   char *str;
40   char *p = buffer;
41   const char* e = buffer + buflen - 1;
42 
43   if (buffer == NULL || buflen < 1) {
44     return -1;
45   }
46 
47   va_start(args, buflen);
48   while ((str = va_arg(args, char *)) != 0) {
49     char c;
50     while (p != e && (c = *str++) != 0) {
51       *p++ = c;
52     }
53   }
54   *p = 0;
55   va_end(args);
56 
57   return static_cast<int32_t>(p - buffer);
58 }
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 &
get(const char * tableKey,const char * itemKey,UnicodeString & result) const88 ICUDataTable::get(const char* tableKey, const char* itemKey, UnicodeString& result) const {
89     return get(tableKey, NULL, itemKey, result);
90 }
91 
92 inline UnicodeString &
getNoFallback(const char * tableKey,const char * itemKey,UnicodeString & result) const93 ICUDataTable::getNoFallback(const char* tableKey, const char* itemKey, UnicodeString& result) const {
94     return getNoFallback(tableKey, NULL, itemKey, result);
95 }
96 
ICUDataTable(const char * path,const Locale & locale)97 ICUDataTable::ICUDataTable(const char* path, const Locale& locale)
98     : path(NULL), locale(Locale::getRoot())
99 {
100   if (path) {
101     int32_t len = static_cast<int32_t>(uprv_strlen(path));
102     this->path = (const char*) uprv_malloc(len + 1);
103     if (this->path) {
104       uprv_strcpy((char *)this->path, path);
105       this->locale = locale;
106     }
107   }
108 }
109 
~ICUDataTable()110 ICUDataTable::~ICUDataTable() {
111   if (path) {
112     uprv_free((void*) path);
113     path = NULL;
114   }
115 }
116 
117 const Locale&
getLocale()118 ICUDataTable::getLocale() {
119   return locale;
120 }
121 
122 UnicodeString &
get(const char * tableKey,const char * subTableKey,const char * itemKey,UnicodeString & result) const123 ICUDataTable::get(const char* tableKey, const char* subTableKey, const char* itemKey,
124                   UnicodeString &result) const {
125   UErrorCode status = U_ZERO_ERROR;
126   int32_t len = 0;
127 
128   const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
129                                                    tableKey, subTableKey, itemKey,
130                                                    &len, &status);
131   if (U_SUCCESS(status) && len > 0) {
132     return result.setTo(s, len);
133   }
134   return result.setTo(UnicodeString(itemKey, -1, US_INV));
135 }
136 
137 UnicodeString &
getNoFallback(const char * tableKey,const char * subTableKey,const char * itemKey,UnicodeString & result) const138 ICUDataTable::getNoFallback(const char* tableKey, const char* subTableKey, const char* itemKey,
139                             UnicodeString& result) const {
140   UErrorCode status = U_ZERO_ERROR;
141   int32_t len = 0;
142 
143   const UChar *s = uloc_getTableStringWithFallback(path, locale.getName(),
144                                                    tableKey, subTableKey, itemKey,
145                                                    &len, &status);
146   if (U_SUCCESS(status)) {
147     return result.setTo(s, len);
148   }
149 
150   result.setToBogus();
151   return result;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////////////////////////
155 
~LocaleDisplayNames()156 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     UnicodeString formatOpenParen;
290     UnicodeString formatReplaceOpenParen;
291     UnicodeString formatCloseParen;
292     UnicodeString formatReplaceCloseParen;
293     UDisplayContext nameLength;
294 
295     // Constants for capitalization context usage types.
296     enum CapContextUsage {
297         kCapContextUsageLanguage,
298         kCapContextUsageScript,
299         kCapContextUsageTerritory,
300         kCapContextUsageVariant,
301         kCapContextUsageKey,
302         kCapContextUsageKeyValue,
303         kCapContextUsageCount
304     };
305     // Capitalization transforms. For each usage type, indicates whether to titlecase for
306     // the context specified in capitalizationContext (which we know at construction time)
307      UBool fCapitalization[kCapContextUsageCount];
308 
309 public:
310     // constructor
311     LocaleDisplayNamesImpl(const Locale& locale, UDialectHandling dialectHandling);
312     LocaleDisplayNamesImpl(const Locale& locale, UDisplayContext *contexts, int32_t length);
313     virtual ~LocaleDisplayNamesImpl();
314 
315     virtual const Locale& getLocale() const;
316     virtual UDialectHandling getDialectHandling() const;
317     virtual UDisplayContext getContext(UDisplayContextType type) const;
318 
319     virtual UnicodeString& localeDisplayName(const Locale& locale,
320                                                 UnicodeString& result) const;
321     virtual UnicodeString& localeDisplayName(const char* localeId,
322                                                 UnicodeString& result) const;
323     virtual UnicodeString& languageDisplayName(const char* lang,
324                                                UnicodeString& result) const;
325     virtual UnicodeString& scriptDisplayName(const char* script,
326                                                 UnicodeString& result) const;
327     virtual UnicodeString& scriptDisplayName(UScriptCode scriptCode,
328                                                 UnicodeString& result) const;
329     virtual UnicodeString& regionDisplayName(const char* region,
330                                                 UnicodeString& result) const;
331     virtual UnicodeString& variantDisplayName(const char* variant,
332                                                 UnicodeString& result) const;
333     virtual UnicodeString& keyDisplayName(const char* key,
334                                                 UnicodeString& result) const;
335     virtual UnicodeString& keyValueDisplayName(const char* key,
336                                                 const char* value,
337                                                 UnicodeString& result) const;
338 private:
339     UnicodeString& localeIdName(const char* localeId,
340                                 UnicodeString& result) const;
341     UnicodeString& appendWithSep(UnicodeString& buffer, const UnicodeString& src) const;
342     UnicodeString& adjustForUsageAndContext(CapContextUsage usage, UnicodeString& result) const;
343     UnicodeString& scriptDisplayName(const char* script, UnicodeString& result, UBool skipAdjust) const;
344     UnicodeString& regionDisplayName(const char* region, UnicodeString& result, UBool skipAdjust) const;
345     UnicodeString& variantDisplayName(const char* variant, UnicodeString& result, UBool skipAdjust) const;
346     UnicodeString& keyDisplayName(const char* key, UnicodeString& result, UBool skipAdjust) const;
347     UnicodeString& keyValueDisplayName(const char* key, const char* value,
348                                         UnicodeString& result, UBool skipAdjust) const;
349     void initialize(void);
350 
351     struct CapitalizationContextSink;
352 };
353 
LocaleDisplayNamesImpl(const Locale & locale,UDialectHandling dialectHandling)354 LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
355                                                UDialectHandling dialectHandling)
356     : dialectHandling(dialectHandling)
357     , langData(U_ICUDATA_LANG, locale)
358     , regionData(U_ICUDATA_REGION, locale)
359     , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
360     , capitalizationBrkIter(NULL)
361     , nameLength(UDISPCTX_LENGTH_FULL)
362 {
363     initialize();
364 }
365 
LocaleDisplayNamesImpl(const Locale & locale,UDisplayContext * contexts,int32_t length)366 LocaleDisplayNamesImpl::LocaleDisplayNamesImpl(const Locale& locale,
367                                                UDisplayContext *contexts, int32_t length)
368     : dialectHandling(ULDN_STANDARD_NAMES)
369     , langData(U_ICUDATA_LANG, locale)
370     , regionData(U_ICUDATA_REGION, locale)
371     , capitalizationContext(UDISPCTX_CAPITALIZATION_NONE)
372     , capitalizationBrkIter(NULL)
373     , nameLength(UDISPCTX_LENGTH_FULL)
374 {
375     while (length-- > 0) {
376         UDisplayContext value = *contexts++;
377         UDisplayContextType selector = (UDisplayContextType)((uint32_t)value >> 8);
378         switch (selector) {
379             case UDISPCTX_TYPE_DIALECT_HANDLING:
380                 dialectHandling = (UDialectHandling)value;
381                 break;
382             case UDISPCTX_TYPE_CAPITALIZATION:
383                 capitalizationContext = value;
384                 break;
385             case UDISPCTX_TYPE_DISPLAY_LENGTH:
386                 nameLength = value;
387                 break;
388             default:
389                 break;
390         }
391     }
392     initialize();
393 }
394 
395 struct LocaleDisplayNamesImpl::CapitalizationContextSink : public ResourceSink {
396     UBool hasCapitalizationUsage;
397     LocaleDisplayNamesImpl& parent;
398 
CapitalizationContextSinkLocaleDisplayNamesImpl::CapitalizationContextSink399     CapitalizationContextSink(LocaleDisplayNamesImpl& _parent)
400       : hasCapitalizationUsage(FALSE), parent(_parent) {}
401     virtual ~CapitalizationContextSink();
402 
putLocaleDisplayNamesImpl::CapitalizationContextSink403     virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
404             UErrorCode &errorCode) {
405         ResourceTable contexts = value.getTable(errorCode);
406         if (U_FAILURE(errorCode)) { return; }
407         for (int i = 0; contexts.getKeyAndValue(i, key, value); ++i) {
408 
409             CapContextUsage usageEnum;
410             if (uprv_strcmp(key, "key") == 0) {
411                 usageEnum = kCapContextUsageKey;
412             } else if (uprv_strcmp(key, "keyValue") == 0) {
413                 usageEnum = kCapContextUsageKeyValue;
414             } else if (uprv_strcmp(key, "languages") == 0) {
415                 usageEnum = kCapContextUsageLanguage;
416             } else if (uprv_strcmp(key, "script") == 0) {
417                 usageEnum = kCapContextUsageScript;
418             } else if (uprv_strcmp(key, "territory") == 0) {
419                 usageEnum = kCapContextUsageTerritory;
420             } else if (uprv_strcmp(key, "variant") == 0) {
421                 usageEnum = kCapContextUsageVariant;
422             } else {
423                 continue;
424             }
425 
426             int32_t len = 0;
427             const int32_t* intVector = value.getIntVector(len, errorCode);
428             if (U_FAILURE(errorCode)) { return; }
429             if (len < 2) { continue; }
430 
431             int32_t titlecaseInt = (parent.capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU) ? intVector[0] : intVector[1];
432             if (titlecaseInt == 0) { continue; }
433 
434             parent.fCapitalization[usageEnum] = TRUE;
435             hasCapitalizationUsage = TRUE;
436         }
437     }
438 };
439 
440 // Virtual destructors must be defined out of line.
~CapitalizationContextSink()441 LocaleDisplayNamesImpl::CapitalizationContextSink::~CapitalizationContextSink() {}
442 
443 void
initialize(void)444 LocaleDisplayNamesImpl::initialize(void) {
445     LocaleDisplayNamesImpl *nonConstThis = (LocaleDisplayNamesImpl *)this;
446     nonConstThis->locale = langData.getLocale() == Locale::getRoot()
447         ? regionData.getLocale()
448         : langData.getLocale();
449 
450     UnicodeString sep;
451     langData.getNoFallback("localeDisplayPattern", "separator", sep);
452     if (sep.isBogus()) {
453         sep = UnicodeString("{0}, {1}", -1, US_INV);
454     }
455     UErrorCode status = U_ZERO_ERROR;
456     separatorFormat.applyPatternMinMaxArguments(sep, 2, 2, status);
457 
458     UnicodeString pattern;
459     langData.getNoFallback("localeDisplayPattern", "pattern", pattern);
460     if (pattern.isBogus()) {
461         pattern = UnicodeString("{0} ({1})", -1, US_INV);
462     }
463     format.applyPatternMinMaxArguments(pattern, 2, 2, status);
464     if (pattern.indexOf((UChar)0xFF08) >= 0) {
465         formatOpenParen.setTo((UChar)0xFF08);         // fullwidth (
466         formatReplaceOpenParen.setTo((UChar)0xFF3B);  // fullwidth [
467         formatCloseParen.setTo((UChar)0xFF09);        // fullwidth )
468         formatReplaceCloseParen.setTo((UChar)0xFF3D); // fullwidth ]
469     } else {
470         formatOpenParen.setTo((UChar)0x0028);         // (
471         formatReplaceOpenParen.setTo((UChar)0x005B);  // [
472         formatCloseParen.setTo((UChar)0x0029);        // )
473         formatReplaceCloseParen.setTo((UChar)0x005D); // ]
474     }
475 
476     UnicodeString ktPattern;
477     langData.get("localeDisplayPattern", "keyTypePattern", ktPattern);
478     if (ktPattern.isBogus()) {
479         ktPattern = UnicodeString("{0}={1}", -1, US_INV);
480     }
481     keyTypeFormat.applyPatternMinMaxArguments(ktPattern, 2, 2, status);
482 
483     uprv_memset(fCapitalization, 0, sizeof(fCapitalization));
484 #if !UCONFIG_NO_BREAK_ITERATION
485     // Only get the context data if we need it! This is a const object so we know now...
486     // Also check whether we will need a break iterator (depends on the data)
487     UBool needBrkIter = FALSE;
488     if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_STANDALONE) {
489         LocalUResourceBundlePointer resource(ures_open(NULL, locale.getName(), &status));
490         if (U_FAILURE(status)) { return; }
491         CapitalizationContextSink sink(*this);
492         ures_getAllItemsWithFallback(resource.getAlias(), "contextTransforms", sink, status);
493         if (status == U_MISSING_RESOURCE_ERROR) {
494             // Silently ignore.  Not every locale has contextTransforms.
495             status = U_ZERO_ERROR;
496         } else if (U_FAILURE(status)) {
497             return;
498         }
499         needBrkIter = sink.hasCapitalizationUsage;
500     }
501     // Get a sentence break iterator if we will need it
502     if (needBrkIter || capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
503         status = U_ZERO_ERROR;
504         capitalizationBrkIter = BreakIterator::createSentenceInstance(locale, status);
505         if (U_FAILURE(status)) {
506             delete capitalizationBrkIter;
507             capitalizationBrkIter = NULL;
508         }
509     }
510 #endif
511 }
512 
~LocaleDisplayNamesImpl()513 LocaleDisplayNamesImpl::~LocaleDisplayNamesImpl() {
514 #if !UCONFIG_NO_BREAK_ITERATION
515     delete capitalizationBrkIter;
516 #endif
517 }
518 
519 const Locale&
getLocale() const520 LocaleDisplayNamesImpl::getLocale() const {
521     return locale;
522 }
523 
524 UDialectHandling
getDialectHandling() const525 LocaleDisplayNamesImpl::getDialectHandling() const {
526     return dialectHandling;
527 }
528 
529 UDisplayContext
getContext(UDisplayContextType type) const530 LocaleDisplayNamesImpl::getContext(UDisplayContextType type) const {
531     switch (type) {
532         case UDISPCTX_TYPE_DIALECT_HANDLING:
533             return (UDisplayContext)dialectHandling;
534         case UDISPCTX_TYPE_CAPITALIZATION:
535             return capitalizationContext;
536         case UDISPCTX_TYPE_DISPLAY_LENGTH:
537             return nameLength;
538         default:
539             break;
540     }
541     return (UDisplayContext)0;
542 }
543 
544 UnicodeString&
adjustForUsageAndContext(CapContextUsage usage,UnicodeString & result) const545 LocaleDisplayNamesImpl::adjustForUsageAndContext(CapContextUsage usage,
546                                                 UnicodeString& result) const {
547 #if !UCONFIG_NO_BREAK_ITERATION
548     // check to see whether we need to titlecase result
549     if ( result.length() > 0 && u_islower(result.char32At(0)) && capitalizationBrkIter!= NULL &&
550           ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE || fCapitalization[usage] ) ) {
551         // note fCapitalization[usage] won't be set unless capitalizationContext is UI_LIST_OR_MENU or STANDALONE
552         static UMutex capitalizationBrkIterLock = U_MUTEX_INITIALIZER;
553         Mutex lock(&capitalizationBrkIterLock);
554         result.toTitle(capitalizationBrkIter, locale, U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
555     }
556 #endif
557     return result;
558 }
559 
560 UnicodeString&
localeDisplayName(const Locale & loc,UnicodeString & result) const561 LocaleDisplayNamesImpl::localeDisplayName(const Locale& loc,
562                                           UnicodeString& result) const {
563   if (loc.isBogus()) {
564     result.setToBogus();
565     return result;
566   }
567   UnicodeString resultName;
568 
569   const char* lang = loc.getLanguage();
570   if (uprv_strlen(lang) == 0) {
571     lang = "root";
572   }
573   const char* script = loc.getScript();
574   const char* country = loc.getCountry();
575   const char* variant = loc.getVariant();
576 
577   UBool hasScript = uprv_strlen(script) > 0;
578   UBool hasCountry = uprv_strlen(country) > 0;
579   UBool hasVariant = uprv_strlen(variant) > 0;
580 
581   if (dialectHandling == ULDN_DIALECT_NAMES) {
582     char buffer[ULOC_FULLNAME_CAPACITY];
583     do { // loop construct is so we can break early out of search
584       if (hasScript && hasCountry) {
585         ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, "_", country, (char *)0);
586         localeIdName(buffer, resultName);
587         if (!resultName.isBogus()) {
588           hasScript = FALSE;
589           hasCountry = FALSE;
590           break;
591         }
592       }
593       if (hasScript) {
594         ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", script, (char *)0);
595         localeIdName(buffer, resultName);
596         if (!resultName.isBogus()) {
597           hasScript = FALSE;
598           break;
599         }
600       }
601       if (hasCountry) {
602         ncat(buffer, ULOC_FULLNAME_CAPACITY, lang, "_", country, (char*)0);
603         localeIdName(buffer, resultName);
604         if (!resultName.isBogus()) {
605           hasCountry = FALSE;
606           break;
607         }
608       }
609     } while (FALSE);
610   }
611   if (resultName.isBogus() || resultName.isEmpty()) {
612     localeIdName(lang, resultName);
613   }
614 
615   UnicodeString resultRemainder;
616   UnicodeString temp;
617   UErrorCode status = U_ZERO_ERROR;
618 
619   if (hasScript) {
620     resultRemainder.append(scriptDisplayName(script, temp, TRUE));
621   }
622   if (hasCountry) {
623     appendWithSep(resultRemainder, regionDisplayName(country, temp, TRUE));
624   }
625   if (hasVariant) {
626     appendWithSep(resultRemainder, variantDisplayName(variant, temp, TRUE));
627   }
628   resultRemainder.findAndReplace(formatOpenParen, formatReplaceOpenParen);
629   resultRemainder.findAndReplace(formatCloseParen, formatReplaceCloseParen);
630 
631   LocalPointer<StringEnumeration> e(loc.createKeywords(status));
632   if (e.isValid() && U_SUCCESS(status)) {
633     UnicodeString temp2;
634     char value[ULOC_KEYWORD_AND_VALUES_CAPACITY]; // sigh, no ULOC_VALUE_CAPACITY
635     const char* key;
636     while ((key = e->next((int32_t *)0, status)) != NULL) {
637       value[0] = 0;
638       loc.getKeywordValue(key, value, ULOC_KEYWORD_AND_VALUES_CAPACITY, status);
639       if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING) {
640         return result;
641       }
642       keyDisplayName(key, temp, TRUE);
643       temp.findAndReplace(formatOpenParen, formatReplaceOpenParen);
644       temp.findAndReplace(formatCloseParen, formatReplaceCloseParen);
645       keyValueDisplayName(key, value, temp2, TRUE);
646       temp2.findAndReplace(formatOpenParen, formatReplaceOpenParen);
647       temp2.findAndReplace(formatCloseParen, formatReplaceCloseParen);
648       if (temp2 != UnicodeString(value, -1, US_INV)) {
649         appendWithSep(resultRemainder, temp2);
650       } else if (temp != UnicodeString(key, -1, US_INV)) {
651         UnicodeString temp3;
652         keyTypeFormat.format(temp, temp2, temp3, status);
653         appendWithSep(resultRemainder, temp3);
654       } else {
655         appendWithSep(resultRemainder, temp)
656           .append((UChar)0x3d /* = */)
657           .append(temp2);
658       }
659     }
660   }
661 
662   if (!resultRemainder.isEmpty()) {
663     format.format(resultName, resultRemainder, result.remove(), status);
664     return adjustForUsageAndContext(kCapContextUsageLanguage, result);
665   }
666 
667   result = resultName;
668   return adjustForUsageAndContext(kCapContextUsageLanguage, result);
669 }
670 
671 UnicodeString&
appendWithSep(UnicodeString & buffer,const UnicodeString & src) const672 LocaleDisplayNamesImpl::appendWithSep(UnicodeString& buffer, const UnicodeString& src) const {
673     if (buffer.isEmpty()) {
674         buffer.setTo(src);
675     } else {
676         const UnicodeString *values[2] = { &buffer, &src };
677         UErrorCode status = U_ZERO_ERROR;
678         separatorFormat.formatAndReplace(values, 2, buffer, NULL, 0, status);
679     }
680     return buffer;
681 }
682 
683 UnicodeString&
localeDisplayName(const char * localeId,UnicodeString & result) const684 LocaleDisplayNamesImpl::localeDisplayName(const char* localeId,
685                                           UnicodeString& result) const {
686     return localeDisplayName(Locale(localeId), result);
687 }
688 
689 // private
690 UnicodeString&
localeIdName(const char * localeId,UnicodeString & result) const691 LocaleDisplayNamesImpl::localeIdName(const char* localeId,
692                                      UnicodeString& result) const {
693     if (nameLength == UDISPCTX_LENGTH_SHORT) {
694         langData.getNoFallback("Languages%short", localeId, result);
695         if (!result.isBogus()) {
696             return result;
697         }
698     }
699     return langData.getNoFallback("Languages", localeId, result);
700 }
701 
702 UnicodeString&
languageDisplayName(const char * lang,UnicodeString & result) const703 LocaleDisplayNamesImpl::languageDisplayName(const char* lang,
704                                             UnicodeString& result) const {
705     if (uprv_strcmp("root", lang) == 0 || uprv_strchr(lang, '_') != NULL) {
706         return result = UnicodeString(lang, -1, US_INV);
707     }
708     if (nameLength == UDISPCTX_LENGTH_SHORT) {
709         langData.get("Languages%short", lang, result);
710         if (!result.isBogus()) {
711             return adjustForUsageAndContext(kCapContextUsageLanguage, result);
712         }
713     }
714     langData.get("Languages", lang, result);
715     return adjustForUsageAndContext(kCapContextUsageLanguage, result);
716 }
717 
718 UnicodeString&
scriptDisplayName(const char * script,UnicodeString & result,UBool skipAdjust) const719 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
720                                           UnicodeString& result,
721                                           UBool skipAdjust) const {
722     if (nameLength == UDISPCTX_LENGTH_SHORT) {
723         langData.get("Scripts%short", script, result);
724         if (!result.isBogus()) {
725             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
726         }
727     }
728     langData.get("Scripts", script, result);
729     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageScript, result);
730 }
731 
732 UnicodeString&
scriptDisplayName(const char * script,UnicodeString & result) const733 LocaleDisplayNamesImpl::scriptDisplayName(const char* script,
734                                           UnicodeString& result) const {
735     return scriptDisplayName(script, result, FALSE);
736 }
737 
738 UnicodeString&
scriptDisplayName(UScriptCode scriptCode,UnicodeString & result) const739 LocaleDisplayNamesImpl::scriptDisplayName(UScriptCode scriptCode,
740                                           UnicodeString& result) const {
741     return scriptDisplayName(uscript_getName(scriptCode), result, FALSE);
742 }
743 
744 UnicodeString&
regionDisplayName(const char * region,UnicodeString & result,UBool skipAdjust) const745 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
746                                           UnicodeString& result,
747                                           UBool skipAdjust) const {
748     if (nameLength == UDISPCTX_LENGTH_SHORT) {
749         regionData.get("Countries%short", region, result);
750         if (!result.isBogus()) {
751             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
752         }
753     }
754     regionData.get("Countries", region, result);
755     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageTerritory, result);
756 }
757 
758 UnicodeString&
regionDisplayName(const char * region,UnicodeString & result) const759 LocaleDisplayNamesImpl::regionDisplayName(const char* region,
760                                           UnicodeString& result) const {
761     return regionDisplayName(region, result, FALSE);
762 }
763 
764 
765 UnicodeString&
variantDisplayName(const char * variant,UnicodeString & result,UBool skipAdjust) const766 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
767                                            UnicodeString& result,
768                                            UBool skipAdjust) const {
769     // don't have a resource for short variant names
770     langData.get("Variants", variant, result);
771     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageVariant, result);
772 }
773 
774 UnicodeString&
variantDisplayName(const char * variant,UnicodeString & result) const775 LocaleDisplayNamesImpl::variantDisplayName(const char* variant,
776                                            UnicodeString& result) const {
777     return variantDisplayName(variant, result, FALSE);
778 }
779 
780 UnicodeString&
keyDisplayName(const char * key,UnicodeString & result,UBool skipAdjust) const781 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
782                                        UnicodeString& result,
783                                        UBool skipAdjust) const {
784     // don't have a resource for short key names
785     langData.get("Keys", key, result);
786     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKey, result);
787 }
788 
789 UnicodeString&
keyDisplayName(const char * key,UnicodeString & result) const790 LocaleDisplayNamesImpl::keyDisplayName(const char* key,
791                                        UnicodeString& result) const {
792     return keyDisplayName(key, result, FALSE);
793 }
794 
795 UnicodeString&
keyValueDisplayName(const char * key,const char * value,UnicodeString & result,UBool skipAdjust) const796 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
797                                             const char* value,
798                                             UnicodeString& result,
799                                             UBool skipAdjust) const {
800     if (uprv_strcmp(key, "currency") == 0) {
801         // ICU4C does not have ICU4J CurrencyDisplayInfo equivalent for now.
802         UErrorCode sts = U_ZERO_ERROR;
803         UnicodeString ustrValue(value, -1, US_INV);
804         int32_t len;
805         UBool isChoice = FALSE;
806         const UChar *currencyName = ucurr_getName(ustrValue.getTerminatedBuffer(),
807             locale.getBaseName(), UCURR_LONG_NAME, &isChoice, &len, &sts);
808         if (U_FAILURE(sts)) {
809             // Return the value as is on failure
810             result = ustrValue;
811             return result;
812         }
813         result.setTo(currencyName, len);
814         return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
815     }
816 
817     if (nameLength == UDISPCTX_LENGTH_SHORT) {
818         langData.get("Types%short", key, value, result);
819         if (!result.isBogus()) {
820             return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
821         }
822     }
823     langData.get("Types", key, value, result);
824     return skipAdjust? result: adjustForUsageAndContext(kCapContextUsageKeyValue, result);
825 }
826 
827 UnicodeString&
keyValueDisplayName(const char * key,const char * value,UnicodeString & result) const828 LocaleDisplayNamesImpl::keyValueDisplayName(const char* key,
829                                             const char* value,
830                                             UnicodeString& result) const {
831     return keyValueDisplayName(key, value, result, FALSE);
832 }
833 
834 ////////////////////////////////////////////////////////////////////////////////////////////////////
835 
836 LocaleDisplayNames*
createInstance(const Locale & locale,UDialectHandling dialectHandling)837 LocaleDisplayNames::createInstance(const Locale& locale,
838                                    UDialectHandling dialectHandling) {
839     return new LocaleDisplayNamesImpl(locale, dialectHandling);
840 }
841 
842 LocaleDisplayNames*
createInstance(const Locale & locale,UDisplayContext * contexts,int32_t length)843 LocaleDisplayNames::createInstance(const Locale& locale,
844                                    UDisplayContext *contexts, int32_t length) {
845     if (contexts == NULL) {
846         length = 0;
847     }
848     return new LocaleDisplayNamesImpl(locale, contexts, length);
849 }
850 
851 U_NAMESPACE_END
852 
853 ////////////////////////////////////////////////////////////////////////////////////////////////////
854 
855 U_NAMESPACE_USE
856 
857 U_CAPI ULocaleDisplayNames * U_EXPORT2
uldn_open(const char * locale,UDialectHandling dialectHandling,UErrorCode * pErrorCode)858 uldn_open(const char * locale,
859           UDialectHandling dialectHandling,
860           UErrorCode *pErrorCode) {
861   if (U_FAILURE(*pErrorCode)) {
862     return 0;
863   }
864   if (locale == NULL) {
865     locale = uloc_getDefault();
866   }
867   return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), dialectHandling);
868 }
869 
870 U_CAPI ULocaleDisplayNames * U_EXPORT2
uldn_openForContext(const char * locale,UDisplayContext * contexts,int32_t length,UErrorCode * pErrorCode)871 uldn_openForContext(const char * locale,
872                     UDisplayContext *contexts, int32_t length,
873                     UErrorCode *pErrorCode) {
874   if (U_FAILURE(*pErrorCode)) {
875     return 0;
876   }
877   if (locale == NULL) {
878     locale = uloc_getDefault();
879   }
880   return (ULocaleDisplayNames *)LocaleDisplayNames::createInstance(Locale(locale), contexts, length);
881 }
882 
883 
884 U_CAPI void U_EXPORT2
uldn_close(ULocaleDisplayNames * ldn)885 uldn_close(ULocaleDisplayNames *ldn) {
886   delete (LocaleDisplayNames *)ldn;
887 }
888 
889 U_CAPI const char * U_EXPORT2
uldn_getLocale(const ULocaleDisplayNames * ldn)890 uldn_getLocale(const ULocaleDisplayNames *ldn) {
891   if (ldn) {
892     return ((const LocaleDisplayNames *)ldn)->getLocale().getName();
893   }
894   return NULL;
895 }
896 
897 U_CAPI UDialectHandling U_EXPORT2
uldn_getDialectHandling(const ULocaleDisplayNames * ldn)898 uldn_getDialectHandling(const ULocaleDisplayNames *ldn) {
899   if (ldn) {
900     return ((const LocaleDisplayNames *)ldn)->getDialectHandling();
901   }
902   return ULDN_STANDARD_NAMES;
903 }
904 
905 U_CAPI UDisplayContext U_EXPORT2
uldn_getContext(const ULocaleDisplayNames * ldn,UDisplayContextType type,UErrorCode * pErrorCode)906 uldn_getContext(const ULocaleDisplayNames *ldn,
907               UDisplayContextType type,
908               UErrorCode *pErrorCode) {
909   if (U_FAILURE(*pErrorCode)) {
910     return (UDisplayContext)0;
911   }
912   return ((const LocaleDisplayNames *)ldn)->getContext(type);
913 }
914 
915 U_CAPI int32_t U_EXPORT2
uldn_localeDisplayName(const ULocaleDisplayNames * ldn,const char * locale,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)916 uldn_localeDisplayName(const ULocaleDisplayNames *ldn,
917                        const char *locale,
918                        UChar *result,
919                        int32_t maxResultSize,
920                        UErrorCode *pErrorCode) {
921   if (U_FAILURE(*pErrorCode)) {
922     return 0;
923   }
924   if (ldn == NULL || locale == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
925     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
926     return 0;
927   }
928   UnicodeString temp(result, 0, maxResultSize);
929   ((const LocaleDisplayNames *)ldn)->localeDisplayName(locale, temp);
930   if (temp.isBogus()) {
931     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
932     return 0;
933   }
934   return temp.extract(result, maxResultSize, *pErrorCode);
935 }
936 
937 U_CAPI int32_t U_EXPORT2
uldn_languageDisplayName(const ULocaleDisplayNames * ldn,const char * lang,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)938 uldn_languageDisplayName(const ULocaleDisplayNames *ldn,
939                          const char *lang,
940                          UChar *result,
941                          int32_t maxResultSize,
942                          UErrorCode *pErrorCode) {
943   if (U_FAILURE(*pErrorCode)) {
944     return 0;
945   }
946   if (ldn == NULL || lang == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
947     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
948     return 0;
949   }
950   UnicodeString temp(result, 0, maxResultSize);
951   ((const LocaleDisplayNames *)ldn)->languageDisplayName(lang, temp);
952   return temp.extract(result, maxResultSize, *pErrorCode);
953 }
954 
955 U_CAPI int32_t U_EXPORT2
uldn_scriptDisplayName(const ULocaleDisplayNames * ldn,const char * script,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)956 uldn_scriptDisplayName(const ULocaleDisplayNames *ldn,
957                        const char *script,
958                        UChar *result,
959                        int32_t maxResultSize,
960                        UErrorCode *pErrorCode) {
961   if (U_FAILURE(*pErrorCode)) {
962     return 0;
963   }
964   if (ldn == NULL || script == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
965     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
966     return 0;
967   }
968   UnicodeString temp(result, 0, maxResultSize);
969   ((const LocaleDisplayNames *)ldn)->scriptDisplayName(script, temp);
970   return temp.extract(result, maxResultSize, *pErrorCode);
971 }
972 
973 U_CAPI int32_t U_EXPORT2
uldn_scriptCodeDisplayName(const ULocaleDisplayNames * ldn,UScriptCode scriptCode,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)974 uldn_scriptCodeDisplayName(const ULocaleDisplayNames *ldn,
975                            UScriptCode scriptCode,
976                            UChar *result,
977                            int32_t maxResultSize,
978                            UErrorCode *pErrorCode) {
979   return uldn_scriptDisplayName(ldn, uscript_getName(scriptCode), result, maxResultSize, pErrorCode);
980 }
981 
982 U_CAPI int32_t U_EXPORT2
uldn_regionDisplayName(const ULocaleDisplayNames * ldn,const char * region,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)983 uldn_regionDisplayName(const ULocaleDisplayNames *ldn,
984                        const char *region,
985                        UChar *result,
986                        int32_t maxResultSize,
987                        UErrorCode *pErrorCode) {
988   if (U_FAILURE(*pErrorCode)) {
989     return 0;
990   }
991   if (ldn == NULL || region == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
992     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
993     return 0;
994   }
995   UnicodeString temp(result, 0, maxResultSize);
996   ((const LocaleDisplayNames *)ldn)->regionDisplayName(region, temp);
997   return temp.extract(result, maxResultSize, *pErrorCode);
998 }
999 
1000 U_CAPI int32_t U_EXPORT2
uldn_variantDisplayName(const ULocaleDisplayNames * ldn,const char * variant,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1001 uldn_variantDisplayName(const ULocaleDisplayNames *ldn,
1002                         const char *variant,
1003                         UChar *result,
1004                         int32_t maxResultSize,
1005                         UErrorCode *pErrorCode) {
1006   if (U_FAILURE(*pErrorCode)) {
1007     return 0;
1008   }
1009   if (ldn == NULL || variant == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1010     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1011     return 0;
1012   }
1013   UnicodeString temp(result, 0, maxResultSize);
1014   ((const LocaleDisplayNames *)ldn)->variantDisplayName(variant, temp);
1015   return temp.extract(result, maxResultSize, *pErrorCode);
1016 }
1017 
1018 U_CAPI int32_t U_EXPORT2
uldn_keyDisplayName(const ULocaleDisplayNames * ldn,const char * key,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1019 uldn_keyDisplayName(const ULocaleDisplayNames *ldn,
1020                     const char *key,
1021                     UChar *result,
1022                     int32_t maxResultSize,
1023                     UErrorCode *pErrorCode) {
1024   if (U_FAILURE(*pErrorCode)) {
1025     return 0;
1026   }
1027   if (ldn == NULL || key == NULL || (result == NULL && maxResultSize > 0) || maxResultSize < 0) {
1028     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1029     return 0;
1030   }
1031   UnicodeString temp(result, 0, maxResultSize);
1032   ((const LocaleDisplayNames *)ldn)->keyDisplayName(key, temp);
1033   return temp.extract(result, maxResultSize, *pErrorCode);
1034 }
1035 
1036 U_CAPI int32_t U_EXPORT2
uldn_keyValueDisplayName(const ULocaleDisplayNames * ldn,const char * key,const char * value,UChar * result,int32_t maxResultSize,UErrorCode * pErrorCode)1037 uldn_keyValueDisplayName(const ULocaleDisplayNames *ldn,
1038                          const char *key,
1039                          const char *value,
1040                          UChar *result,
1041                          int32_t maxResultSize,
1042                          UErrorCode *pErrorCode) {
1043   if (U_FAILURE(*pErrorCode)) {
1044     return 0;
1045   }
1046   if (ldn == NULL || key == NULL || value == NULL || (result == NULL && maxResultSize > 0)
1047       || maxResultSize < 0) {
1048     *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1049     return 0;
1050   }
1051   UnicodeString temp(result, 0, maxResultSize);
1052   ((const LocaleDisplayNames *)ldn)->keyValueDisplayName(key, value, temp);
1053   return temp.extract(result, maxResultSize, *pErrorCode);
1054 }
1055 
1056 #endif
1057