Bug 1719462 - Use ICUError in NumberFormat; r=platform-i18n-reviewers,gregtatum

Differential Revision: https://phabricator.services.mozilla.com/D121410
This commit is contained in:
Dan Minor 2021-08-30 19:50:19 +00:00
Родитель 962b25b43e
Коммит d8d32990af
9 изменённых файлов: 149 добавлений и 153 удалений

Просмотреть файл

@ -9,6 +9,7 @@ EXPORTS.mozilla.intl = [
"src/DateTimeFormat.h",
"src/DateTimePatternGenerator.h",
"src/ICU4CGlue.h",
"src/ICUError.h",
"src/NumberFormat.h",
"src/NumberRangeFormat.h",
"src/PluralRules.h",

Просмотреть файл

@ -12,19 +12,10 @@
#include "mozilla/Result.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/Vector.h"
#include "mozilla/intl/ICUError.h"
namespace mozilla::intl {
enum class ICUError : uint8_t {
OutOfMemory,
InternalError,
};
/**
* Error type when a method call can only result in an internal ICU error.
*/
struct InternalError {};
using ICUResult = Result<Ok, ICUError>;
/**

Просмотреть файл

@ -0,0 +1,27 @@
/* 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_ICUError_h
#define intl_components_ICUError_h
#include <cstdint>
namespace mozilla::intl {
/**
* General purpose error type for operations that can result in an ICU error.
*/
enum class ICUError : uint8_t {
OutOfMemory,
InternalError,
};
/**
* Error type when a method call can only result in an internal ICU error.
*/
struct InternalError {};
} // namespace mozilla::intl
#endif

Просмотреть файл

@ -12,11 +12,10 @@
namespace mozilla {
namespace intl {
/*static*/ Result<UniquePtr<NumberFormat>, NumberFormat::FormatError>
NumberFormat::TryCreate(std::string_view aLocale,
const NumberFormatOptions& aOptions) {
/*static*/ Result<UniquePtr<NumberFormat>, ICUError> NumberFormat::TryCreate(
std::string_view aLocale, const NumberFormatOptions& aOptions) {
UniquePtr<NumberFormat> nf = MakeUnique<NumberFormat>();
Result<Ok, FormatError> result = nf->initialize(aLocale, aOptions);
Result<Ok, ICUError> result = nf->initialize(aLocale, aOptions);
if (result.isOk()) {
return nf;
}
@ -33,7 +32,7 @@ NumberFormat::~NumberFormat() {
}
}
Result<Ok, NumberFormat::FormatError> NumberFormat::initialize(
Result<Ok, ICUError> NumberFormat::initialize(
std::string_view aLocale, const NumberFormatOptions& aOptions) {
mFormatForUnit = aOptions.mUnit.isSome();
NumberFormatterSkeleton skeleton(aOptions);
@ -45,24 +44,24 @@ Result<Ok, NumberFormat::FormatError> NumberFormat::initialize(
return Ok();
}
}
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
Result<int32_t, NumberFormat::FormatError> NumberFormat::selectFormatted(
Result<int32_t, ICUError> NumberFormat::selectFormatted(
double number, char16_t* keyword, int32_t keywordSize,
UPluralRules* pluralRules) const {
MOZ_ASSERT(keyword && pluralRules);
UErrorCode status = U_ZERO_ERROR;
if (format(number).isErr()) {
return Err(NumberFormat::FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t utf16KeywordLength = uplrules_selectFormatted(
pluralRules, mFormattedNumber, keyword, keywordSize, &status);
if (U_FAILURE(status)) {
return Err(NumberFormat::FormatError::InternalError);
return Err(ICUError::InternalError);
}
return utf16KeywordLength;
@ -94,21 +93,20 @@ bool NumberFormat::formatInternal(std::string_view number) const {
return U_SUCCESS(status);
}
Result<std::u16string_view, NumberFormat::FormatError>
NumberFormat::formatResult() const {
Result<std::u16string_view, ICUError> NumberFormat::formatResult() const {
UErrorCode status = U_ZERO_ERROR;
const UFormattedValue* formattedValue =
unumf_resultAsValue(mFormattedNumber, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t utf16Length;
const char16_t* utf16Str =
ufmtval_getString(formattedValue, &utf16Length, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return std::u16string_view(utf16Str, static_cast<size_t>(utf16Length));
@ -176,34 +174,33 @@ Maybe<NumberPartType> GetPartTypeForNumberField(UNumberFormatFields fieldName,
return Nothing();
}
Result<std::u16string_view, NumberFormat::FormatError>
NumberFormat::formatResultToParts(Maybe<double> number, bool isNegative,
NumberPartVector& parts) const {
Result<std::u16string_view, ICUError> NumberFormat::formatResultToParts(
Maybe<double> number, bool isNegative, NumberPartVector& parts) const {
UErrorCode status = U_ZERO_ERROR;
const UFormattedValue* formattedValue =
unumf_resultAsValue(mFormattedNumber, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t utf16Length;
const char16_t* utf16Str =
ufmtval_getString(formattedValue, &utf16Length, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
UConstrainedFieldPosition* fpos = ucfpos_open(&status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
ScopedICUObject<UConstrainedFieldPosition, ucfpos_close> toCloseFpos(fpos);
// We're only interested in UFIELD_CATEGORY_NUMBER fields.
ucfpos_constrainCategory(fpos, UFIELD_CATEGORY_NUMBER, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
// Vacuum up fields in the overall formatted string.
@ -212,7 +209,7 @@ NumberFormat::formatResultToParts(Maybe<double> number, bool isNegative,
while (true) {
bool hasMore = ufmtval_nextPosition(formattedValue, fpos, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
if (!hasMore) {
break;
@ -220,24 +217,24 @@ NumberFormat::formatResultToParts(Maybe<double> number, bool isNegative,
int32_t fieldName = ucfpos_getField(fpos, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t beginIndex, endIndex;
ucfpos_getIndexes(fpos, &beginIndex, &endIndex, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
Maybe<NumberPartType> partType = GetPartTypeForNumberField(
UNumberFormatFields(fieldName), number, isNegative, mFormatForUnit);
if (!partType || !fields.append(*partType, beginIndex, endIndex)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
}
if (!fields.toPartsVector(utf16Length, parts)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return std::u16string_view(utf16Str, static_cast<size_t>(utf16Length));

Просмотреть файл

@ -15,6 +15,7 @@
#include "mozilla/ResultVariant.h"
#include "mozilla/Utf8.h"
#include "mozilla/Vector.h"
#include "mozilla/intl/ICUError.h"
#include "unicode/ustring.h"
#include "unicode/unum.h"
@ -236,18 +237,13 @@ using NumberPartVector = mozilla::Vector<NumberPart, 8>;
*/
class NumberFormat final {
public:
enum class FormatError {
InternalError,
OutOfMemory,
};
/**
* Initialize a new NumberFormat for the provided locale and using the
* provided options.
*
* https://tc39.es/ecma402/#sec-initializenumberformat
*/
static Result<UniquePtr<NumberFormat>, NumberFormat::FormatError> TryCreate(
static Result<UniquePtr<NumberFormat>, ICUError> TryCreate(
std::string_view aLocale, const NumberFormatOptions& aOptions);
NumberFormat() = default;
@ -262,10 +258,9 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
Result<std::u16string_view, NumberFormat::FormatError> format(
double number) const {
Result<std::u16string_view, ICUError> format(double number) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult();
@ -278,10 +273,10 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-partitionnumberpattern
*/
Result<std::u16string_view, NumberFormat::FormatError> formatToParts(
Result<std::u16string_view, ICUError> formatToParts(
double number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
bool isNegative = !IsNaN(number) && IsNegative(number);
@ -295,9 +290,9 @@ class NumberFormat final {
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
template <typename B>
Result<Ok, NumberFormat::FormatError> format(double number, B& buffer) const {
Result<Ok, ICUError> format(double number, B& buffer) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult<typename B::CharType, B>(buffer);
@ -310,10 +305,9 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
Result<std::u16string_view, NumberFormat::FormatError> format(
int64_t number) const {
Result<std::u16string_view, ICUError> format(int64_t number) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult();
@ -326,10 +320,10 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-partitionnumberpattern
*/
Result<std::u16string_view, NumberFormat::FormatError> formatToParts(
Result<std::u16string_view, ICUError> formatToParts(
int64_t number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResultToParts(Nothing(), number < 0, parts);
@ -341,10 +335,9 @@ class NumberFormat final {
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
template <typename B>
Result<Ok, NumberFormat::FormatError> format(int64_t number,
B& buffer) const {
Result<Ok, ICUError> format(int64_t number, B& buffer) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult<typename B::CharType, B>(buffer);
@ -357,10 +350,9 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
Result<std::u16string_view, NumberFormat::FormatError> format(
std::string_view number) const {
Result<std::u16string_view, ICUError> format(std::string_view number) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult();
@ -374,10 +366,10 @@ class NumberFormat final {
*
* https://tc39.es/ecma402/#sec-partitionnumberpattern
*/
Result<std::u16string_view, NumberFormat::FormatError> formatToParts(
Result<std::u16string_view, ICUError> formatToParts(
std::string_view number, NumberPartVector& parts) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
// Non-finite numbers aren't currently supported here. If we ever need to
@ -399,10 +391,9 @@ class NumberFormat final {
* https://tc39.es/ecma402/#sec-formatnumberstring
*/
template <typename B>
Result<Ok, NumberFormat::FormatError> format(std::string_view number,
B& buffer) const {
Result<Ok, ICUError> format(std::string_view number, B& buffer) const {
if (!formatInternal(number)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult<typename B::CharType, B>(buffer);
@ -421,53 +412,53 @@ class NumberFormat final {
* functionality should be removed from NumberFormat and invoked
* solely from PluralRules.
*/
Result<int32_t, NumberFormat::FormatError> selectFormatted(
double number, char16_t* keyword, int32_t keywordSize,
UPluralRules* pluralRules) const;
Result<int32_t, ICUError> selectFormatted(double number, char16_t* keyword,
int32_t keywordSize,
UPluralRules* pluralRules) const;
private:
UNumberFormatter* mNumberFormatter = nullptr;
UFormattedNumber* mFormattedNumber = nullptr;
bool mFormatForUnit = false;
Result<Ok, NumberFormat::FormatError> initialize(
std::string_view aLocale, const NumberFormatOptions& aOptions);
Result<Ok, ICUError> initialize(std::string_view aLocale,
const NumberFormatOptions& aOptions);
[[nodiscard]] bool formatInternal(double number) const;
[[nodiscard]] bool formatInternal(int64_t number) const;
[[nodiscard]] bool formatInternal(std::string_view number) const;
Result<std::u16string_view, NumberFormat::FormatError> formatResult() const;
Result<std::u16string_view, NumberFormat::FormatError> formatResultToParts(
Result<std::u16string_view, ICUError> formatResult() const;
Result<std::u16string_view, ICUError> formatResultToParts(
const Maybe<double> number, bool isNegative,
NumberPartVector& parts) const;
template <typename C, typename B>
Result<Ok, NumberFormat::FormatError> formatResult(B& buffer) const {
Result<Ok, ICUError> formatResult(B& buffer) const {
// We only support buffers with char or char16_t.
static_assert(std::is_same<C, char>::value ||
std::is_same<C, char16_t>::value);
return formatResult().andThen([&buffer](std::u16string_view result)
-> Result<Ok, NumberFormat::FormatError> {
if constexpr (std::is_same<C, char>::value) {
if (!FillUTF8Buffer(Span(result.data(), result.size()), buffer)) {
return Err(FormatError::OutOfMemory);
}
return Ok();
} else {
// ICU provides APIs which accept a buffer, but they just copy from an
// internal buffer behind the scenes anyway.
if (!buffer.reserve(result.size())) {
return Err(FormatError::OutOfMemory);
}
PodCopy(static_cast<char16_t*>(buffer.data()), result.data(),
result.size());
buffer.written(result.size());
return formatResult().andThen(
[&buffer](std::u16string_view result) -> Result<Ok, ICUError> {
if constexpr (std::is_same<C, char>::value) {
if (!FillUTF8Buffer(Span(result.data(), result.size()), buffer)) {
return Err(ICUError::OutOfMemory);
}
return Ok();
} else {
// ICU provides APIs which accept a buffer, but they just copy from
// an internal buffer behind the scenes anyway.
if (!buffer.reserve(result.size())) {
return Err(ICUError::OutOfMemory);
}
PodCopy(static_cast<char16_t*>(buffer.data()), result.data(),
result.size());
buffer.written(result.size());
return Ok();
}
});
return Ok();
}
});
}
};

Просмотреть файл

@ -17,7 +17,7 @@ namespace mozilla::intl {
#ifdef MOZ_INTL_HAS_NUMBER_RANGE_FORMAT
/*static*/ Result<UniquePtr<NumberRangeFormat>, NumberRangeFormat::FormatError>
/*static*/ Result<UniquePtr<NumberRangeFormat>, ICUError>
NumberRangeFormat::TryCreate(std::string_view aLocale,
const NumberRangeFormatOptions& aOptions) {
UniquePtr<NumberRangeFormat> nrf = MakeUnique<NumberRangeFormat>();
@ -34,7 +34,7 @@ NumberRangeFormat::~NumberRangeFormat() {
}
}
Result<Ok, NumberRangeFormat::FormatError> NumberRangeFormat::initialize(
Result<Ok, ICUError> NumberRangeFormat::initialize(
std::string_view aLocale, const NumberRangeFormatOptions& aOptions) {
mFormatForUnit = aOptions.mUnit.isSome();
switch (aOptions.mRangeIdentityFallback) {
@ -58,13 +58,12 @@ Result<Ok, NumberRangeFormat::FormatError> NumberRangeFormat::initialize(
return Ok();
}
}
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
Result<int32_t, NumberRangeFormat::FormatError>
NumberRangeFormat::selectForRange(double start, double end, char16_t* keyword,
int32_t keywordSize,
const UPluralRules* pluralRules) const {
Result<int32_t, ICUError> NumberRangeFormat::selectForRange(
double start, double end, char16_t* keyword, int32_t keywordSize,
const UPluralRules* pluralRules) const {
MOZ_ASSERT(keyword);
MOZ_ASSERT(pluralRules);
@ -74,7 +73,7 @@ NumberRangeFormat::selectForRange(double start, double end, char16_t* keyword,
int32_t utf16KeywordLength = uplrules_selectForRange(
pluralRules, mFormattedNumberRange, keyword, keywordSize, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return utf16KeywordLength;
@ -106,37 +105,34 @@ bool NumberRangeFormat::formatInternal(std::string_view start,
return U_SUCCESS(status);
}
Result<std::u16string_view, NumberRangeFormat::FormatError>
NumberRangeFormat::formatResult() const {
Result<std::u16string_view, ICUError> NumberRangeFormat::formatResult() const {
UErrorCode status = U_ZERO_ERROR;
const UFormattedValue* formattedValue =
unumrf_resultAsValue(mFormattedNumberRange, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t utf16Length;
const char16_t* utf16Str =
ufmtval_getString(formattedValue, &utf16Length, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return std::u16string_view(utf16Str, static_cast<size_t>(utf16Length));
}
Result<std::u16string_view, NumberRangeFormat::FormatError>
NumberRangeFormat::formatResultToParts(Maybe<double> start,
bool startIsNegative, Maybe<double> end,
bool endIsNegative,
NumberPartVector& parts) const {
Result<std::u16string_view, ICUError> NumberRangeFormat::formatResultToParts(
Maybe<double> start, bool startIsNegative, Maybe<double> end,
bool endIsNegative, NumberPartVector& parts) const {
UErrorCode status = U_ZERO_ERROR;
UNumberRangeIdentityResult identity =
unumrf_resultGetIdentityResult(mFormattedNumberRange, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
bool isIdenticalNumber = identity != UNUM_IDENTITY_RESULT_NOT_EQUAL;
@ -144,19 +140,19 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
const UFormattedValue* formattedValue =
unumrf_resultAsValue(mFormattedNumberRange, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t utf16Length;
const char16_t* utf16Str =
ufmtval_getString(formattedValue, &utf16Length, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
UConstrainedFieldPosition* fpos = ucfpos_open(&status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
ScopedICUObject<UConstrainedFieldPosition, ucfpos_close> toCloseFpos(fpos);
@ -168,7 +164,7 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
if (isIdenticalNumber) {
ucfpos_constrainCategory(fpos, UFIELD_CATEGORY_NUMBER, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
}
@ -183,7 +179,7 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
while (true) {
bool hasMore = ufmtval_nextPosition(formattedValue, fpos, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
if (!hasMore) {
break;
@ -191,18 +187,18 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
int32_t category = ucfpos_getCategory(fpos, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t fieldName = ucfpos_getField(fpos, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
int32_t beginIndex, endIndex;
ucfpos_getIndexes(fpos, &beginIndex, &endIndex, &status);
if (U_FAILURE(status)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
if (category == UFIELD_CATEGORY_NUMBER_RANGE_SPAN) {
@ -235,12 +231,12 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
Maybe<NumberPartType> partType = GetPartTypeForNumberField(
UNumberFormatFields(fieldName), number, isNegative, mFormatForUnit);
if (!partType || !fields.append(*partType, beginIndex, endIndex)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
}
if (!fields.toPartsVector(utf16Length, sourceMap, parts)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
// Find the approximately sign for ECMA-402.
@ -295,7 +291,7 @@ NumberRangeFormat::formatResultToParts(Maybe<double> start,
#else
/*static*/ Result<UniquePtr<NumberRangeFormat>, NumberRangeFormat::FormatError>
/*static*/ Result<UniquePtr<NumberRangeFormat>, ICUError>
NumberRangeFormat::TryCreate(std::string_view aLocale,
const NumberFormatOptions& aOptions) {
return MakeUnique<NumberRangeFormat>();

Просмотреть файл

@ -5,6 +5,7 @@
#define intl_components_NumberRangeFormat_h_
#include "mozilla/FloatingPoint.h"
#include "mozilla/intl/ICUError.h"
#include "mozilla/intl/NumberFormat.h"
#include "mozilla/Result.h"
#include "mozilla/ResultVariant.h"
@ -95,18 +96,13 @@ struct MOZ_STACK_CLASS NumberRangeFormatOptions : public NumberFormatOptions {
*/
class NumberRangeFormat final {
public:
enum class FormatError {
InternalError,
OutOfMemory,
};
/**
* Initialize a new NumberRangeFormat for the provided locale and using the
* provided options.
*
* https://tc39.es/ecma402/#sec-initializenumberformat
*/
static Result<UniquePtr<NumberRangeFormat>, FormatError> TryCreate(
static Result<UniquePtr<NumberRangeFormat>, ICUError> TryCreate(
std::string_view aLocale, const NumberRangeFormatOptions& aOptions);
NumberRangeFormat() = default;
@ -123,10 +119,9 @@ class NumberRangeFormat final {
*
* https://tc39.es/ecma402/#sec-formatnumericrange
*/
Result<std::u16string_view, FormatError> format(double start,
double end) const {
Result<std::u16string_view, ICUError> format(double start, double end) const {
if (!formatInternal(start, end)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult();
@ -139,10 +134,10 @@ class NumberRangeFormat final {
*
* https://tc39.es/ecma402/#sec-partitionnumberrangepattern
*/
Result<std::u16string_view, FormatError> formatToParts(
Result<std::u16string_view, ICUError> formatToParts(
double start, double end, NumberPartVector& parts) const {
if (!formatInternal(start, end)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
bool isNegativeStart = !IsNaN(start) && IsNegative(start);
@ -159,10 +154,10 @@ class NumberRangeFormat final {
*
* https://tc39.es/ecma402/#sec-formatnumericrange
*/
Result<std::u16string_view, FormatError> format(std::string_view start,
std::string_view end) const {
Result<std::u16string_view, ICUError> format(std::string_view start,
std::string_view end) const {
if (!formatInternal(start, end)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
return formatResult();
@ -176,11 +171,11 @@ class NumberRangeFormat final {
*
* https://tc39.es/ecma402/#sec-partitionnumberrangepattern
*/
Result<std::u16string_view, FormatError> formatToParts(
Result<std::u16string_view, ICUError> formatToParts(
std::string_view start, std::string_view end,
NumberPartVector& parts) const {
if (!formatInternal(start, end)) {
return Err(FormatError::InternalError);
return Err(ICUError::InternalError);
}
Maybe<double> numStart = Nothing();
@ -223,7 +218,7 @@ class NumberRangeFormat final {
* functionality should be removed from NumberRangeFormat and invoked
* solely from PluralRules.
*/
Result<int32_t, FormatError> selectForRange(
Result<int32_t, ICUError> selectForRange(
double start, double end, char16_t* keyword, int32_t keywordSize,
const UPluralRules* pluralRules) const;
@ -233,20 +228,19 @@ class NumberRangeFormat final {
bool mFormatForUnit = false;
bool mFormatWithApprox = false;
Result<Ok, FormatError> initialize(std::string_view aLocale,
const NumberRangeFormatOptions& aOptions);
Result<Ok, ICUError> initialize(std::string_view aLocale,
const NumberRangeFormatOptions& aOptions);
[[nodiscard]] bool formatInternal(double start, double end) const;
[[nodiscard]] bool formatInternal(std::string_view start,
std::string_view end) const;
Result<std::u16string_view, FormatError> formatResult() const;
Result<std::u16string_view, ICUError> formatResult() const;
Result<std::u16string_view, NumberRangeFormat::FormatError>
formatResultToParts(Maybe<double> start, bool startIsNegative,
Maybe<double> end, bool endIsNegative,
NumberPartVector& parts) const;
Result<std::u16string_view, ICUError> formatResultToParts(
Maybe<double> start, bool startIsNegative, Maybe<double> end,
bool endIsNegative, NumberPartVector& parts) const;
#endif
};

Просмотреть файл

@ -289,7 +289,7 @@ ffi::RawNumberFormatter* FluentBuiltInNumberFormatterCreate(
aOptions->minimum_fraction_digits, aOptions->maximum_fraction_digits));
}
Result<UniquePtr<NumberFormat>, NumberFormat::FormatError> result =
Result<UniquePtr<NumberFormat>, ICUError> result =
NumberFormat::TryCreate(aLocale->get(), options);
MOZ_ASSERT(result.isOk());

Просмотреть файл

@ -816,8 +816,7 @@ static Formatter* NewNumberFormat(JSContext* cx,
options.mRangeIdentityFallback =
NumberFormatOptions::RangeIdentityFallback::Approximately;
mozilla::Result<mozilla::UniquePtr<Formatter>,
typename Formatter::FormatError>
mozilla::Result<mozilla::UniquePtr<Formatter>, mozilla::intl::ICUError>
result = Formatter::TryCreate(locale.get(), options);
if (result.isOk()) {
@ -1682,11 +1681,11 @@ bool js::intl_FormatNumber(JSContext* cx, unsigned argc, Value* vp) {
}
// Actually format the number
using FormatError = mozilla::intl::NumberFormat::FormatError;
using ICUError = mozilla::intl::ICUError;
bool formatToParts = args[2].toBoolean();
mozilla::Result<std::u16string_view, FormatError> result =
mozilla::Err(FormatError::InternalError);
mozilla::Result<std::u16string_view, ICUError> result =
mozilla::Err(ICUError::InternalError);
mozilla::intl::NumberPartVector parts;
if (value.isNumber()) {
double num = value.toNumber();
@ -2044,10 +2043,10 @@ bool js::intl_FormatNumberRange(JSContext* cx, unsigned argc, Value* vp) {
};
// Actually format the number range.
using FormatError = NumberRangeFormat::FormatError;
using ICUError = mozilla::intl::ICUError;
mozilla::Result<std::u16string_view, FormatError> result =
mozilla::Err(FormatError::InternalError);
mozilla::Result<std::u16string_view, ICUError> result =
mozilla::Err(ICUError::InternalError);
mozilla::intl::NumberPartVector parts;
double numStart, numEnd;