Jacek Caban : jscript: Added String.indexOf implementation.

Alexandre Julliard julliard at winehq.org
Tue Oct 7 08:54:11 CDT 2008


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

Author: Jacek Caban <jacek at codeweavers.com>
Date:   Mon Oct  6 09:57:03 2008 -0500

jscript: Added String.indexOf implementation.

---

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

diff --git a/dlls/jscript/string.c b/dlls/jscript/string.c
index 11ef882..ba4c82d 100644
--- a/dlls/jscript/string.c
+++ b/dlls/jscript/string.c
@@ -323,8 +323,69 @@ static HRESULT String_fontsize(DispatchEx *dispex, LCID lcid, WORD flags, DISPPA
 static HRESULT String_indexOf(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
         VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
 {
-    FIXME("\n");
-    return E_NOTIMPL;
+    DWORD length, pos = 0;
+    const WCHAR *str;
+    BSTR search_str;
+    INT ret = -1;
+    HRESULT hres;
+
+    TRACE("\n");
+
+    if(is_class(dispex, JSCLASS_STRING)) {
+        StringInstance *string = (StringInstance*)dispex;
+
+        str = string->str;
+        length = string->length;
+    }else {
+        FIXME("not String this\n");
+        return E_NOTIMPL;
+    }
+
+    if(!arg_cnt(dp)) {
+        if(retv) {
+            V_VT(retv) = VT_I4;
+            V_I4(retv) = -1;
+        }
+        return S_OK;
+    }
+
+    hres = to_string(dispex->ctx, get_arg(dp,0), ei, &search_str);
+    if(FAILED(hres))
+        return hres;
+
+    if(arg_cnt(dp) >= 2) {
+        VARIANT ival;
+
+        hres = to_integer(dispex->ctx, get_arg(dp,1), ei, &ival);
+        if(SUCCEEDED(hres)) {
+            if(V_VT(&ival) == VT_I4)
+                pos = V_VT(&ival) > 0 ? V_I4(&ival) : 0;
+            else
+                pos = V_R8(&ival) > 0.0 ? length : 0;
+            if(pos > length)
+                pos = length;
+        }
+    }
+
+    if(SUCCEEDED(hres)) {
+        const WCHAR *ptr;
+
+        ptr = strstrW(str+pos, search_str);
+        if(ptr)
+            ret = ptr - str;
+        else
+            ret = -1;
+    }
+
+    SysFreeString(search_str);
+    if(FAILED(hres))
+        return hres;
+
+    if(retv) {
+        V_VT(retv) = VT_I4;
+        V_I4(retv) = ret;
+    }
+    return S_OK;
 }
 
 static HRESULT String_italics(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js
index aef7c2b..153c1f5 100644
--- a/dlls/jscript/tests/api.js
+++ b/dlls/jscript/tests/api.js
@@ -179,6 +179,23 @@ ok(r === "-ret-", "r = " + r + " expected '-ret-'");
 r = "-[test]-".replace("[test]", replaceFunc3, "test");
 ok(r === "-ret-", "r = " + r + " expected '-ret-'");
 
+tmp = "abcd".indexOf("bc",0);
+ok(tmp === 1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("bc",1);
+ok(tmp === 1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("bc");
+ok(tmp === 1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("ac");
+ok(tmp === -1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("bc",2);
+ok(tmp === -1, "indexOf = " + tmp);
+tmp = "abcd".indexOf("a",0);
+ok(tmp === 0, "indexOf = " + tmp);
+tmp = "abcd".indexOf("bc",0,"test");
+ok(tmp === 1, "indexOf = " + tmp);
+tmp = "abcd".indexOf();
+ok(tmp == -1, "indexOf = " + 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