Bug 1728104 - Part 4: Merge NumberFormatFieldsUtil.[cpp|h] into NumberFormatFields.[cpp|h]. r=platform-i18n-reviewers,anba,dminor

Differential Revision: https://phabricator.services.mozilla.com/D125758
This commit is contained in:
Yoshi Cheng-Hao Huang 2021-09-16 14:47:03 +00:00
parent 70b53c7640
commit 7ded87e9a4
6 changed files with 74 additions and 101 deletions

View File

@ -29,7 +29,6 @@ UNIFIED_SOURCES += [
"src/LocaleCanonicalizer.cpp",
"src/NumberFormat.cpp",
"src/NumberFormatFields.cpp",
"src/NumberFormatFieldsUtil.cpp",
"src/NumberFormatterSkeleton.cpp",
"src/NumberRangeFormat.cpp",
"src/PluralRules.cpp",

View File

@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ICU4CGlue.h"
#include "NumberFormatFields.h"
#include "NumberFormatFieldsUtil.h"
#include "ScopedICUObject.h"
#include "unicode/uformattedvalue.h"
@ -324,4 +323,71 @@ Result<std::u16string_view, ICUError> FormatResultToParts(
return std::u16string_view(utf16Str, static_cast<size_t>(utf16Length));
}
// See intl/icu/source/i18n/unicode/unum.h for a detailed field list. This
// list is deliberately exhaustive: cases might have to be added/removed if
// this code is compiled with a different ICU with more UNumberFormatFields
// enum initializers. Please guard such cases with appropriate ICU
// version-testing #ifdefs, should cross-version divergence occur.
Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName,
Maybe<double> number,
bool isNegative,
bool formatForUnit) {
switch (fieldName) {
case UNUM_INTEGER_FIELD:
if (number.isSome()) {
if (IsNaN(*number)) {
return Some(NumberPartType::Nan);
}
if (!IsFinite(*number)) {
return Some(NumberPartType::Infinity);
}
}
return Some(NumberPartType::Integer);
case UNUM_FRACTION_FIELD:
return Some(NumberPartType::Fraction);
case UNUM_DECIMAL_SEPARATOR_FIELD:
return Some(NumberPartType::Decimal);
case UNUM_EXPONENT_SYMBOL_FIELD:
return Some(NumberPartType::ExponentSeparator);
case UNUM_EXPONENT_SIGN_FIELD:
return Some(NumberPartType::ExponentMinusSign);
case UNUM_EXPONENT_FIELD:
return Some(NumberPartType::ExponentInteger);
case UNUM_GROUPING_SEPARATOR_FIELD:
return Some(NumberPartType::Group);
case UNUM_CURRENCY_FIELD:
return Some(NumberPartType::Currency);
case UNUM_PERCENT_FIELD:
if (formatForUnit) {
return Some(NumberPartType::Unit);
}
return Some(NumberPartType::Percent);
case UNUM_PERMILL_FIELD:
MOZ_ASSERT_UNREACHABLE(
"unexpected permill field found, even though "
"we don't use any user-defined patterns that "
"would require a permill field");
break;
case UNUM_SIGN_FIELD:
if (isNegative) {
return Some(NumberPartType::MinusSign);
}
return Some(NumberPartType::PlusSign);
case UNUM_MEASURE_UNIT_FIELD:
return Some(NumberPartType::Unit);
case UNUM_COMPACT_FIELD:
return Some(NumberPartType::Compact);
#ifndef U_HIDE_DEPRECATED_API
case UNUM_FIELD_COUNT:
MOZ_ASSERT_UNREACHABLE(
"format field sentinel value returned by iterator!");
break;
#endif
}
MOZ_ASSERT_UNREACHABLE(
"unenumerated, undocumented format field returned by iterator");
return Nothing();
}
} // namespace mozilla::intl

View File

