[4/4] jscript: Simplify create_utc_string and add basic tests.

Sebastian Lackner sebastian at fds-team.de
Sun Sep 11 11:50:26 CDT 2016


Signed-off-by: Sebastian Lackner <sebastian at fds-team.de>
---
 dlls/jscript/date.c       |   39 +++++++++++----------------------------
 dlls/jscript/tests/api.js |    4 ++++
 2 files changed, 15 insertions(+), 28 deletions(-)

diff --git a/dlls/jscript/date.c b/dlls/jscript/date.c
index a9d2ab4..a66a286 100644
--- a/dlls/jscript/date.c
+++ b/dlls/jscript/date.c
@@ -654,9 +654,10 @@ static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis, jsva
 
     BOOL formatAD = TRUE;
     WCHAR week[64], month[64];
+    WCHAR buf[192];
     DateInstance *date;
     jsstr_t *date_str;
-    int len, size, year, day;
+    int year, day;
     DWORD lcid_en;
 
     if(!(date = date_this(jsthis)))
@@ -669,48 +670,30 @@ static inline HRESULT create_utc_string(script_ctx_t *ctx, vdisp_t *jsthis, jsva
     }
 
     if(r) {
-        WCHAR *ptr;
-
-        len = 17;
-
         lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
 
-        size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(date->time)], week, sizeof(week)/sizeof(*week));
-        len += size-1;
-
-        size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(date->time)], month, sizeof(month)/sizeof(*month));
-        len += size-1;
+        week[0] = 0;
+        GetLocaleInfoW(lcid_en, week_ids[(int)week_day(date->time)], week, sizeof(week)/sizeof(*week));
 
-        year = year_from_time(date->time);
-        if(year<0)
-            year = -year+1;
-        do {
-            year /= 10;
-            len++;
-        } while(year);
+        month[0] = 0;
+        GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(date->time)], month, sizeof(month)/sizeof(*month));
 
         year = year_from_time(date->time);
         if(year<0) {
             formatAD = FALSE;
             year = -year+1;
-            len += 5;
         }
 
         day = date_from_time(date->time);
-        do {
-            day /= 10;
-            len++;
-        } while(day);
-        day = date_from_time(date->time);
-
-        date_str = jsstr_alloc_buf(len, &ptr);
-        if(!date_str)
-            return E_OUTOFMEMORY;
 
-        sprintfW(ptr, formatAD?formatADW:formatBCW, week, day, month, year,
+        sprintfW(buf, formatAD ? formatADW : formatBCW, week, day, month, year,
                 (int)hour_from_time(date->time), (int)min_from_time(date->time),
                 (int)sec_from_time(date->time));
 
+        date_str = jsstr_alloc(buf);
+        if(!date_str)
+            return E_OUTOFMEMORY;
+
         *r = jsval_string(date_str);
     }
     return S_OK;
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index 40a749b..5a78b3a 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -2151,6 +2151,8 @@ ok(Date.parse("Tue, 22 Mar 2016 09:57:55 -0300") === Date.parse("Tue, 22 Mar 201
 ok(Date.parse("Tue, 22 Mar 2016 09:57:55 +0400") === Date.parse("Tue, 22 Mar 2016 09:57:55 UTC+0400"),
         "Date.parse(\"Tue, 22 Mar 2016 09:57:55 +0400\") = " + Date.parse("Tue, 22 Mar 2016 09:57:55 +0400"));
 
+tmp = (new Date()).toGMTString();
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 tmp = (new Date()).toLocaleDateString();
 ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 tmp = (new Date(1600, 1, 1, 0, 0, 0, 0)).toLocaleDateString();
@@ -2163,6 +2165,8 @@ tmp = (new Date()).toString();
 ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 tmp = (new Date()).toTimeString();
 ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
+tmp = (new Date()).toUTCString();
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 
 ok(typeof(Math.PI) === "number", "typeof(Math.PI) = " + typeof(Math.PI));
 ok(Math.floor(Math.PI*100) === 314, "Math.PI = " + Math.PI);
-- 
2.9.0



More information about the wine-patches mailing list