jscript: Added parseInt implementation.

This commit is contained in:
Jacek Caban 2008-09-21 15:42:48 +02:00 committed by Alexandre Julliard
parent 8612ae880f
commit f8537b6eb9
2 changed files with 94 additions and 2 deletions

View File

@ -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,

View File

@ -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);