1 // © 2022 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7
8 #include "unicode/displayoptions.h"
9 #include "unicode/udisplayoptions.h"
10 #include "cstring.h"
11
12 U_NAMESPACE_BEGIN
13
builder()14 DisplayOptions::Builder DisplayOptions::builder() { return DisplayOptions::Builder(); }
15
copyToBuilder() const16 DisplayOptions::Builder DisplayOptions::copyToBuilder() const { return Builder(*this); }
17
DisplayOptions(const Builder & builder)18 DisplayOptions::DisplayOptions(const Builder &builder) {
19 grammaticalCase = builder.grammaticalCase;
20 nounClass = builder.nounClass;
21 pluralCategory = builder.pluralCategory;
22 capitalization = builder.capitalization;
23 nameStyle = builder.nameStyle;
24 displayLength = builder.displayLength;
25 substituteHandling = builder.substituteHandling;
26 }
27
Builder()28 DisplayOptions::Builder::Builder() {
29 // Sets default values.
30 grammaticalCase = UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
31 nounClass = UDISPOPT_NOUN_CLASS_UNDEFINED;
32 pluralCategory = UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
33 capitalization = UDISPOPT_CAPITALIZATION_UNDEFINED;
34 nameStyle = UDISPOPT_NAME_STYLE_UNDEFINED;
35 displayLength = UDISPOPT_DISPLAY_LENGTH_UNDEFINED;
36 substituteHandling = UDISPOPT_SUBSTITUTE_HANDLING_UNDEFINED;
37 }
38
Builder(const DisplayOptions & displayOptions)39 DisplayOptions::Builder::Builder(const DisplayOptions &displayOptions) {
40 grammaticalCase = displayOptions.grammaticalCase;
41 nounClass = displayOptions.nounClass;
42 pluralCategory = displayOptions.pluralCategory;
43 capitalization = displayOptions.capitalization;
44 nameStyle = displayOptions.nameStyle;
45 displayLength = displayOptions.displayLength;
46 substituteHandling = displayOptions.substituteHandling;
47 }
48
49 U_NAMESPACE_END
50
51 // C API ------------------------------------------------------------------- ***
52
53 U_NAMESPACE_USE
54
55 namespace {
56
57 const char *grammaticalCaseIds[] = {
58 "undefined", // 0
59 "ablative", // 1
60 "accusative", // 2
61 "comitative", // 3
62 "dative", // 4
63 "ergative", // 5
64 "genitive", // 6
65 "instrumental", // 7
66 "locative", // 8
67 "locative_copulative", // 9
68 "nominative", // 10
69 "oblique", // 11
70 "prepositional", // 12
71 "sociative", // 13
72 "vocative", // 14
73 };
74
75 } // namespace
76
77 U_CAPI const char * U_EXPORT2
udispopt_getGrammaticalCaseIdentifier(UDisplayOptionsGrammaticalCase grammaticalCase)78 udispopt_getGrammaticalCaseIdentifier(UDisplayOptionsGrammaticalCase grammaticalCase) {
79 if (grammaticalCase >= 0 && grammaticalCase < UPRV_LENGTHOF(grammaticalCaseIds)) {
80 return grammaticalCaseIds[grammaticalCase];
81 }
82
83 return grammaticalCaseIds[0];
84 }
85
86 U_CAPI UDisplayOptionsGrammaticalCase U_EXPORT2
udispopt_fromGrammaticalCaseIdentifier(const char * identifier)87 udispopt_fromGrammaticalCaseIdentifier(const char *identifier) {
88 for (int32_t i = 0; i < UPRV_LENGTHOF(grammaticalCaseIds); i++) {
89 if (uprv_strcmp(identifier, grammaticalCaseIds[i]) == 0) {
90 return static_cast<UDisplayOptionsGrammaticalCase>(i);
91 }
92 }
93
94 return UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
95 }
96
97 namespace {
98
99 const char *pluralCategoryIds[] = {
100 "undefined", // 0
101 "zero", // 1
102 "one", // 2
103 "two", // 3
104 "few", // 4
105 "many", // 5
106 "other", // 6
107 };
108
109 } // namespace
110
111 U_CAPI const char * U_EXPORT2
udispopt_getPluralCategoryIdentifier(UDisplayOptionsPluralCategory pluralCategory)112 udispopt_getPluralCategoryIdentifier(UDisplayOptionsPluralCategory pluralCategory) {
113 if (pluralCategory >= 0 && pluralCategory < UPRV_LENGTHOF(pluralCategoryIds)) {
114 return pluralCategoryIds[pluralCategory];
115 }
116
117 return pluralCategoryIds[0];
118 }
119
120 U_CAPI UDisplayOptionsPluralCategory U_EXPORT2
udispopt_fromPluralCategoryIdentifier(const char * identifier)121 udispopt_fromPluralCategoryIdentifier(const char *identifier) {
122 for (int32_t i = 0; i < UPRV_LENGTHOF(pluralCategoryIds); i++) {
123 if (uprv_strcmp(identifier, pluralCategoryIds[i]) == 0) {
124 return static_cast<UDisplayOptionsPluralCategory>(i);
125 }
126 }
127
128 return UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
129 }
130
131 namespace {
132
133 const char *nounClassIds[] = {
134 "undefined", // 0
135 "other", // 1
136 "neuter", // 2
137 "feminine", // 3
138 "masculine", // 4
139 "animate", // 5
140 "inanimate", // 6
141 "personal", // 7
142 "common", // 8
143 };
144
145 } // namespace
146
147 U_CAPI const char * U_EXPORT2
udispopt_getNounClassIdentifier(UDisplayOptionsNounClass nounClass)148 udispopt_getNounClassIdentifier(UDisplayOptionsNounClass nounClass) {
149 if (nounClass >= 0 && nounClass < UPRV_LENGTHOF(nounClassIds)) {
150 return nounClassIds[nounClass];
151 }
152
153 return nounClassIds[0];
154 }
155
156 U_CAPI UDisplayOptionsNounClass U_EXPORT2
udispopt_fromNounClassIdentifier(const char * identifier)157 udispopt_fromNounClassIdentifier(const char *identifier) {
158 for (int32_t i = 0; i < UPRV_LENGTHOF(nounClassIds); i++) {
159 if (uprv_strcmp(identifier, nounClassIds[i]) == 0) {
160 return static_cast<UDisplayOptionsNounClass>(i);
161 }
162 }
163
164 return UDISPOPT_NOUN_CLASS_UNDEFINED;
165 }
166
167 #endif /* #if !UCONFIG_NO_FORMATTING */
168