Bug 1869647 - Mark hasStorageAccess.sub.https.window.html as intermittent after wpt...
[gecko.git] / intl / locale / Quotes.cpp
blob87c47bfa5ef0a99dce0d3b17d36a3fdb5211e298
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode:nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "Quotes.h"
7 #include "mozilla/ClearOnShutdown.h"
8 #include "mozilla/StaticPtr.h"
9 #include "mozilla/intl/Locale.h"
10 #include "nsTHashMap.h"
11 #include "nsPrintfCString.h"
13 using namespace mozilla;
14 using namespace mozilla::intl;
16 namespace {
17 struct LangQuotesRec {
18 const char* mLangs;
19 Quotes mQuotes;
22 #include "cldr-quotes.inc"
24 static StaticAutoPtr<nsTHashMap<nsCStringHashKey, Quotes>> sQuotesForLang;
25 } // anonymous namespace
27 namespace mozilla {
28 namespace intl {
30 const Quotes* QuotesForLang(const nsAtom* aLang) {
31 MOZ_ASSERT(NS_IsMainThread());
33 // On first use, initialize the hashtable from our CLDR-derived data array.
34 if (!sQuotesForLang) {
35 sQuotesForLang = new nsTHashMap<nsCStringHashKey, Quotes>(32);
36 ClearOnShutdown(&sQuotesForLang);
37 for (const auto& i : sLangQuotes) {
38 const char* s = i.mLangs;
39 size_t len;
40 while ((len = strlen(s))) {
41 sQuotesForLang->InsertOrUpdate(nsDependentCString(s, len), i.mQuotes);
42 s += len + 1;
47 nsAtomCString langStr(aLang);
48 const Quotes* entry = sQuotesForLang->Lookup(langStr).DataPtrOrNull();
49 if (entry) {
50 // Found an exact match for the requested lang.
51 return entry;
54 // Try parsing lang as a Locale and canonicalizing the subtags, then see if
55 // we can match it with region or script subtags, if present, or just the
56 // primary language tag.
57 Locale loc;
58 auto result = LocaleParser::TryParse(langStr, loc);
59 if (result.isErr()) {
60 return nullptr;
62 if (loc.Canonicalize().isErr()) {
63 return nullptr;
65 if (loc.Region().Present()) {
66 nsAutoCString langAndRegion;
67 langAndRegion.Append(loc.Language().Span());
68 langAndRegion.Append('-');
69 langAndRegion.Append(loc.Region().Span());
70 if ((entry = sQuotesForLang->Lookup(langAndRegion).DataPtrOrNull())) {
71 return entry;
74 if (loc.Script().Present()) {
75 nsAutoCString langAndScript;
76 langAndScript.Append(loc.Language().Span());
77 langAndScript.Append('-');
78 langAndScript.Append(loc.Script().Span());
79 if ((entry = sQuotesForLang->Lookup(langAndScript).DataPtrOrNull())) {
80 return entry;
83 Span<const char> langAsSpan = loc.Language().Span();
84 nsAutoCString lang(langAsSpan.data(), langAsSpan.size());
85 if ((entry = sQuotesForLang->Lookup(lang).DataPtrOrNull())) {
86 return entry;
89 return nullptr;
92 } // namespace intl
93 } // namespace mozilla