Bug 1448786 - Avoid UTF8ToUnicodeBuffer in OSPreferences_win.cpp. draft
authorHenri Sivonen <hsivonen@hsivonen.fi>
Mon, 26 Mar 2018 15:42:27 +0300
changeset 773019 bf82fa4a0c269c46441189958ab3816d850b25ac
parent 772548 17c49c1edac74c7feab7846498876d3a60983760
push id104105
push userbmo:hsivonen@hsivonen.fi
push dateTue, 27 Mar 2018 07:44:20 +0000
bugs1448786
milestone61.0a1
Bug 1448786 - Avoid UTF8ToUnicodeBuffer in OSPreferences_win.cpp. MozReview-Commit-ID: 7a11ogGXo8E
intl/locale/windows/OSPreferences_win.cpp
--- a/intl/locale/windows/OSPreferences_win.cpp
+++ b/intl/locale/windows/OSPreferences_win.cpp
@@ -128,55 +128,55 @@ ToTimeLCType(OSPreferences::DateTimeForm
  * for combined date/time string, since Windows API does not provide an
  * option for this.
  */
 bool
 OSPreferences::ReadDateTimePattern(DateTimeFormatStyle aDateStyle,
                                    DateTimeFormatStyle aTimeStyle,
                                    const nsACString& aLocale, nsAString& aRetVal)
 {
-  WCHAR localeName[LOCALE_NAME_MAX_LENGTH];
-  UTF8ToUnicodeBuffer(aLocale, (char16_t*)localeName);
+  nsAutoString localeName;
+  CopyASCIItoUTF16(aLocale, localeName);
 
   bool isDate = aDateStyle != DateTimeFormatStyle::None &&
                 aDateStyle != DateTimeFormatStyle::Invalid;
   bool isTime = aTimeStyle != DateTimeFormatStyle::None &&
                 aTimeStyle != DateTimeFormatStyle::Invalid;
 
   // If both date and time are wanted, we'll initially read them into a
   // local string, and then insert them into the overall date+time pattern;
   // but if only one is needed we'll work directly with the return value.
   // Set 'str' to point to the string we will use to retrieve patterns
   // from Windows.
   nsAutoString tmpStr;
   nsAString* str;
   if (isDate && isTime) {
-    if (!GetDateTimeConnectorPattern(NS_ConvertUTF16toUTF8(localeName), aRetVal)) {
+    if (!GetDateTimeConnectorPattern(aLocale, aRetVal)) {
       NS_WARNING("failed to get date/time connector");
       aRetVal.AssignLiteral(u"{1} {0}");
     }
     str = &tmpStr;
   } else if (isDate || isTime) {
     str = &aRetVal;
   } else {
     aRetVal.Truncate(0);
     return true;
   }
 
   if (isDate) {
     LCTYPE lcType = ToDateLCType(aDateStyle);
-    size_t len = GetLocaleInfoEx(localeName, lcType, nullptr, 0);
+    size_t len = GetLocaleInfoEx(reinterpret_cast<const wchar_t*>(localeName.BeginReading()), lcType, nullptr, 0);
     if (len == 0) {
       return false;
     }
 
     // We're doing it to ensure the terminator will fit when Windows writes the data
     // to its output buffer. See bug 1358159 for details.
     str->SetLength(len);
-    GetLocaleInfoEx(localeName, lcType, (WCHAR*)str->BeginWriting(), len);
+    GetLocaleInfoEx(reinterpret_cast<const wchar_t*>(localeName.BeginReading()), lcType, (WCHAR*)str->BeginWriting(), len);
     str->SetLength(len - 1); // -1 because len counts the null terminator
 
     // Windows uses "ddd" and "dddd" for abbreviated and full day names respectively,
     //   https://msdn.microsoft.com/en-us/library/windows/desktop/dd317787(v=vs.85).aspx
     // but in a CLDR/ICU-style pattern these should be "EEE" and "EEEE".
     //   http://userguide.icu-project.org/formatparse/datetime
     // So we fix that up here.
     nsAString::const_iterator start, pos, end;
@@ -212,25 +212,25 @@ OSPreferences::ReadDateTimePattern(DateT
       if (FindInReadable(NS_LITERAL_STRING("{1}"), pos, end)) {
         aRetVal.Replace(pos - start, 3, tmpStr);
       }
     }
   }
 
   if (isTime) {
     LCTYPE lcType = ToTimeLCType(aTimeStyle);
-    size_t len = GetLocaleInfoEx(localeName, lcType, nullptr, 0);
+    size_t len = GetLocaleInfoEx(reinterpret_cast<const wchar_t*>(localeName.BeginReading()), lcType, nullptr, 0);
     if (len == 0) {
       return false;
     }
 
     // We're doing it to ensure the terminator will fit when Windows writes the data
     // to its output buffer. See bug 1358159 for details.
     str->SetLength(len);
-    GetLocaleInfoEx(localeName, lcType, (WCHAR*)str->BeginWriting(), len);
+    GetLocaleInfoEx(reinterpret_cast<const wchar_t*>(localeName.BeginReading()), lcType, (WCHAR*)str->BeginWriting(), len);
     str->SetLength(len - 1);
 
     // Windows uses "t" or "tt" for a "time marker" (am/pm indicator),
     //   https://msdn.microsoft.com/en-us/library/windows/desktop/dd318148(v=vs.85).aspx
     // but in a CLDR/ICU-style pattern that should be "a".
     //   http://userguide.icu-project.org/formatparse/datetime
     // So we fix that up here.
     int32_t index = str->FindChar('t');