vbscript: Added Mid function implementation.

This commit is contained in:
Jacek Caban 2012-03-29 12:06:11 +02:00 committed by Alexandre Julliard
parent eccd9770cc
commit 4b00511c64
2 changed files with 91 additions and 3 deletions

View File

@ -105,6 +105,23 @@ static inline HRESULT return_null(VARIANT *res)
return S_OK;
}
static HRESULT to_int(VARIANT *v, int *ret)
{
switch(V_VT(v)) {
case VT_I2:
*ret = V_I2(v);
break;
case VT_I4:
*ret = V_I4(v);
break;
default:
FIXME("not supported %s\n", debugstr_variant(v));
return E_NOTIMPL;
}
return S_OK;
}
static IUnknown *create_object(script_ctx_t *ctx, const WCHAR *progid)
{
IInternetHostSecurityManager *secmgr = NULL;
@ -471,10 +488,60 @@ static HRESULT Global_RightB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VA
return E_NOTIMPL;
}
static HRESULT Global_Mid(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)
static HRESULT Global_Mid(vbdisp_t *This, VARIANT *args, unsigned args_cnt, VARIANT *res)
{
FIXME("\n");
return E_NOTIMPL;
int len = -1, start, str_len;
BSTR str;
HRESULT hres;
TRACE("\n");
switch(args_cnt) {
case 3:
hres = to_int(args, &len);
if(FAILED(hres))
return hres;
if(len < 0) {
FIXME("len = %d\n", len);
return E_FAIL;
}
/* fallthrough */
case 2:
hres = to_int(args+args_cnt-2, &start);
if(FAILED(hres))
return hres;
if(V_VT(args+args_cnt-1) != VT_BSTR) {
FIXME("args[0] = %s\n", debugstr_variant(args+args_cnt-1));
return E_NOTIMPL;
}
str = V_BSTR(args+args_cnt-1);
break;
default:
assert(0);
}
str_len = SysStringLen(str);
start--;
if(start > str_len)
start = str_len;
if(len == -1)
len = str_len-start;
else if(len > str_len-start)
len = str_len-start;
if(res) {
V_VT(res) = VT_BSTR;
V_BSTR(res) = SysAllocStringLen(str+start, len);
if(!V_BSTR(res))
return E_OUTOFMEMORY;
}
return S_OK;
}
static HRESULT Global_MidB(vbdisp_t *This, VARIANT *arg, unsigned args_cnt, VARIANT *res)

View File

@ -124,4 +124,25 @@ Call ok(isNull(x), "InStr returned " & x)
x = InStr(2, null, "abcd")
Call ok(isNull(x), "InStr returned " & x)
Sub TestMid(str, start, len, ex)
x = Mid(str, start, len)
Call ok(x = ex, "Mid(" & str & ", " & start & ", " & len & ") = " & x & " expected " & ex)
End Sub
Sub TestMid2(str, start, ex)
x = Mid(str, start)
Call ok(x = ex, "Mid(" & str & ", " & start & ") = " & x & " expected " & ex)
End Sub
TestMid "test", 2, 2, "es"
TestMid "test", 2, 4, "est"
TestMid "test", 1, 2, "te"
TestMid "test", 1, 0, ""
TestMid "test", 1, 0, ""
TestMid "test", 5, 2, ""
TestMid2 "test", 1, "test"
TestMid2 "test", 2, "est"
TestMid2 "test", 4, "t"
TestMid2 "test", 5, ""
Call reportSuccess()