@ -9,6 +9,8 @@
#include "mozilla/Result.h"
#include "mozilla/Vector.h"
#include "unicode/unum.h"
struct UFormattedNumber;
struct UFormattedValue;
@ -79,6 +81,11 @@ Result<std::u16string_view, ICUError> FormatResultToParts(
const UFormattedValue* value, Maybe<double> number, bool isNegative,
bool formatForUnit, NumberPartVector& parts);
Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName,
Maybe<double> number,
bool isNegative,
bool formatForUnit);
} // namespace mozilla::intl
#endif

View File

@ -1,77 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "NumberFormatFieldsUtil.h"
#include "mozilla/FloatingPoint.h"
namespace mozilla::intl {
// See intl/icu/source/i18n/unicode/unum.h for a detailed field list. This
// list is deliberately exhaustive: cases might have to be added/removed if
// this code is compiled with a different ICU with more UNumberFormatFields
// enum initializers. Please guard such cases with appropriate ICU
// version-testing #ifdefs, should cross-version divergence occur.
Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName,
Maybe<double> number,
bool isNegative,
bool formatForUnit) {
switch (fieldName) {
case UNUM_INTEGER_FIELD:
if (number.isSome()) {
if (IsNaN(*number)) {
return Some(NumberPartType::Nan);
}
if (!IsFinite(*number)) {
return Some(NumberPartType::Infinity);
}
}
return Some(NumberPartType::Integer);
case UNUM_FRACTION_FIELD:
return Some(NumberPartType::Fraction);
case UNUM_DECIMAL_SEPARATOR_FIELD:
return Some(NumberPartType::Decimal);
case UNUM_EXPONENT_SYMBOL_FIELD:
return Some(NumberPartType::ExponentSeparator);
case UNUM_EXPONENT_SIGN_FIELD:
return Some(NumberPartType::ExponentMinusSign);
case UNUM_EXPONENT_FIELD:
return Some(NumberPartType::ExponentInteger);
case UNUM_GROUPING_SEPARATOR_FIELD:
return Some(NumberPartType::Group);
case UNUM_CURRENCY_FIELD:
return Some(NumberPartType::Currency);
case UNUM_PERCENT_FIELD:
if (formatForUnit) {
return Some(NumberPartType::Unit);
}
return Some(NumberPartType::Percent);
case UNUM_PERMILL_FIELD:
MOZ_ASSERT_UNREACHABLE(
"unexpected permill field found, even though "
"we don't use any user-defined patterns that "
"would require a permill field");
break;
case UNUM_SIGN_FIELD:
if (isNegative) {
return Some(NumberPartType::MinusSign);
}
return Some(NumberPartType::PlusSign);
case UNUM_MEASURE_UNIT_FIELD:
return Some(NumberPartType::Unit);
case UNUM_COMPACT_FIELD:
return Some(NumberPartType::Compact);
#ifndef U_HIDE_DEPRECATED_API
case UNUM_FIELD_COUNT:
MOZ_ASSERT_UNREACHABLE(
"format field sentinel value returned by iterator!");
break;
#endif
}
MOZ_ASSERT_UNREACHABLE(
"unenumerated, undocumented format field returned by iterator");
return Nothing();
}
} // namespace mozilla::intl

View File

@ -1,21 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef intl_components_NumberFormatFieldsUtil_h_
#define intl_components_NumberFormatFieldsUtil_h_
#include "mozilla/intl/NumberPart.h"
#include "mozilla/Maybe.h"
#include "unicode/unum.h"
namespace mozilla::intl {
Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName,
Maybe<double> number,
bool isNegative,
bool formatForUnit);
} // namespace mozilla::intl
#endif

View File

@ -7,7 +7,6 @@
#include "mozilla/intl/ICU4CGlue.h"
#include "mozilla/intl/NumberFormat.h"
#include "NumberFormatFields.h"
#include "NumberFormatFieldsUtil.h"
#include "NumberFormatterSkeleton.h"
#include "ScopedICUObject.h"