Bug 1719746 - Rename Locale to MozLocale; r=platform-i18n-reviewers,zbraniecki

There is also a mozilla::intl::Locale in intl/locale/MozLocale.h. This naming
collision was causing crashes in debug builds where the wrong destructor ended
up being called. This patch renames the intl/locale/MozLocale.h class to
MozLocale.

I've filed Bug 1736017 to move callers of the MozLocale version to the
unified intl/components/Locale version.

Differential Revision: https://phabricator.services.mozilla.com/D128593
This commit is contained in:
Dan Minor 2021-10-18 13:38:24 +00:00
parent d7c49630cb
commit a9e674d68d
12 changed files with 71 additions and 74 deletions

View File

@ -14,7 +14,7 @@
#include "mozilla/intl/MozLocale.h"
using mozilla::intl::LineBreaker;
using mozilla::intl::Locale;
using mozilla::intl::MozLocale;
nsLineBreaker::nsLineBreaker()
: mCurrentWordLanguage(nullptr),
@ -471,7 +471,7 @@ void nsLineBreaker::UpdateCurrentWordLanguage(nsAtom* aHyphenationLanguage) {
mScriptIsChineseOrJapanese = false;
} else {
if (aHyphenationLanguage && !mCurrentWordLanguage) {
Locale loc = Locale(nsAtomCString(aHyphenationLanguage));
MozLocale loc = MozLocale(nsAtomCString(aHyphenationLanguage));
if (loc.GetScript().IsEmpty()) {
loc.Maximize();
}

View File

@ -95,7 +95,7 @@ bool PaymentRequest::PrefEnabled(JSContext* aCx, JSObject* aObj) {
}
nsAutoCString locale;
LocaleService::GetInstance()->GetAppLocaleAsBCP47(locale);
mozilla::intl::Locale loc = mozilla::intl::Locale(locale);
mozilla::intl::MozLocale loc = mozilla::intl::MozLocale(locale);
if (!(loc.GetLanguage() == "en" && loc.GetRegion() == "US")) {
return false;
}

View File

@ -893,7 +893,7 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
// Required dictionary was not available. Try to get a dictionary
// matching at least language part of dictName.
mozilla::intl::Locale loc = mozilla::intl::Locale(dictName);
mozilla::intl::MozLocale loc = mozilla::intl::MozLocale(dictName);
nsAutoCString langCode(loc.GetLanguage());
// Try dictionary.spellchecker preference, if it starts with langCode,
@ -916,7 +916,8 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
// equlas applocation locale language.
LocaleService::GetInstance()->GetAppLocaleAsBCP47(appLocaleStr);
if (!appLocaleStr.IsEmpty()) {
mozilla::intl::Locale appLoc = mozilla::intl::Locale(appLocaleStr);
mozilla::intl::MozLocale appLoc =
mozilla::intl::MozLocale(appLocaleStr);
if (langCode.Equals(appLoc.GetLanguage())) {
BuildDictionaryList(appLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);
@ -928,7 +929,8 @@ void EditorSpellCheck::SetFallbackDictionary(DictionaryFetcher* aFetcher) {
nsAutoCString sysLocaleStr;
OSPreferences::GetInstance()->GetSystemLocale(sysLocaleStr);
if (!sysLocaleStr.IsEmpty()) {
mozilla::intl::Locale sysLoc = mozilla::intl::Locale(sysLocaleStr);
mozilla::intl::MozLocale sysLoc =
mozilla::intl::MozLocale(sysLocaleStr);
if (langCode.Equals(sysLoc.GetLanguage())) {
BuildDictionaryList(sysLocaleStr, dictList,
DICT_COMPARE_CASE_INSENSITIVE, tryDictList);

View File

@ -50,8 +50,8 @@
#include <numeric>
using namespace mozilla;
using mozilla::intl::Locale;
using mozilla::intl::LocaleService;
using mozilla::intl::MozLocale;
using mozilla::intl::OSPreferences;
#define LOG_FONTLIST(args) \
@ -480,8 +480,9 @@ bool gfxPlatformFontList::InitFontList() {
} else {
NS_DispatchToMainThread(
NS_NewRunnableFunction("font-info-updated notification callback", [] {
gfxPlatform::ForceGlobalReflow(gfxPlatform::NeedsReframe::Yes,
gfxPlatform::BroadcastToChildren::No);
gfxPlatform::ForceGlobalReflow(
gfxPlatform::NeedsReframe::Yes,
gfxPlatform::BroadcastToChildren::No);
}));
}
@ -2165,7 +2166,7 @@ void gfxPlatformFontList::AppendCJKPrefLangs(eFontPrefLang aPrefLangs[],
LocaleService::GetInstance()->GetAppLocaleAsBCP47(localeStr);
{
Locale locale(localeStr);
MozLocale locale(localeStr);
if (locale.GetLanguage().Equals("ja")) {
AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Japanese);
} else if (locale.GetLanguage().Equals("zh")) {
@ -2197,7 +2198,7 @@ void gfxPlatformFontList::AppendCJKPrefLangs(eFontPrefLang aPrefLangs[],
sysLocales, prefLocales, ""_ns,
LocaleService::kLangNegStrategyFiltering, negLocales);
for (const auto& localeStr : negLocales) {
Locale locale(localeStr);
MozLocale locale(localeStr);
if (locale.GetLanguage().Equals("ja")) {
AppendPrefLang(tempPrefLangs, tempLen, eFontPrefLang_Japanese);

View File

@ -303,8 +303,8 @@ LocaleService::Observe(nsISupports* aSubject, const char* aTopic,
bool LocaleService::LanguagesMatch(const nsACString& aRequested,
const nsACString& aAvailable) {
Locale requested = Locale(aRequested);
Locale available = Locale(aAvailable);
MozLocale requested = MozLocale(aRequested);
MozLocale available = MozLocale(aAvailable);
return requested.GetLanguage().Equals(available.GetLanguage());
}
@ -528,7 +528,7 @@ LocaleService::NegotiateLanguages(const nsTArray<nsCString>& aRequested,
}
MOZ_ASSERT(
aDefaultLocale.IsEmpty() || Locale(aDefaultLocale).IsWellFormed(),
aDefaultLocale.IsEmpty() || MozLocale(aDefaultLocale).IsWellFormed(),
"If specified, default locale must be a well-formed BCP47 language tag.");
if (aStrategy == kLangNegStrategyLookup && aDefaultLocale.IsEmpty()) {

View File

@ -8,43 +8,39 @@
using namespace mozilla::intl;
using namespace mozilla::intl::ffi;
/**
* Note: The file name is `MozLocale` to avoid compilation problems on
* case-insensitive Windows. The class name is `Locale`.
*/
Locale::Locale(const nsACString& aLocale)
MozLocale::MozLocale(const nsACString& aLocale)
: mRaw(unic_langid_new(&aLocale, &mIsWellFormed)) {}
const nsCString Locale::AsString() const {
const nsCString MozLocale::AsString() const {
nsCString tag;
unic_langid_as_string(mRaw.get(), &tag);
return tag;
}
const nsDependentCSubstring Locale::GetLanguage() const {
const nsDependentCSubstring MozLocale::GetLanguage() const {
nsDependentCSubstring sub;
unic_langid_get_language(mRaw.get(), &sub);
return sub;
}
const nsDependentCSubstring Locale::GetScript() const {
const nsDependentCSubstring MozLocale::GetScript() const {
nsDependentCSubstring sub;
unic_langid_get_script(mRaw.get(), &sub);
return sub;
}
const nsDependentCSubstring Locale::GetRegion() const {
const nsDependentCSubstring MozLocale::GetRegion() const {
nsDependentCSubstring sub;
unic_langid_get_region(mRaw.get(), &sub);
return sub;
}
void Locale::GetVariants(nsTArray<nsCString>& aRetVal) const {
void MozLocale::GetVariants(nsTArray<nsCString>& aRetVal) const {
unic_langid_get_variants(mRaw.get(), &aRetVal);
}
bool Locale::Matches(const Locale& aOther, bool aThisRange,
bool aOtherRange) const {
bool MozLocale::Matches(const MozLocale& aOther, bool aThisRange,
bool aOtherRange) const {
if (!IsWellFormed() || !aOther.IsWellFormed()) {
return false;
}
@ -52,8 +48,8 @@ bool Locale::Matches(const Locale& aOther, bool aThisRange,
return unic_langid_matches(mRaw.get(), aOther.Raw(), aThisRange, aOtherRange);
}
bool Locale::Maximize() { return unic_langid_maximize(mRaw.get()); }
bool MozLocale::Maximize() { return unic_langid_maximize(mRaw.get()); }
void Locale::ClearVariants() { unic_langid_clear_variants(mRaw.get()); }
void MozLocale::ClearVariants() { unic_langid_clear_variants(mRaw.get()); }
void Locale::ClearRegion() { unic_langid_clear_region(mRaw.get()); }
void MozLocale::ClearRegion() { unic_langid_clear_region(mRaw.get()); }

View File

@ -41,17 +41,13 @@ namespace intl {
*
* Example:
*
* Locale loc = Locale("de-at");
* MozLocale loc = MozLocale("de-at");
*
* ASSERT_TRUE(loc.GetLanguage().Equals("de"));
* ASSERT_TRUE(loc.GetScript().IsEmpty());
* ASSERT_TRUE(loc.GetRegion().Equals("AT")); // canonicalized to upper case
*
*
* Note: The file name is `MozLocale` to avoid compilation problems on
* case-insensitive Windows. The class name is `Locale`.
*/
class Locale {
class MozLocale {
public:
/**
* The constructor expects the input to be a well-formed BCP47-style locale
@ -66,8 +62,9 @@ class Locale {
* created with its flag `mWellFormed` set to false which will make the Locale
* never match.
*/
explicit Locale(const nsACString& aLocale);
explicit Locale(const char* aLocale) : Locale(nsDependentCString(aLocale)){};
explicit MozLocale(const nsACString& aLocale);
explicit MozLocale(const char* aLocale)
: MozLocale(nsDependentCString(aLocale)){};
const nsDependentCSubstring GetLanguage() const;
const nsDependentCSubstring GetScript() const;
@ -102,7 +99,8 @@ class Locale {
* locale is being treated as a range and matches any region field
* value including "US" of the other locale.
*/
bool Matches(const Locale& aOther, bool aThisRange, bool aOtherRange) const;
bool Matches(const MozLocale& aOther, bool aThisRange,
bool aOtherRange) const;
/**
* This operation uses CLDR data to build a more specific version
@ -133,14 +131,14 @@ class Locale {
/**
* Compares two locales expecting all fields to match each other.
*/
bool operator==(const Locale& aOther) {
bool operator==(const MozLocale& aOther) {
// Note: non-well-formed Locale objects are never
// treated as equal to anything
// (even other non-well-formed ones).
return Matches(aOther, false, false);
}
Locale(Locale&& aOther)
MozLocale(MozLocale&& aOther)
: mIsWellFormed(aOther.mIsWellFormed), mRaw(std::move(aOther.mRaw)) {}
ffi::LanguageIdentifier* Raw() { return mRaw.get(); }
@ -157,6 +155,6 @@ class Locale {
} // namespace intl
} // namespace mozilla
MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(mozilla::intl::Locale)
MOZ_DECLARE_RELOCATE_USING_MOVE_CONSTRUCTOR(mozilla::intl::MozLocale)
#endif /* mozilla_intl_MozLocale_h__ */

View File

@ -54,7 +54,7 @@ const Quotes* QuotesForLang(const nsAtom* aLang) {
// Try parsing lang as a Locale (which will also canonicalize case of the
// subtags), then see if we can match it with region or script subtags,
// if present, or just the primary language tag.
Locale loc(langStr);
MozLocale loc(langStr);
if (!loc.IsWellFormed()) {
return nullptr;
}

View File

@ -187,7 +187,7 @@ nsStaticAtom* nsLanguageAtomService::GetUncachedLanguageGroup(
langStr.Truncate(start.get() - langStr.BeginReading());
}
Locale loc(langStr);
MozLocale loc(langStr);
if (loc.IsWellFormed()) {
// Fill in script subtag if not present.
if (loc.GetScript().IsEmpty()) {

View File

@ -138,7 +138,7 @@ TEST(Intl_Locale_LocaleService, GetDefaultLocale)
LocaleService::GetInstance()->GetDefaultLocale(locStr);
ASSERT_FALSE(locStr.IsEmpty());
ASSERT_TRUE(Locale(locStr).IsWellFormed());
ASSERT_TRUE(MozLocale(locStr).IsWellFormed());
}
TEST(Intl_Locale_LocaleService, IsAppLocaleRTL)

View File

@ -8,24 +8,24 @@
using namespace mozilla::intl;
TEST(Intl_Locale_Locale, Locale)
TEST(Intl_MozLocale_MozLocale, MozLocale)
{
Locale loc = Locale("en-US");
MozLocale loc = MozLocale("en-US");
ASSERT_TRUE(loc.GetLanguage().Equals("en"));
ASSERT_TRUE(loc.GetRegion().Equals("US"));
}
TEST(Intl_Locale_Locale, AsString)
TEST(Intl_MozLocale_MozLocale, AsString)
{
Locale loc = Locale("ja-jp-windows");
MozLocale loc = MozLocale("ja-jp-windows");
ASSERT_TRUE(loc.AsString().Equals("ja-JP-windows"));
}
TEST(Intl_Locale_Locale, GetSubTags)
TEST(Intl_MozLocale_MozLocale, GetSubTags)
{
Locale loc = Locale("en-latn-us-macos");
MozLocale loc = MozLocale("en-latn-us-macos");
ASSERT_TRUE(loc.GetLanguage().Equals("en"));
ASSERT_TRUE(loc.GetScript().Equals("Latn"));
@ -37,32 +37,32 @@ TEST(Intl_Locale_Locale, GetSubTags)
ASSERT_TRUE(variants[0].Equals("macos"));
}
TEST(Intl_Locale_Locale, Matches)
TEST(Intl_MozLocale_MozLocale, Matches)
{
Locale loc = Locale("en-US");
MozLocale loc = MozLocale("en-US");
Locale loc2 = Locale("en-GB");
MozLocale loc2 = MozLocale("en-GB");
ASSERT_FALSE(loc == loc2);
Locale loc3 = Locale("en-US");
MozLocale loc3 = MozLocale("en-US");
ASSERT_TRUE(loc == loc3);
Locale loc4 = Locale("En_us");
MozLocale loc4 = MozLocale("En_us");
ASSERT_TRUE(loc == loc4);
}
TEST(Intl_Locale_Locale, MatchesRange)
TEST(Intl_MozLocale_MozLocale, MatchesRange)
{
Locale loc = Locale("en-US");
MozLocale loc = MozLocale("en-US");
Locale loc2 = Locale("en-Latn-US");
MozLocale loc2 = MozLocale("en-Latn-US");
ASSERT_FALSE(loc == loc2);
ASSERT_TRUE(loc.Matches(loc2, true, false));
ASSERT_FALSE(loc.Matches(loc2, false, true));
ASSERT_FALSE(loc.Matches(loc2, false, false));
ASSERT_TRUE(loc.Matches(loc2, true, true));
Locale loc3 = Locale("en");
MozLocale loc3 = MozLocale("en");
ASSERT_FALSE(loc == loc3);
ASSERT_TRUE(loc.Matches(loc3, false, true));
ASSERT_FALSE(loc.Matches(loc3, true, false));
@ -70,46 +70,46 @@ TEST(Intl_Locale_Locale, MatchesRange)
ASSERT_TRUE(loc.Matches(loc3, true, true));
}
TEST(Intl_Locale_Locale, Variants)
TEST(Intl_MozLocale_MozLocale, Variants)
{
Locale loc = Locale("en-US-UniFon-BasicEng");
MozLocale loc = MozLocale("en-US-UniFon-BasicEng");
// Make sure that we canonicalize and sort variant tags
ASSERT_TRUE(loc.AsString().Equals("en-US-basiceng-unifon"));
}
TEST(Intl_Locale_Locale, InvalidLocale)
TEST(Intl_MozLocale_MozLocale, InvalidMozLocale)
{
Locale loc = Locale("en-verylongsubtag");
MozLocale loc = MozLocale("en-verylongsubtag");
ASSERT_FALSE(loc.IsWellFormed());
Locale loc2 = Locale("p-te");
MozLocale loc2 = MozLocale("p-te");
ASSERT_FALSE(loc2.IsWellFormed());
}
TEST(Intl_Locale_Locale, ClearRegion)
TEST(Intl_MozLocale_MozLocale, ClearRegion)
{
Locale loc = Locale("en-US");
MozLocale loc = MozLocale("en-US");
loc.ClearRegion();
ASSERT_TRUE(loc.AsString().Equals("en"));
}
TEST(Intl_Locale_Locale, ClearVariants)
TEST(Intl_MozLocale_MozLocale, ClearVariants)
{
Locale loc = Locale("en-US-windows");
MozLocale loc = MozLocale("en-US-windows");
loc.ClearVariants();
ASSERT_TRUE(loc.AsString().Equals("en-US"));
}
TEST(Intl_Locale_Locale, jaJPmac)
TEST(Intl_MozLocale_MozLocale, jaJPmac)
{
Locale loc = Locale("ja-JP-mac");
MozLocale loc = MozLocale("ja-JP-mac");
ASSERT_TRUE(loc.AsString().Equals("ja-JP-macos"));
}
TEST(Intl_Locale_Locale, Maximize)
TEST(Intl_MozLocale_MozLocale, Maximize)
{
Locale loc = Locale("en");
MozLocale loc = MozLocale("en");
ASSERT_TRUE(loc.GetLanguage().Equals("en"));
ASSERT_TRUE(loc.GetScript().IsEmpty());

View File

@ -64,9 +64,9 @@ bool OSPreferences::ReadSystemLocales(nsTArray<nsCString>& aLocaleList) {
// language code with no region subtag, but the
// GlobalizationPreferences API may give us one (e.g. "ja").
// So if there's no hyphen in the string at this point, we use
// Locale::Maximize to get a suitable region code to
// MozLocale::Maximize to get a suitable region code to
// go with it.
Locale locale(loc);
MozLocale locale(loc);
if (locale.Maximize() && !locale.GetRegion().IsEmpty()) {
loc.Append('-');
loc.Append(locale.GetRegion());