Jacek Caban : jscript: Added String.substring implementation.

Alexandre Julliard julliard at winehq.org
Mon Sep 22 07:04:14 CDT 2008


Module: wine
Branch: master
Commit: 5fdf258b1a3e16a098d0d8e69d22185460d3c97f
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=5fdf258b1a3e16a098d0d8e69d22185460d3c97f

Author: Jacek Caban <jacek at codeweavers.com>
Date:   Sun Sep 21 15:41:02 2008 +0200

jscript: Added String.substring implementation.

---

 dlls/jscript/string.c     |   68 +++++++++++++++++++++++++++++++++++++++++++-
 dlls/jscript/tests/api.js |   17 +++++++++++
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c
index 12140d5..d8673b3 100644
--- a/dlls/jscript/string.c
+++ b/dlls/jscript/string.c
@@ -367,11 +367,75 @@ static HRESULT String_sub(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS
     return E_NOTIMPL;
 }
 
+/* ECMA-262 3rd Edition    15.5.4.15 */
 static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    const WCHAR *str;
+    INT start=0, end;
+    DWORD length;
+    VARIANT v;
+    HRESULT hres;
+
+    TRACE("\n");
+
+    if(is_class(dispex, JSCLASS_STRING)) {
+        StringInstance *string = (StringInstance*)dispex;
+
+        length = string->length;
+        str = string->str;
+    }else {
+        FIXME("not string this not supported\n");
+        return E_NOTIMPL;
+    }
+
+    if(arg_cnt(dp) >= 1) {
+        hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
+        if(FAILED(hres))
+            return hres;
+
+        if(V_VT(&v) == VT_I4) {
+            start = V_I4(&v);
+            if(start < 0)
+                start = 0;
+            else if(start >= length)
+                start = length;
+        }else {
+            start = V_R8(&v) < 0.0 ? 0 : length;
+        }
+    }
+
+    if(arg_cnt(dp) >= 2) {
+        hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-2, ei, &v);
+        if(FAILED(hres))
+            return hres;
+
+        if(V_VT(&v) == VT_I4) {
+            end = V_I4(&v);
+            if(end < 0)
+                end = 0;
+            else if(end > length)
+                end = length;
+        }else {
+            end = V_R8(&v) < 0.0 ? 0 : length;
+        }
+    }else {
+        end = length;
+    }
+
+    if(start > end) {
+        INT tmp = start;
+        start = end;
+        end = tmp;
+    }
+
+    if(retv) {
+        V_VT(retv) = VT_BSTR;
+        V_BSTR(retv) = SysAllocStringLen(str+start, end-start);
+        if(!V_BSTR(retv))
+            return E_OUTOFMEMORY;
+    }
+    return S_OK;
 }
 
 static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index 4872263..14d4dea 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -43,6 +43,23 @@ ok(tmp === "", "'abc',charAt(-1) = " + tmp);
 tmp = "abc".charAt(0,2);
 ok(tmp === "a", "'abc',charAt(0.2) = " + tmp);
 
+tmp = "abcd".substring(1,3);
+ok(tmp === "bc", "'abcd'.substring(1,3) = " + tmp);
+tmp = "abcd".substring(-1,3);
+ok(tmp === "abc", "'abcd'.substring(-1,3) = " + tmp);
+tmp = "abcd".substring(1,6);
+ok(tmp === "bcd", "'abcd'.substring(1,6) = " + tmp);
+tmp = "abcd".substring(3,1);
+ok(tmp === "bc", "'abcd'.substring(3,1) = " + tmp);
+tmp = "abcd".substring(2,2);
+ok(tmp === "", "'abcd'.substring(2,2) = " + tmp);
+tmp = "abcd".substring(true,"3");
+ok(tmp === "bc", "'abcd'.substring(true,'3') = " + tmp);
+tmp = "abcd".substring(1,3,2);
+ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
+tmp = "abcd".substring();
+ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
+
 var arr = new Array();
 ok(typeof(arr) === "object", "arr () is not object");
 ok((arr.length === 0), "arr.length is not 0");




More information about the wine-cvs mailing list