summaryrefslogtreecommitdiffstats
path: root/intl/icu/source/i18n/displayoptions.cpp
blob: bb49e6033f3c9f94ff65e6575849a6baa6440e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// © 2022 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING

#include "unicode/displayoptions.h"
#include "unicode/udisplayoptions.h"
#include "cstring.h"

U_NAMESPACE_BEGIN

DisplayOptions::Builder DisplayOptions::builder() { return DisplayOptions::Builder(); }

DisplayOptions::Builder DisplayOptions::copyToBuilder() const { return Builder(*this); }

DisplayOptions::DisplayOptions(const Builder &builder) {
    grammaticalCase = builder.grammaticalCase;
    nounClass = builder.nounClass;
    pluralCategory = builder.pluralCategory;
    capitalization = builder.capitalization;
    nameStyle = builder.nameStyle;
    displayLength = builder.displayLength;
    substituteHandling = builder.substituteHandling;
}

DisplayOptions::Builder::Builder() {
    // Sets default values.
    grammaticalCase = UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
    nounClass = UDISPOPT_NOUN_CLASS_UNDEFINED;
    pluralCategory = UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
    capitalization = UDISPOPT_CAPITALIZATION_UNDEFINED;
    nameStyle = UDISPOPT_NAME_STYLE_UNDEFINED;
    displayLength = UDISPOPT_DISPLAY_LENGTH_UNDEFINED;
    substituteHandling = UDISPOPT_SUBSTITUTE_HANDLING_UNDEFINED;
}

DisplayOptions::Builder::Builder(const DisplayOptions &displayOptions) {
    grammaticalCase = displayOptions.grammaticalCase;
    nounClass = displayOptions.nounClass;
    pluralCategory = displayOptions.pluralCategory;
    capitalization = displayOptions.capitalization;
    nameStyle = displayOptions.nameStyle;
    displayLength = displayOptions.displayLength;
    substituteHandling = displayOptions.substituteHandling;
}

U_NAMESPACE_END

// C API ------------------------------------------------------------------- ***

U_NAMESPACE_USE

namespace {

const char *grammaticalCaseIds[] = {
    "undefined",           // 0
    "ablative",            // 1
    "accusative",          // 2
    "comitative",          // 3
    "dative",              // 4
    "ergative",            // 5
    "genitive",            // 6
    "instrumental",        // 7
    "locative",            // 8
    "locative_copulative", // 9
    "nominative",          // 10
    "oblique",             // 11
    "prepositional",       // 12
    "sociative",           // 13
    "vocative",            // 14
};

} // namespace

U_CAPI const char * U_EXPORT2
udispopt_getGrammaticalCaseIdentifier(UDisplayOptionsGrammaticalCase grammaticalCase) {
    if (grammaticalCase >= 0 && grammaticalCase < UPRV_LENGTHOF(grammaticalCaseIds)) {
        return grammaticalCaseIds[grammaticalCase];
    }

    return grammaticalCaseIds[0];
}

U_CAPI UDisplayOptionsGrammaticalCase U_EXPORT2
udispopt_fromGrammaticalCaseIdentifier(const char *identifier) {
    for (int32_t i = 0; i < UPRV_LENGTHOF(grammaticalCaseIds); i++) {
        if (uprv_strcmp(identifier, grammaticalCaseIds[i]) == 0) {
            return static_cast<UDisplayOptionsGrammaticalCase>(i);
        }
    }

    return UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
}

namespace {

const char *pluralCategoryIds[] = {
    "undefined", // 0
    "zero",      // 1
    "one",       // 2
    "two",       // 3
    "few",       // 4
    "many",      // 5
    "other",     // 6
};

} // namespace

U_CAPI const char * U_EXPORT2
udispopt_getPluralCategoryIdentifier(UDisplayOptionsPluralCategory pluralCategory) {
    if (pluralCategory >= 0 && pluralCategory < UPRV_LENGTHOF(pluralCategoryIds)) {
        return pluralCategoryIds[pluralCategory];
    }

    return pluralCategoryIds[0];
}

U_CAPI UDisplayOptionsPluralCategory U_EXPORT2
udispopt_fromPluralCategoryIdentifier(const char *identifier) {
    for (int32_t i = 0; i < UPRV_LENGTHOF(pluralCategoryIds); i++) {
        if (uprv_strcmp(identifier, pluralCategoryIds[i]) == 0) {
            return static_cast<UDisplayOptionsPluralCategory>(i);
        }
    }

    return UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
}

namespace {

const char *nounClassIds[] = {
    "undefined", // 0
    "other",     // 1
    "neuter",    // 2
    "feminine",  // 3
    "masculine", // 4
    "animate",   // 5
    "inanimate", // 6
    "personal",  // 7
    "common",    // 8
};

} // namespace

U_CAPI const char * U_EXPORT2
udispopt_getNounClassIdentifier(UDisplayOptionsNounClass nounClass) {
    if (nounClass >= 0 && nounClass < UPRV_LENGTHOF(nounClassIds)) {
        return nounClassIds[nounClass];
    }

    return nounClassIds[0];
}

U_CAPI UDisplayOptionsNounClass U_EXPORT2
udispopt_fromNounClassIdentifier(const char *identifier) {
    for (int32_t i = 0; i < UPRV_LENGTHOF(nounClassIds); i++) {
        if (uprv_strcmp(identifier, nounClassIds[i]) == 0) {
            return static_cast<UDisplayOptionsNounClass>(i);
        }
    }

    return UDISPOPT_NOUN_CLASS_UNDEFINED;
}

#endif /* #if !UCONFIG_NO_FORMATTING */