Bug 1719735 - Have IcuLocale take a null-terminated Span; r=platform-i18n-reviewers,dminor

This simplifies the call sites that take Span locale, and ensures null
termination.

Differential Revision: https://phabricator.services.mozilla.com/D131534
This commit is contained in:
Greg Tatum 2021-11-30 19:05:56 +00:00
Родитель bc63ca7d8c
Коммит 072e80dc1d
4 изменённых файлов: 30 добавлений и 21 удалений

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

@ -54,10 +54,10 @@ Result<UniquePtr<DateIntervalFormat>, ICUError> DateIntervalFormat::TryCreate(
Span<const char> aLocale, Span<const char16_t> aSkeleton,
Span<const char16_t> aTimeZone) {
UErrorCode status = U_ZERO_ERROR;
UDateIntervalFormat* dif = udtitvfmt_open(
IcuLocale(AssertNullTerminatedString(aLocale)), aSkeleton.data(),
AssertedCast<int32_t>(aSkeleton.size()), aTimeZone.data(),
AssertedCast<int32_t>(aTimeZone.size()), &status);
UDateIntervalFormat* dif =
udtitvfmt_open(IcuLocale(aLocale), aSkeleton.data(),
AssertedCast<int32_t>(aSkeleton.size()), aTimeZone.data(),
AssertedCast<int32_t>(aTimeZone.size()), &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}

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

@ -270,10 +270,9 @@ Result<UniquePtr<DateTimeFormat>, ICUError> DateTimeFormat::TryCreateFromStyle(
}
UErrorCode status = U_ZERO_ERROR;
UDateFormat* dateFormat = udat_open(
timeStyle, dateStyle, IcuLocale(AssertNullTerminatedString(aLocale)),
tzID, tzIDLength,
/* pattern */ nullptr, /* pattern length */ -1, &status);
UDateFormat* dateFormat =
udat_open(timeStyle, dateStyle, IcuLocale(aLocale), tzID, tzIDLength,
/* pattern */ nullptr, /* pattern length */ -1, &status);
if (U_FAILURE(status)) {
return Err(ToICUError(status));
}
@ -554,8 +553,7 @@ DateTimeFormat::TryCreateFromPattern(
// Create the date formatter.
UDateFormat* dateFormat = udat_open(
UDAT_PATTERN, UDAT_PATTERN,
IcuLocale(AssertNullTerminatedString(aLocale)), tzID, tzIDLength,
UDAT_PATTERN, UDAT_PATTERN, IcuLocale(aLocale), tzID, tzIDLength,
aPattern.data(), static_cast<int32_t>(aPattern.size()), &status);
if (U_FAILURE(status)) {

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

@ -32,15 +32,6 @@
struct UFormattedValue;
namespace mozilla::intl {
static inline const char* IcuLocale(const char* aLocale) {
// Return the empty string if the input is exactly equal to the string "und".
const char* locale = aLocale;
if (!std::strcmp(locale, "und")) {
locale = ""; // ICU root locale
}
return locale;
}
static inline const char* AssertNullTerminatedString(Span<const char> aSpan) {
// Intentionally check one past the last character, because we expect that the
// NUL character isn't part of the string.
@ -63,6 +54,26 @@ static inline const char* AssertNullTerminatedString(std::string_view aView) {
return aView.data();
}
/**
* Map the "und" locale to an empty string, which the ICU uses internally.
*/
static inline const char* IcuLocale(const char* aLocale) {
// Return the empty string if the input is exactly equal to the string "und".
const char* locale = aLocale;
if (!std::strcmp(locale, "und")) {
locale = ""; // ICU root locale
}
return locale;
}
/**
* Ensure a locale is null-terminated, and map the "und" locale to an empty
* string, which the ICU uses internally.
*/
static inline const char* IcuLocale(Span<const char> aLocale) {
return IcuLocale(AssertNullTerminatedString(aLocale));
}
using ICUResult = Result<Ok, ICUError>;
/**

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

@ -13,8 +13,8 @@ namespace mozilla::intl {
UListFormatterWidth uwidth = ToUListFormatterWidth(aOptions.mStyle);
UErrorCode status = U_ZERO_ERROR;
UListFormatter* fmt = ulistfmt_openForType(
IcuLocale(AssertNullTerminatedString(aLocale)), utype, uwidth, &status);
UListFormatter* fmt =
ulistfmt_openForType(IcuLocale(aLocale), utype, uwidth, &status);
if (U_FAILURE(status)) {
return Err(ICUError::InternalError);
}