Bug 1426907 - Make OSPreferences::GetPatternForSkeleton public and use in DateTimeFormat; r=zbraniecki
authorDan Minor <dminor@mozilla.com>
Tue, 27 Oct 2020 20:45:17 +0000
changeset 554795 e1137eef3ae34db7527e4c9c2213167b148224f3
parent 554794 4602056737ce4a30f9ffb8572f43259307e04d61
child 554796 1a5d1964ff105ec4c7c5895282c4e900d97207dc
push id37898
push userabutkovits@mozilla.com
push dateWed, 28 Oct 2020 09:24:21 +0000
treeherdermozilla-central@83bf4fd3b1fb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerszbraniecki
bugs1426907
milestone84.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1426907 - Make OSPreferences::GetPatternForSkeleton public and use in DateTimeFormat; r=zbraniecki This allows for the removal of duplicated code between DateTimeFormat and OSPreferences. Differential Revision: https://phabricator.services.mozilla.com/D94432
intl/locale/DateTimeFormat.cpp
intl/locale/OSPreferences.h
--- a/intl/locale/DateTimeFormat.cpp
+++ b/intl/locale/DateTimeFormat.cpp
@@ -69,51 +69,31 @@ nsresult DateTimeFormat::FormatDateTime(
     return rv;
   }
 
   aStringOut.Truncate();
 
   UErrorCode status = U_ZERO_ERROR;
 
   nsAutoString skeleton;
-  nsAutoString pattern;
-
   switch (aSkeleton) {
     case Skeleton::yyyyMM:
       skeleton.AssignASCII("yyyyMM");
       break;
     case Skeleton::yyyyMMMM:
       skeleton.AssignASCII("yyyyMMMM");
       break;
     default:
       MOZ_ASSERT_UNREACHABLE("Unhandled skeleton enum");
   }
 
-  UDateTimePatternGenerator* patternGenerator =
-      udatpg_open(mLocale->get(), &status);
-  if (U_SUCCESS(status)) {
-    int32_t patternLength;
-    pattern.SetLength(DATETIME_FORMAT_INITIAL_LEN);
-    patternLength = udatpg_getBestPattern(
-        patternGenerator,
-        reinterpret_cast<const UChar*>(skeleton.BeginReading()),
-        skeleton.Length(), reinterpret_cast<UChar*>(pattern.BeginWriting()),
-        DATETIME_FORMAT_INITIAL_LEN, &status);
-    pattern.SetLength(patternLength);
-
-    if (status == U_BUFFER_OVERFLOW_ERROR) {
-      status = U_ZERO_ERROR;
-      udatpg_getBestPattern(
-          patternGenerator,
-          reinterpret_cast<const UChar*>(skeleton.BeginReading()),
-          skeleton.Length(), reinterpret_cast<UChar*>(pattern.BeginWriting()),
-          patternLength, &status);
-    }
+  nsAutoString pattern;
+  if (!OSPreferences::GetPatternForSkeleton(skeleton, *mLocale, pattern)) {
+    return NS_ERROR_FAILURE;
   }
-  udatpg_close(patternGenerator);
 
   nsAutoString timeZoneID;
   BuildTimeZoneString(aExplodedTime->tm_params, timeZoneID);
 
   UDateFormat* dateTimeFormat =
       udat_open(UDAT_PATTERN, UDAT_PATTERN, mLocale->get(),
                 reinterpret_cast<const UChar*>(timeZoneID.BeginReading()),
                 timeZoneID.Length(),
--- a/intl/locale/OSPreferences.h
+++ b/intl/locale/OSPreferences.h
@@ -72,16 +72,20 @@ class OSPreferences : public mozIOSPrefe
   /**
    * Return an addRef'd pointer to the singleton instance. This is used by the
    * XPCOM constructor that exists to support usage from JS.
    */
   static already_AddRefed<OSPreferences> GetInstanceAddRefed() {
     return RefPtr<OSPreferences>(GetInstance()).forget();
   }
 
+  static bool GetPatternForSkeleton(const nsAString& aSkeleton,
+                                    const nsACString& aLocale,
+                                    nsAString& aRetVal);
+
   static bool GetDateTimeConnectorPattern(const nsACString& aLocale,
                                           nsAString& aRetVal);
 
   /**
    * Triggers a refresh of retrieving data from host environment.
    *
    * If the result differs from the previous list, it will additionally
    * trigger global events for changed values:
@@ -116,19 +120,16 @@ class OSPreferences : public mozIOSPrefe
                                   const nsACString& aLocale,
                                   nsAString& aRetVal);
 
   bool GetDateTimeSkeletonForStyle(DateTimeFormatStyle aDateStyle,
                                    DateTimeFormatStyle aTimeStyle,
                                    const nsACString& aLocale,
                                    nsAString& aRetVal);
 
-  bool GetPatternForSkeleton(const nsAString& aSkeleton,
-                             const nsACString& aLocale, nsAString& aRetVal);
-
   /**
    * This is a host environment specific method that will be implemented
    * separately for each platform.
    *
    * It is only called when the cache is empty or invalidated.
    *
    * The return value indicates whether the function successfully
    * resolved at least one locale.