Skip to content

Commit

Permalink
Bug 1719746 - Rename Locale to MozLocale; r=platform-i18n-reviewers,z…
Browse files Browse the repository at this point in the history
…braniecki

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
  • Loading branch information
dminor committed Oct 18, 2021
1 parent d7c4963 commit a9e674d
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 74 deletions.
4 changes: 2 additions & 2 deletions dom/base/nsLineBreaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion dom/payments/PaymentRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 5 additions & 3 deletions editor/spellchecker/EditorSpellCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions gfx/thebes/gfxPlatformFontList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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);
}));
}

Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions intl/locale/LocaleService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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()) {
Expand Down
26 changes: 11 additions & 15 deletions intl/locale/MozLocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,48 @@
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;
}

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()); }
22 changes: 10 additions & 12 deletions intl/locale/MozLocale.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(); }
Expand All @@ -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__ */
2 changes: 1 addition & 1 deletion intl/locale/Quotes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion intl/locale/nsLanguageAtomService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion intl/locale/tests/gtest/TestLocaleService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit a9e674d

Please sign in to comment.