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

Sebastian Lackner sebastian at fds-team.de
Sun Sep 11 11:48:54 CDT 2016


Signed-off-by: Sebastian Lackner <sebastian at fds-team.de>
---

No bug in this case, but those length computations make the code unnecessary complex.
The same also applies to the next patch.

 dlls/jscript/date.c       |   48 +++++++++++++---------------------------------
 dlls/jscript/tests/api.js |    4 +++
 2 files changed, 18 insertions(+), 34 deletions(-)

diff --git a/dlls/jscript/date.c b/dlls/jscript/date.c
index 6553e80..a9d2ab4 100644
--- a/dlls/jscript/date.c
+++ b/dlls/jscript/date.c
@@ -494,8 +494,9 @@ static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset,
 
     BOOL formatAD = TRUE;
     WCHAR week[64], month[64];
+    WCHAR buf[192];
     jsstr_t *date_jsstr;
-    int len, size, year, day;
+    int year, day;
     DWORD lcid_en;
     WCHAR sign = '-';
 
@@ -506,66 +507,45 @@ static inline HRESULT date_to_string(DOUBLE time, BOOL show_offset, int offset,
     }
 
     if(r) {
-        WCHAR *date_str;
-
-        len = 21;
-
         lcid_en = MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT);
 
-        size = GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week));
-        assert(size);
-        len += size-1;
-
-        size = GetLocaleInfoW(lcid_en, month_ids[(int)month_from_time(time)], month, sizeof(month)/sizeof(*month));
-        len += size-1;
+        week[0] = 0;
+        GetLocaleInfoW(lcid_en, week_ids[(int)week_day(time)], week, sizeof(week)/sizeof(*week));
 
-        year = year_from_time(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(time)], month, sizeof(month)/sizeof(*month));
 
         year = year_from_time(time);
         if(year<0) {
             formatAD = FALSE;
             year = -year+1;
-            len += 5;
         }
 
         day = date_from_time(time);
-        do {
-            day /= 10;
-            len++;
-        } while(day);
-        day = date_from_time(time);
 
-        if(!show_offset) len -= 9;
-        else if(offset == 0) len -= 5;
-        else if(offset < 0) {
+        if(offset < 0) {
             sign = '+';
             offset = -offset;
         }
 
-        date_jsstr = jsstr_alloc_buf(len, &date_str);
-        if(!date_jsstr)
-            return E_OUTOFMEMORY;
-
         if(!show_offset)
-            sprintfW(date_str, formatNoOffsetW, week, month, day,
+            sprintfW(buf, formatNoOffsetW, week, month, day,
                     (int)hour_from_time(time), (int)min_from_time(time),
                     (int)sec_from_time(time), year, formatAD?ADW:BCW);
         else if(offset)
-            sprintfW(date_str, formatW, week, month, day,
+            sprintfW(buf, formatW, week, month, day,
                     (int)hour_from_time(time), (int)min_from_time(time),
                     (int)sec_from_time(time), sign, offset/60, offset%60,
                     year, formatAD?ADW:BCW);
         else
-            sprintfW(date_str, formatUTCW, week, month, day,
+            sprintfW(buf, formatUTCW, week, month, day,
                     (int)hour_from_time(time), (int)min_from_time(time),
                     (int)sec_from_time(time), year, formatAD?ADW:BCW);
 
+        date_jsstr = jsstr_alloc(buf);
+        if(!date_jsstr)
+            return E_OUTOFMEMORY;
+
         *r = jsval_string(date_jsstr);
     }
     return S_OK;
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index e986294..40a749b 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -2155,8 +2155,12 @@ 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();
 ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
+tmp = (new Date(1600, 1, 1, 0, 0, 0, 0)).toLocaleString();
+ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
 tmp = (new Date()).toLocaleTimeString();
 ok(tmp.indexOf(String.fromCharCode(0)) == -1, "invalid null byte");
+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");
 
-- 
2.9.0



More information about the wine-patches mailing list