Bug 1300825 - Use more SprintfLiteral in jsdate. r=till
authorAndré Bargull <andre.bargull@gmail.com>
Wed, 05 Oct 2016 03:24:31 -0700
changeset 316651 0512a9e149d6f7fd6e72097200475ed5375b3a44
parent 316650 291a6f2dc42e32f0fe2a5729ae8d7df13cbb99fd
child 316652 f9fadd6ed3c190b55c4753a24fe1e95a1a3c4ebc
push id82508
push usercbook@mozilla.com
push dateThu, 06 Oct 2016 06:27:00 +0000
treeherdermozilla-inbound@67b189765ba9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstill
bugs1300825
milestone52.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 1300825 - Use more SprintfLiteral in jsdate. r=till
js/src/jsdate.cpp
--- a/js/src/jsdate.cpp
+++ b/js/src/jsdate.cpp
@@ -2422,72 +2422,39 @@ static const char * const days[] =
 {
    "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
 };
 static const char * const months[] =
 {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 };
 
-
-static void
-print_gmt_string(char* buf, size_t size, double utctime)
-{
-    MOZ_ASSERT(NumbersAreIdentical(TimeClip(utctime).toDouble(), utctime));
-    snprintf(buf, size, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
-             days[int(WeekDay(utctime))],
-             int(DateFromTime(utctime)),
-             months[int(MonthFromTime(utctime))],
-             int(YearFromTime(utctime)),
-             int(HourFromTime(utctime)),
-             int(MinFromTime(utctime)),
-             int(SecFromTime(utctime)));
-}
-
-static void
-print_iso_string(char* buf, size_t size, double utctime)
-{
-    MOZ_ASSERT(NumbersAreIdentical(TimeClip(utctime).toDouble(), utctime));
-    snprintf(buf, size, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
-             int(YearFromTime(utctime)),
-             int(MonthFromTime(utctime)) + 1,
-             int(DateFromTime(utctime)),
-             int(HourFromTime(utctime)),
-             int(MinFromTime(utctime)),
-             int(SecFromTime(utctime)),
-             int(msFromTime(utctime)));
-}
-
-static void
-print_iso_extended_string(char* buf, size_t size, double utctime)
-{
-    MOZ_ASSERT(NumbersAreIdentical(TimeClip(utctime).toDouble(), utctime));
-    snprintf(buf, size, "%+.6d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
-             int(YearFromTime(utctime)),
-             int(MonthFromTime(utctime)) + 1,
-             int(DateFromTime(utctime)),
-             int(HourFromTime(utctime)),
-             int(MinFromTime(utctime)),
-             int(SecFromTime(utctime)),
-             int(msFromTime(utctime)));
-}
-
 /* ES5 B.2.6. */
 MOZ_ALWAYS_INLINE bool
 date_toGMTString_impl(JSContext* cx, const CallArgs& args)
 {
     double utctime = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
 
     char buf[100];
-    if (!IsFinite(utctime))
-        SprintfLiteral(buf, js_NaN_date_str);
-    else
-        print_gmt_string(buf, sizeof buf, utctime);
-
-    JSString* str = JS_NewStringCopyZ(cx, buf);
+    JSString* str;
+    if (!IsFinite(utctime)) {
+        str = NewStringCopyZ<CanGC>(cx, js_NaN_date_str);
+    } else {
+        SprintfLiteral(buf, "%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
+                       days[int(WeekDay(utctime))],
+                       int(DateFromTime(utctime)),
+                       months[int(MonthFromTime(utctime))],
+                       int(YearFromTime(utctime)),
+                       int(HourFromTime(utctime)),
+                       int(MinFromTime(utctime)),
+                       int(SecFromTime(utctime)));
+
+        str = NewStringCopyZ<CanGC>(cx, buf);
+    }
+
     if (!str)
         return false;
     args.rval().setString(str);
     return true;
 }
 
 static bool
 date_toGMTString(JSContext* cx, unsigned argc, Value* vp)
@@ -2503,22 +2470,37 @@ date_toISOString_impl(JSContext* cx, con
     double utctime = args.thisv().toObject().as<DateObject>().UTCTime().toNumber();
     if (!IsFinite(utctime)) {
         JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr, JSMSG_INVALID_DATE);
         return false;
     }
 
     char buf[100];
     int year = int(YearFromTime(utctime));
