Commit b67e875e authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

jscript: Added String.split implementation for non-regexp arguments.

parent 67af45be
......@@ -846,12 +846,13 @@ static HRESULT String_small(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAM
static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{
match_result_t *match_result = NULL;
DWORD match_cnt, i, match_len = 0;
StringInstance *string;
match_result_t *match_result;
DWORD match_cnt, i, len;
const WCHAR *ptr;
const WCHAR *ptr, *ptr2;
VARIANT *arg, var;
DispatchEx *array;
BSTR match_str = NULL;
HRESULT hres;
TRACE("\n");
......@@ -886,18 +887,38 @@ static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAM
}
}
default:
FIXME("unsupported vt %d\n", V_VT(arg));
return E_NOTIMPL;
hres = to_string(dispex->ctx, arg, ei, &match_str);
if(FAILED(hres))
return hres;
match_len = SysStringLen(match_str);
if(!match_len) {
SysFreeString(match_str);
match_str = NULL;
}
}
hres = create_array(dispex->ctx, match_cnt+1, &array);
hres = create_array(dispex->ctx, 0, &array);
if(SUCCEEDED(hres)) {
ptr = string->str;
for(i=0; i < match_cnt; i++) {
len = match_result[i].str-ptr;
for(i=0;; i++) {
if(match_result) {
if(i == match_cnt)
break;
ptr2 = match_result[i].str;
}else if(match_str) {
ptr2 = strstrW(ptr, match_str);
if(!ptr2)
break;
}else {
if(!*ptr)
break;
ptr2 = ptr+1;
}
V_VT(&var) = VT_BSTR;
V_BSTR(&var) = SysAllocStringLen(ptr, len);
V_BSTR(&var) = SysAllocStringLen(ptr, ptr2-ptr);
if(!V_BSTR(&var)) {
hres = E_OUTOFMEMORY;
break;
......@@ -908,20 +929,32 @@ static HRESULT String_split(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAM
if(FAILED(hres))
break;
ptr = match_result[i].str + match_result[i].len;
if(match_result)
ptr = match_result[i].str + match_result[i].len;
else if(match_str)
ptr = ptr2 + match_len;
else
ptr++;
}
}
if(SUCCEEDED(hres)) {
len = (string->str+string->length) - ptr;
if(SUCCEEDED(hres) && (match_str || match_result)) {
DWORD len = (string->str+string->length) - ptr;
V_VT(&var) = VT_BSTR;
V_BSTR(&var) = SysAllocStringLen(ptr, len);
if(len || match_str) {
V_VT(&var) = VT_BSTR;
V_BSTR(&var) = SysAllocStringLen(ptr, len);
hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
SysFreeString(V_BSTR(&var));
if(V_BSTR(&var)) {
hres = jsdisp_propput_idx(array, i, lcid, &var, ei, sp);
SysFreeString(V_BSTR(&var));
}else {
hres = E_OUTOFMEMORY;
}
}
}
SysFreeString(match_str);
heap_free(match_result);
if(SUCCEEDED(hres) && retv) {
......
......@@ -200,6 +200,36 @@ ok(r === "-ret-", "r = " + r + " expected '-ret-'");
r = "-[test]-".replace("[test]", replaceFunc3, "test");
ok(r === "-ret-", "r = " + r + " expected '-ret-'");
r = "1,2,3".split(",");
ok(typeof(r) === "object", "typeof(r) = " + typeof(r));
ok(r.length === 3, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
ok(r[2] === "3", "r[2] = " + r[2]);
r = "1,2,3".split(",*");
ok(r.length === 1, "r.length = " + r.length);
ok(r[0] === "1,2,3", "r[0] = " + r[0]);
r = "123".split("");
ok(r.length === 3, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
ok(r[2] === "3", "r[2] = " + r[2]);
r = "123".split(2);
ok(r.length === 2, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "3", "r[1] = " + r[1]);
r = "1,2,".split(",");
ok(typeof(r) === "object", "typeof(r) = " + typeof(r));
ok(r.length === 3, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
ok(r[2] === "", "r[2] = " + r[2]);
tmp = "abcd".indexOf("bc",0);
ok(tmp === 1, "indexOf = " + tmp);
tmp = "abcd".indexOf("bc",1);
......
......@@ -150,4 +150,21 @@ function replaceFunc2(m, subm, off, str) {
r = "[test1] [test2]".replace(/\[([^\[]+)\]/g, replaceFunc2);
ok(r === "r0 r1", "r = '" + r + "' expected 'r0 r1'");
r = "1,,2,3".split(/,+/g);
ok(r.length === 3, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
ok(r[2] === "3", "r[2] = " + r[2]);
r = "1,,2,3".split(/,+/);
ok(r.length === 3, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
ok(r[2] === "3", "r[2] = " + r[2]);
r = "1,,2,".split(/,+/);
ok(r.length === 2, "r.length = " + r.length);
ok(r[0] === "1", "r[0] = " + r[0]);
ok(r[1] === "2", "r[1] = " + r[1]);
reportSuccess();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment