From f8537b6eb976ea65136d8ba0b0190c422974c5db Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Sun, 21 Sep 2008 15:42:48 +0200 Subject: [PATCH] jscript: Added parseInt implementation. --- dlls/jscript/global.c | 75 ++++++++++++++++++++++++++++++++++++++- dlls/jscript/tests/api.js | 21 ++++++++++- 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/dlls/jscript/global.c b/dlls/jscript/global.c index 4404bffe8c3..adee3714522 100644 --- a/dlls/jscript/global.c +++ b/dlls/jscript/global.c @@ -228,10 +228,83 @@ static HRESULT JSGlobal_isFinite(DispatchEx *dispex, LCID lcid, WORD flags, DISP return E_NOTIMPL; } +static INT char_to_int(WCHAR c) +{ + if('0' <= c && c <= '9') + return c - '0'; + if('a' <= c && c <= 'z') + return c - 'a' + 10; + if('A' <= c && c <= 'Z') + return c - 'A' + 10; + return 100; +} + static HRESULT JSGlobal_parseInt(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp) { - return E_NOTIMPL; + DOUBLE ret = 0.0; + INT radix=10, i; + WCHAR *ptr; + BOOL neg = FALSE; + BSTR str; + HRESULT hres; + + if(!arg_cnt(dp)) { + FIXME("NAN\n"); + return E_NOTIMPL; + } + + if(arg_cnt(dp) >= 2) { + hres = to_int32(dispex->ctx, get_arg(dp, 1), ei, &radix); + if(FAILED(hres)) + return hres; + + if(!radix) { + radix = 10; + }else if(radix < 2 || radix > 36) { + WARN("radix %d out of range\n", radix); + return E_FAIL; + } + } + + hres = to_string(dispex->ctx, get_arg(dp, 0), ei, &str); + if(FAILED(hres)) + return hres; + + for(ptr = str; isspaceW(*ptr); ptr++); + + switch(*ptr) { + case '+': + ptr++; + break; + case '-': + neg = TRUE; + ptr++; + break; + case '0': + ptr++; + if(*ptr == 'x' || *ptr == 'X') { + radix = 16; + ptr++; + } + } + + while(*ptr) { + i = char_to_int(*ptr++); + if(i > radix) + break; + + ret = ret*radix + i; + } + + SysFreeString(str); + + if(neg) + ret = -ret; + + if(retv) + num_set_val(retv, ret); + return S_OK; } static HRESULT JSGlobal_parseFloat(DispatchEx *dispex, LCID lcid, WORD flags, DISPPARAMS *dp, diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index f8b660754ec..8237ddbabe8 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -16,7 +16,26 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -var tmp; +var tmp, i; + +i = parseInt("0"); +ok(i === 0, "parseInt('0') = " + i); +i = parseInt("123"); +ok(i === 123, "parseInt('123') = " + i); +i = parseInt("-123"); +ok(i === -123, "parseInt('-123') = " + i); +i = parseInt("0xff"); +ok(i === 0xff, "parseInt('0xff') = " + i); +i = parseInt("11", 8); +ok(i === 9, "parseInt('11', 8) = " + i); +i = parseInt("1j", 22); +ok(i === 41, "parseInt('1j', 32) = " + i); +i = parseInt("123", 0); +ok(i === 123, "parseInt('123', 0) = " + i); +i = parseInt("123", 10, "test"); +ok(i === 123, "parseInt('123', 10, 'test') = " + i); +i = parseInt("11", "8"); +ok(i === 9, "parseInt('11', '8') = " + i); tmp = "" + new Object(); ok(tmp === "[object Object]", "'' + new Object() = " + tmp);