-    if (year < 0 || year > 9999)
-        print_iso_extended_string(buf, sizeof buf, utctime);
-    else
-        print_iso_string(buf, sizeof buf, utctime);
-
-    JSString* str = JS_NewStringCopyZ(cx, buf);
+    if (year < 0 || year > 9999) {
+        SprintfLiteral(buf, "%+.6d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
+                       int(YearFromTime(utctime)),
+                       int(MonthFromTime(utctime)) + 1,
+                       int(DateFromTime(utctime)),
+                       int(HourFromTime(utctime)),
+                       int(MinFromTime(utctime)),
+                       int(SecFromTime(utctime)),
+                       int(msFromTime(utctime)));
+    } else {
+        SprintfLiteral(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%.3dZ",
+                       int(YearFromTime(utctime)),
+                       int(MonthFromTime(utctime)) + 1,
+                       int(DateFromTime(utctime)),
+                       int(HourFromTime(utctime)),
+                       int(MinFromTime(utctime)),
+                       int(SecFromTime(utctime)),
+                       int(msFromTime(utctime)));
+    }
+
+    JSString* str = NewStringCopyZ<CanGC>(cx, buf);
     if (!str)
         return false;
     args.rval().setString(str);
     return true;
 
 }
 
 static bool
@@ -2594,18 +2576,19 @@ static bool
 FormatDate(JSContext* cx, double utcTime, formatspec format, MutableHandleValue rval)
 {
     char buf[100];
     char tzbuf[100];
     bool usetz;
     size_t i, tzlen;
     PRMJTime prtm;
 
+    JSString* str;
     if (!IsFinite(utcTime)) {
-        SprintfLiteral(buf, js_NaN_date_str);
+        str = NewStringCopyZ<CanGC>(cx, js_NaN_date_str);
     } else {
         MOZ_ASSERT(NumbersAreIdentical(TimeClip(utcTime).toDouble(), utcTime));
 
         double localTime = LocalTime(utcTime);
 
         /*
          * Offset from GMT in minutes.  The offset includes daylight
          * savings, if it applies.
@@ -2686,33 +2669,34 @@ FormatDate(JSContext* cx, double utcTime
                            int(HourFromTime(localTime)),
                            int(MinFromTime(localTime)),
                            int(SecFromTime(localTime)),
                            offset,
                            usetz ? " " : "",
                            usetz ? tzbuf : "");
             break;
         }
+
+        str = NewStringCopyZ<CanGC>(cx, buf);
     }
 
-    JSString* str = JS_NewStringCopyZ(cx, buf);
     if (!str)
         return false;
     rval.setString(str);
     return true;
 }
 
 static bool
 ToLocaleFormatHelper(JSContext* cx, HandleObject obj, const char* format, MutableHandleValue rval)
 {
     double utcTime = obj->as<DateObject>().UTCTime().toNumber();
 
     char buf[100];
     if (!IsFinite(utcTime)) {
-        SprintfLiteral(buf, js_NaN_date_str);
+        strcpy(buf, js_NaN_date_str);
     } else {
         int result_len;
         double localTime = LocalTime(utcTime);
         PRMJTime prtm;
         ToPRMJTime(localTime, &prtm);
 
         /* Let PRMJTime format it. */
         result_len = PRMJ_FormatTime(buf, sizeof buf, format, &prtm);
@@ -2727,26 +2711,25 @@ ToLocaleFormatHelper(JSContext* cx, Hand
                hack end of 3/11/22 or 11.03.22 or 11Mar22 to use 4-digit yr...*/
             !isdigit(buf[result_len - 3]) &&
             isdigit(buf[result_len - 2]) && isdigit(buf[result_len - 1]) &&
             /* ...but not if starts with 4-digit year, like 2022/3/11. */
             !(isdigit(buf[0]) && isdigit(buf[1]) &&
               isdigit(buf[2]) && isdigit(buf[3]))) {
             double localtime = obj->as<DateObject>().cachedLocalTime();
             int year = IsNaN(localtime) ? 0 : (int) YearFromTime(localtime);
-            snprintf(buf + (result_len - 2), (sizeof buf) - (result_len - 2),
-                     "%d", year);
+            snprintf(buf + (result_len - 2), (sizeof buf) - (result_len - 2), "%d", year);
         }
 
     }
 
     if (cx->runtime()->localeCallbacks && cx->runtime()->localeCallbacks->localeToUnicode)
         return cx->runtime()->localeCallbacks->localeToUnicode(cx, buf, rval);
 
-    JSString* str = JS_NewStringCopyZ(cx, buf);
+    JSString* str = NewStringCopyZ<CanGC>(cx, buf);
     if (!str)
         return false;
     rval.setString(str);
     return true;
 }
 
 #if !EXPOSE_INTL_API
 static bool