Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=47570 Signed-off-by: Robert Wilhelm robert.wilhelm@gmx.net --- v3: Try to address comments from Jacek's review: Fix some corner cases, avoid memory leaks and add more tests. v4: If there are two delimeters or delimeter is at front or rear, an empty string is returned. --- dlls/vbscript/global.c | 126 +++++++++++++++++++++++++++++++++++- dlls/vbscript/tests/api.vbs | 43 ++++++++++++ 2 files changed, 166 insertions(+), 3 deletions(-)
diff --git a/dlls/vbscript/global.c b/dlls/vbscript/global.c index 122f5c7b71..b3f4999685 100644 --- a/dlls/vbscript/global.c +++ b/dlls/vbscript/global.c @@ -2291,10 +2291,130 @@ static HRESULT Global_Join(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, V return E_NOTIMPL; }
-static HRESULT Global_Split(BuiltinDisp *This, VARIANT *arg, unsigned args_cnt, VARIANT *res) +static HRESULT Global_Split(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res) { - FIXME("\n"); - return E_NOTIMPL; + BSTR str, string, delimeter = NULL; + int count, max, mode, len, start, end, ret, delimeterlen = 1; + SAFEARRAYBOUND bounds; + SAFEARRAY *sa = NULL; + VARIANT *data, var; + HRESULT hres = S_OK; + + TRACE("%s %u...\n", debugstr_variant(args), args_cnt); + + assert(1 <= args_cnt && args_cnt <= 4); + if(V_VT(args) != VT_BSTR) { + hres = to_string(args, &string); + if(FAILED(hres)) + return hres; + }else { + string = V_BSTR(args); + } + + if(args_cnt > 1) + { + if(V_VT(args+1) != VT_BSTR) { + hres = to_string(args+1, &delimeter); + if(FAILED(hres)) + goto error; + }else { + delimeter = V_BSTR(args+1); + } + delimeterlen = SysStringLen(delimeter); + } + + if(args_cnt > 2) { + hres = to_int(args+2, &max); + if(FAILED(hres)) + goto error;; + }else { + max = -1; + } + + if(args_cnt == 4) { + hres = to_int(args+3, &mode); + if(FAILED(hres)) + goto error; + if (mode != 0 && mode != 1) { + FIXME("unknown compare mode = %d\n", mode); + hres = E_FAIL; + goto error; + } + } + else { + mode = 0; + } + + start = 0; + bounds.lLbound = 0; + bounds.cElements = 0; + sa = SafeArrayCreate( VT_VARIANT, 1, &bounds); + if (!sa) + { + hres = E_OUTOFMEMORY; + goto error; + } + len = SysStringLen(string); + count = 0; + + while(1) { + ret = -1; + if (delimeterlen) { + ret = FindStringOrdinal(FIND_FROMSTART, string + start, len -start, + delimeter ? delimeter : L" ", delimeterlen, mode); + } + + if (ret == -1) { + end = len; + } + else { + end = start + ret; + } + + str = SysAllocStringLen(string + start, end - start); + if (!str) { + hres = E_OUTOFMEMORY; + break; + } + V_VT(&var) = VT_BSTR; + V_BSTR(&var) = str; + bounds.cElements++; + SafeArrayRedim(sa, &bounds); + hres = SafeArrayAccessData(sa, (void**)&data); + if(FAILED(hres)) { + SafeArrayDestroy(sa); + break; + } + + hres = VariantCopyInd(data+count, &var); + if(FAILED(hres)) { + SafeArrayUnaccessData(sa); + SafeArrayDestroy(sa); + break; + } + + SafeArrayUnaccessData(sa); + count++; + + if (ret == -1) break; + start = start + ret + delimeterlen; + if (count == max) break; + if (start > len) break; + } + +error: + if(res) { + V_VT(res) = VT_ARRAY|VT_VARIANT; + V_ARRAY(res) = sa; + }else { + if (sa) SafeArrayDestroy(sa); + } + + if(V_VT(args) != VT_BSTR) + SysFreeString(string); + if(V_VT(args+1) != VT_BSTR) + SysFreeString(delimeter); + return hres; }
static HRESULT Global_Replace(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res) diff --git a/dlls/vbscript/tests/api.vbs b/dlls/vbscript/tests/api.vbs index 6af49c849d..60ad6359dc 100644 --- a/dlls/vbscript/tests/api.vbs +++ b/dlls/vbscript/tests/api.vbs @@ -609,6 +609,49 @@ TestLCase 0.123, doubleAsString(0.123) TestLCase Empty, "" Call ok(getVT(LCase(Null)) = "VT_NULL", "getVT(LCase(Null)) = " & getVT(LCase(Null)))
+x=Split("abc") +Call ok(x(0) = "abc", "Split(""abc"")(0)=" & x(0)) +x = Split("abc def") +Call ok(x(0) = "abc", "Split(""abc def"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc def"")(1)=" & x(1)) +x = Split("abc def ghi") +Call ok(x(0) = "abc", "Split(""abc def ghi"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc def ghi"")(1)=" & x(1)) +Call ok(x(2) = "ghi", "Split(""abc def ghi"")(2)=" & x(2)) +x = Split("abc def","") +Call ok(x(0) = "abc def", "Split(""abc def"","""")(0)=" & x(0)) +x = Split("abc-def","-") +Call ok(x(0) = "abc", "Split(""abc-def"",""-"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc-def"",""-"")(1)=" & x(1)) +x = Split("abc--def","-") +Call ok(x(0) = "abc", "Split(""abc--def"",""-"")(0)=" & x(0)) +Call ok(x(1) = "", "Split(""abc--def"",""-"")(1)=" & x(1)) +Call ok(x(2) = "def", "Split(""abc--def"",""-"")(2)=" & x(2)) +x = Split("abcdefghi","def") +Call ok(x(0) = "abc", "Split(""abcdefghi"",""def"")(0)=" & x(0)) +Call ok(x(1) = "ghi", "Split(""abcdefghi"",""def"")(1)=" & x(1)) +x = Split("12345",3) +Call ok(x(0) = "12", "Split(""12345"",3)(0)=" & x(0)) +Call ok(x(1) = "45", "Split(""12345"",3)(1)=" & x(1)) +x = Split("12345",5) +Call ok(x(0) = "1234", "Split(""12345"",5)(0)=" & x(0)) +Call ok(x(1) = "", "Split(""12345"",5)(1)=" & x(1)) +x = Split("12345",12) +Call ok(x(0) = "", "Split(""12345"",12)(0)=" & x(0)) +Call ok(x(1) = "345", "Split(""12345"",12)(1)=" & x(1)) +x = Split("abc-def-ghi","-") +Call ok(UBound(x) = 2, "UBound(Split(""abc-def-ghi"",""-""))=" & UBound(x)) +x = Split("abc-def-ghi","-",2) +Call ok(UBound(x) = 1, "UBound(Split(""abc-def-ghi"",""-"",2))=" & UBound(x)) +x = Split("abc-def-ghi","-",4) +Call ok(UBound(x) = 2, "UBound(Split(""abc-def-ghi"",""-"",4))=" & UBound(x)) +x = Split("abcZdefZghi","Z",3,0) +Call ok(UBound(x) = 2, "UBound(Split(""abcZdefZghi"",""Z"",3,0))=" & UBound(x)) +x = Split("abcZdefZghi","z",3,0) +Call ok(UBound(x) = 0, "UBound(Split(""abcZdefZghi"",""z"",3,0))=" & UBound(x)) +x = Split("abcZdefZghi","z",3,1) +Call ok(UBound(x) = 2, "UBound(Split(""abcZdefZghi"",""z"",3,1))=" & UBound(x)) + Sub TestStrComp(str_left, str_right, mode, ex) x = StrComp(str_left, str_right, mode) Call ok(x = ex, "StrComp(" & str_left & ", " & str_right & ", " & mode & ") = " & x & " expected " & ex) -- 2.26.2
Hi Robert,
The patch looks better now, thanks.
On 19.08.2020 22:34, Robert Wilhelm wrote:
V_VT(&var) = VT_BSTR;
V_BSTR(&var) = str;
bounds.cElements++;
SafeArrayRedim(sa, &bounds);
hres = SafeArrayAccessData(sa, (void**)&data);
if(FAILED(hres)) {
SafeArrayDestroy(sa);
break;
}
Resizing safe array in each iteration is a bit suboptimal. Maybe we could have a temporary array of result strings (that we could grow exponentially) and construct the actual SAFEARRAY when we know the size.
hres = VariantCopyInd(data+count, &var);
if(FAILED(hres)) {
SafeArrayUnaccessData(sa);
SafeArrayDestroy(sa);
break;
}
SafeArrayUnaccessData(sa);
count++;
if (ret == -1) break;
start = start + ret + delimeterlen;
if (count == max) break;
if (start > len) break;
- }
+error:
- if(res) {
V_VT(res) = VT_ARRAY|VT_VARIANT;
V_ARRAY(res) = sa;
We should set res value only when we return success. Otherwise the caller may assume that data is invalid and leak it.
}else {
if (sa) SafeArrayDestroy(sa);
}
if(V_VT(args) != VT_BSTR)
SysFreeString(string);
if(V_VT(args+1) != VT_BSTR)
SysFreeString(delimeter);
return hres; }
static HRESULT Global_Replace(BuiltinDisp *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
diff --git a/dlls/vbscript/tests/api.vbs b/dlls/vbscript/tests/api.vbs index 6af49c849d..60ad6359dc 100644 --- a/dlls/vbscript/tests/api.vbs +++ b/dlls/vbscript/tests/api.vbs @@ -609,6 +609,49 @@ TestLCase 0.123, doubleAsString(0.123) TestLCase Empty, "" Call ok(getVT(LCase(Null)) = "VT_NULL", "getVT(LCase(Null)) = " & getVT(LCase(Null)))
+x=Split("abc") +Call ok(x(0) = "abc", "Split(""abc"")(0)=" & x(0)) +x = Split("abc def") +Call ok(x(0) = "abc", "Split(""abc def"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc def"")(1)=" & x(1)) +x = Split("abc def ghi") +Call ok(x(0) = "abc", "Split(""abc def ghi"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc def ghi"")(1)=" & x(1)) +Call ok(x(2) = "ghi", "Split(""abc def ghi"")(2)=" & x(2)) +x = Split("abc def","") +Call ok(x(0) = "abc def", "Split(""abc def"","""")(0)=" & x(0)) +x = Split("abc-def","-") +Call ok(x(0) = "abc", "Split(""abc-def"",""-"")(0)=" & x(0)) +Call ok(x(1) = "def", "Split(""abc-def"",""-"")(1)=" & x(1)) +x = Split("abc--def","-") +Call ok(x(0) = "abc", "Split(""abc--def"",""-"")(0)=" & x(0)) +Call ok(x(1) = "", "Split(""abc--def"",""-"")(1)=" & x(1)) +Call ok(x(2) = "def", "Split(""abc--def"",""-"")(2)=" & x(2)) +x = Split("abcdefghi","def") +Call ok(x(0) = "abc", "Split(""abcdefghi"",""def"")(0)=" & x(0)) +Call ok(x(1) = "ghi", "Split(""abcdefghi"",""def"")(1)=" & x(1)) +x = Split("12345",3) +Call ok(x(0) = "12", "Split(""12345"",3)(0)=" & x(0)) +Call ok(x(1) = "45", "Split(""12345"",3)(1)=" & x(1)) +x = Split("12345",5) +Call ok(x(0) = "1234", "Split(""12345"",5)(0)=" & x(0)) +Call ok(x(1) = "", "Split(""12345"",5)(1)=" & x(1)) +x = Split("12345",12) +Call ok(x(0) = "", "Split(""12345"",12)(0)=" & x(0)) +Call ok(x(1) = "345", "Split(""12345"",12)(1)=" & x(1)) +x = Split("abc-def-ghi","-") +Call ok(UBound(x) = 2, "UBound(Split(""abc-def-ghi"",""-""))=" & UBound(x)) +x = Split("abc-def-ghi","-",2) +Call ok(UBound(x) = 1, "UBound(Split(""abc-def-ghi"",""-"",2))=" & UBound(x)) +x = Split("abc-def-ghi","-",4) +Call ok(UBound(x) = 2, "UBound(Split(""abc-def-ghi"",""-"",4))=" & UBound(x)) +x = Split("abcZdefZghi","Z",3,0) +Call ok(UBound(x) = 2, "UBound(Split(""abcZdefZghi"",""Z"",3,0))=" & UBound(x)) +x = Split("abcZdefZghi","z",3,0) +Call ok(UBound(x) = 0, "UBound(Split(""abcZdefZghi"",""z"",3,0))=" & UBound(x)) +x = Split("abcZdefZghi","z",3,1) +Call ok(UBound(x) = 2, "UBound(Split(""abcZdefZghi"",""z"",3,1))=" & UBound(x))
A test for negative "max" argument would be nice: both an explicit -1 and some other value.
Thanks,
Jacek