Skip to content

Commit

Permalink
ICU-21935 DisplayOptions cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markusicu committed May 26, 2022
1 parent e172195 commit 8d5a97a
Show file tree
Hide file tree
Showing 10 changed files with 1,212 additions and 1,204 deletions.
128 changes: 68 additions & 60 deletions icu4c/source/i18n/displayoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,56 @@

#if !UCONFIG_NO_FORMATTING

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

U_NAMESPACE_BEGIN

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

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

icu::DisplayOptions::DisplayOptions(const Builder &builder) {
this->grammaticalCase = builder.grammaticalCase;
this->nounClass = builder.nounClass;
this->pluralCategory = builder.pluralCategory;
this->capitalization = builder.capitalization;
this->nameStyle = builder.nameStyle;
this->displayLength = builder.displayLength;
this->substituteHandling = builder.substituteHandling;
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;
}

icu::DisplayOptions::Builder::Builder() {
DisplayOptions::Builder::Builder() {
// Sets default values.
this->grammaticalCase = UDisplayOptionsGrammaticalCase::UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
this->nounClass = UDisplayOptionsNounClass::UDISPOPT_NOUN_CLASS_UNDEFINED;
this->pluralCategory = UDisplayOptionsPluralCategory::UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
this->capitalization = UDisplayOptionsCapitalization::UDISPOPT_CAPITALIZATION_UNDEFINED;
this->nameStyle = UDisplayOptionsNameStyle::UDISPOPT_NAME_STYLE_UNDEFINED;
this->displayLength = UDisplayOptionsDisplayLength::UDISPOPT_DISPLAY_LENGTH_UNDEFINED;
this->substituteHandling = UDisplayOptionsSubstituteHandling::UDISPOPT_SUBSTITUTE_HANDLING_UNDEFINED;
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;
}

icu::DisplayOptions::Builder::Builder(const DisplayOptions &displayOptions) {
this->grammaticalCase = displayOptions.grammaticalCase;
this->nounClass = displayOptions.nounClass;
this->pluralCategory = displayOptions.pluralCategory;
this->capitalization = displayOptions.capitalization;
this->nameStyle = displayOptions.nameStyle;
this->displayLength = displayOptions.displayLength;
this->substituteHandling = displayOptions.substituteHandling;
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 *grammaticalCasesIds[] = {
const char *grammaticalCaseIds[] = {
"undefined", // 0
"ablative", // 1
"accusative", // 2
Expand All @@ -64,31 +72,31 @@ const char *grammaticalCasesIds[] = {
"vocative", // 14
};

const int32_t grammaticalCasesCount = 15;

} // namespace

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

return grammaticalCasesIds[0];
return grammaticalCaseIds[0];
}

UDisplayOptionsGrammaticalCase udispopt_fromGrammaticalCaseIdentifier(const char *identifier) {
for (int32_t i = 0; i < grammaticalCasesCount; i++) {
if (uprv_strcmp(identifier, grammaticalCasesIds[i]) == 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 UDisplayOptionsGrammaticalCase::UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
return UDISPOPT_GRAMMATICAL_CASE_UNDEFINED;
}

namespace {

const char *pluralCategoriesIds[] = {
const char *pluralCategoryIds[] = {
"undefined", // 0
"zero", // 1
"one", // 2
Expand All @@ -100,29 +108,29 @@ const char *pluralCategoriesIds[] = {

} // namespace

const int32_t pluralCategoriesCount = 7;

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

return pluralCategoriesIds[0];
return pluralCategoryIds[0];
}

UDisplayOptionsPluralCategory udispopt_fromPluralCategoryIdentifier(const char *identifier) {
for (int32_t i = 0; i < pluralCategoriesCount; i++) {
if (uprv_strcmp(identifier, pluralCategoriesIds[i]) == 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 UDisplayOptionsPluralCategory::UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
return UDISPOPT_PLURAL_CATEGORY_UNDEFINED;
}

namespace {

const char *nounClassesIds[] = {
const char *nounClassIds[] = {
"undefined", // 0
"other", // 1
"neuter", // 2
Expand All @@ -134,26 +142,26 @@ const char *nounClassesIds[] = {
"common", // 8
};

const int32_t nounClassesCount = 9;

} // namespace

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

return nounClassesIds[0];
return nounClassIds[0];
}

UDisplayOptionsNounClass udispopt_fromNounClassIdentifier(const char *identifier) {
for (int32_t i = 0; i < nounClassesCount; i++) {
if (uprv_strcmp(identifier, nounClassesIds[i]) == 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 UDisplayOptionsNounClass::UDISPOPT_NOUN_CLASS_UNDEFINED;
return UDISPOPT_NOUN_CLASS_UNDEFINED;
}

#endif /* #if !UCONFIG_NO_FORMATTING */
2 changes: 1 addition & 1 deletion icu4c/source/i18n/number_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ MeasureUnit FormattedNumber::getOutputUnit(UErrorCode& status) const {
}

UDisplayOptionsNounClass FormattedNumber::getNounClass(UErrorCode &status) const {
UPRV_FORMATTED_VALUE_METHOD_GUARD(UDisplayOptionsNounClass::UDISPOPT_NOUN_CLASS_UNDEFINED);
UPRV_FORMATTED_VALUE_METHOD_GUARD(UDISPOPT_NOUN_CLASS_UNDEFINED);
const char *nounClass = fData->gender;
return udispopt_fromNounClassIdentifier(nounClass);
}
Expand Down
Loading

0 comments on commit 8d5a97a

Please sign in to comment.