Bug 1418743 - Stop using Encoding::ForName in FallbackEncoding.cpp. r?hsivonen draft
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Wed, 15 Nov 2017 22:56:08 +0900
changeset 707528 46c58e31e6fa05145fa0c416728ba4c647c1573e
parent 707503 b4cef8d1dff06a1ec2b9bb17211c0c3c7f5b76fa
child 707529 83895d04c4d5982b7387c9d53839a07a54eee285
push id92142
push userVYV03354@nifty.ne.jp
push dateTue, 05 Dec 2017 13:10:45 +0000
reviewershsivonen
bugs1418743
milestone59.0a1
Bug 1418743 - Stop using Encoding::ForName in FallbackEncoding.cpp. r?hsivonen MozReview-Commit-ID: EsUdqCvSlBf
dom/encoding/FallbackEncoding.cpp
dom/encoding/encodings2arrays.py
dom/encoding/moz.build
--- a/dom/encoding/FallbackEncoding.cpp
+++ b/dom/encoding/FallbackEncoding.cpp
@@ -13,21 +13,42 @@
 #include "nsIObserverService.h"
 #include "nsUConvPropertySearch.h"
 
 using mozilla::intl::LocaleService;
 
 namespace mozilla {
 namespace dom {
 
-static constexpr nsUConvProp localesFallbacks[] = {
+struct EncodingProp
+{
+  const char* const mKey;
+  NotNull<const Encoding*> mValue;
+};
+
+template <int32_t N>
+static NotNull<const Encoding*>
+SearchEncodingProp(const EncodingProp (&aProperties)[N],
+                   const nsACString& aKey)
+{
+  const nsCString& flat = PromiseFlatCString(aKey);
+  size_t index;
+  if (!BinarySearchIf(aProperties, 0, ArrayLength(aProperties),
+                      [&flat](const EncodingProp& aProperty)
+                      { return flat.Compare(aProperty.mKey); }, &index)) {
+    return WINDOWS_1252_ENCODING;
+  }
+  return aProperties[index].mValue;
+}
+
+static const EncodingProp localesFallbacks[] = {
 #include "localesfallbacks.properties.h"
 };
 
-static constexpr nsUConvProp domainsFallbacks[] = {
+static const EncodingProp domainsFallbacks[] = {
 #include "domainsfallbacks.properties.h"
 };
 
 static constexpr nsUConvProp nonParticipatingDomains[] = {
 #include "nonparticipatingdomains.properties.h"
 };
 
 NS_IMPL_ISUPPORTS(FallbackEncoding, nsIObserver)
@@ -86,25 +107,20 @@ FallbackEncoding::Get()
 
   // Throw away regions and other variants to accommodate weird stuff seen
   // in telemetry--apparently unofficial language packs.
   int32_t index = locale.FindChar('-');
   if (index >= 0) {
     locale.Truncate(index);
   }
 
-  nsAutoCString fallback;
-  if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
-      localesFallbacks, ArrayLength(localesFallbacks), locale, fallback))) {
-    mFallback = WINDOWS_1252_ENCODING;
-  } else {
-    mFallback = Encoding::ForName(fallback);
-  }
+  auto fallback = SearchEncodingProp(localesFallbacks, locale);
+  mFallback = fallback;
 
-  return WrapNotNull(mFallback);
+  return fallback;
 }
 
 NotNull<const Encoding*>
 FallbackEncoding::FromLocale()
 {
   MOZ_ASSERT(FallbackEncoding::sInstance,
              "Using uninitialized fallback cache.");
   return FallbackEncoding::sInstance->Get();
@@ -171,18 +187,13 @@ FallbackEncoding::IsParticipatingTopLeve
       ArrayLength(nonParticipatingDomains),
       aTLD,
       dummy));
 }
 
 NotNull<const Encoding*>
 FallbackEncoding::FromTopLevelDomain(const nsACString& aTLD)
 {
-  nsAutoCString fallback;
-  if (NS_FAILED(nsUConvPropertySearch::SearchPropertyValue(
-      domainsFallbacks, ArrayLength(domainsFallbacks), aTLD, fallback))) {
-    return WINDOWS_1252_ENCODING;
-  }
-  return Encoding::ForName(fallback);
+  return SearchEncodingProp(domainsFallbacks, aTLD);
 }
 
 } // namespace dom
 } // namespace mozilla
copy from intl/locale/props2arrays.py
copy to dom/encoding/encodings2arrays.py
--- a/intl/locale/props2arrays.py
+++ b/dom/encoding/encodings2arrays.py
@@ -16,12 +16,12 @@ def main(header, propFile):
           mappings[parts[0].strip()] = parts[1].strip()
  
   keys = mappings.keys()
   keys.sort()
 
   header.write("// This is a generated file. Please do not edit.\n")
   header.write("// Please edit the corresponding .properties file instead.\n")
 
-  entries = ['{ "%s", "%s", %d }'
-             % (key, mappings[key], len(mappings[key])) for key in keys]
+  entries = ['{ "%s", %s }'
+             % (key, mappings[key].replace('-', '_').upper() + '_ENCODING') for key in keys]
   header.write(',\n'.join(entries) + '\n')
 
--- a/dom/encoding/moz.build
+++ b/dom/encoding/moz.build
@@ -19,29 +19,35 @@ UNIFIED_SOURCES += [
     'TextEncoder.cpp',
 ]
 
 FINAL_LIBRARY = 'xul'
 LOCAL_INCLUDES += [
     '/intl/locale',
 ]
 
-props2arrays = '/intl/locale/props2arrays.py'
+props2arrays = 'encodings2arrays.py'
 prefixes = (
     'domainsfallbacks',
     'labelsencodings',
     'localesfallbacks',
-    'nonparticipatingdomains',
 )
 
 for prefix in prefixes:
     input_file = prefix + '.properties'
     header = prefix + '.properties.h'
     GENERATED_FILES += [header]
     props = GENERATED_FILES[header]
     props.script = props2arrays
     props.inputs = [input_file]
 
+input_file = 'nonparticipatingdomains.properties'
+header = input_file + '.h'
+GENERATED_FILES += [header]
+props = GENERATED_FILES[header]
+props.script = '../../intl/locale/props2arrays.py'
+props.inputs = [input_file]
+
 MOCHITEST_MANIFESTS += [
     'test/mochitest.ini',
 ]
 MOCHITEST_CHROME_MANIFESTS += ['test/chrome.ini']
 XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']