Commit b9d7ebd9 authored by Piotr Caban's avatar Piotr Caban Committed by Alexandre Julliard

jscript: Added String.substr implementation.

parent 0091b3fb
...@@ -1380,11 +1380,85 @@ static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPP ...@@ -1380,11 +1380,85 @@ static HRESULT String_substring(DispatchEx *dispex, LCID lcid, WORD flags, DISPP
return S_OK; return S_OK;
} }
/* ECMA-262 3rd Edition B.2.3 */
static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, static HRESULT String_substr(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
{ {
FIXME("\n"); BSTR val_str = NULL;
return E_NOTIMPL; const WCHAR *str;
INT start=0, len;
DWORD length;
VARIANT v;
HRESULT hres;
TRACE("\n");
if(is_class(dispex, JSCLASS_STRING)) {
StringInstance *this = (StringInstance*)dispex;
str = this->str;
length = this->length;
}else {
VARIANT this;
V_VT(&this) = VT_DISPATCH;
V_DISPATCH(&this) = (IDispatch*)_IDispatchEx_(dispex);
hres = to_string(dispex->ctx, &this, ei, &val_str);
if(FAILED(hres))
return hres;
str = val_str;
length = SysStringLen(val_str);
}
if(arg_cnt(dp) >= 1) {
hres = to_integer(dispex->ctx, dp->rgvarg + dp->cArgs-1, ei, &v);
if(FAILED(hres)) {
SysFreeString(val_str);
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)) {
SysFreeString(val_str);
return hres;
}
if(V_VT(&v) == VT_I4) {
len = V_I4(&v);
if(len < 0)
len = 0;
else if(len > length-start)
len = length-start;
}else {
len = V_R8(&v) < 0.0 ? 0 : length-start;
}
}else {
len = length-start;
}
hres = S_OK;
if(retv) {
V_VT(retv) = VT_BSTR;
V_BSTR(retv) = SysAllocStringLen(str+start, len);
if(!V_BSTR(retv))
hres = E_OUTOFMEMORY;
}
SysFreeString(val_str);
return hres;
} }
static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, static HRESULT String_sup(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp,
......
...@@ -111,6 +111,10 @@ ok(str.toString() === "", "str.toString() = " + str.toString()); ...@@ -111,6 +111,10 @@ ok(str.toString() === "", "str.toString() = " + str.toString());
var str = new String("test", "abc"); var str = new String("test", "abc");
ok(str.toString() === "test", "str.toString() = " + str.toString()); ok(str.toString() === "test", "str.toString() = " + str.toString());
var strObj = new Object();
strObj.toString = function() { return "abcd" };
strObj.substr = String.prototype.substr;
tmp = "value " + str; tmp = "value " + str;
ok(tmp === "value test", "'value ' + str = " + tmp); ok(tmp === "value test", "'value ' + str = " + tmp);
...@@ -172,6 +176,25 @@ ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp); ...@@ -172,6 +176,25 @@ ok(tmp === "bc", "'abcd'.substring(1,3,2) = " + tmp);
tmp = "abcd".substring(); tmp = "abcd".substring();
ok(tmp === "abcd", "'abcd'.substring() = " + tmp); ok(tmp === "abcd", "'abcd'.substring() = " + tmp);
tmp = "abcd".substr(1,3);
ok(tmp === "bcd", "'abcd'.substr(1,3) = " + tmp);
tmp = "abcd".substr(-1,3);
ok(tmp === "abc", "'abcd'.substr(-1,3) = " + tmp);
tmp = "abcd".substr(1,6);
ok(tmp === "bcd", "'abcd'.substr(1,6) = " + tmp);
tmp = "abcd".substr(2,-1);
ok(tmp === "", "'abcd'.substr(3,1) = " + tmp);
tmp = "abcd".substr(2,0);
ok(tmp === "", "'abcd'.substr(2,2) = " + tmp);
tmp = "abcd".substr(true,"3");
ok(tmp === "bcd", "'abcd'.substr(true,'3') = " + tmp);
tmp = "abcd".substr(1,3,2);
ok(tmp === "bcd", "'abcd'.substr(1,3,2) = " + tmp);
tmp = "abcd".substr();
ok(tmp === "abcd", "'abcd'.substr() = " + tmp);
tmp = strObj.substr(1,1);
ok(tmp === "b", "'abcd'.substr(1,3) = " + tmp);
tmp = "abcd".slice(1,3); tmp = "abcd".slice(1,3);
ok(tmp === "bc", "'abcd'.slice(1,3) = " + tmp); ok(tmp === "bc", "'abcd'.slice(1,3) = " + tmp);
tmp = "abcd".slice(1,-1); tmp = "abcd".slice(1,-1);
......
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