diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-06-01 12:31:10 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-06-01 12:53:26 +0200 |
commit | 9efbe183d7805eb60652a3745ec48cd32682ef8d (patch) | |
tree | 469c4727d14938b1a28bb53b6192891682442d6b /lib.c | |
parent | 9b35df7b37f21043f4be0bdba011000ad4f7cf0f (diff) |
lib: refactor `uc_int()`
For string cases, turn `int()` into a thin `strtoll()` wrapper which
attempts to parse the initial portion of the string as a decimal integer
literal, optionally preceded by white space and a sign character.
Also introduce an optional `base` argument for string cases while we're
at it and adjust the existing stdlib test case accordingly.
The function now behaves mostly the same as ECMAScript `parseInt(val, 10)`
for string cases, means it will recognize `012` as `12` and not `10` and
it will accept trailing non-digit characters after the initial portition
of the input string.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -570,7 +570,22 @@ uc_hex(uc_vm_t *vm, size_t nargs) static uc_value_t * uc_int(uc_vm_t *vm, size_t nargs) { - int64_t n = ucv_to_integer(uc_fn_arg(0)); + uc_value_t *val = uc_fn_arg(0); + uc_value_t *base = uc_fn_arg(1); + char *e, *v; + int64_t n; + + if (ucv_type(val) == UC_STRING) { + errno = 0; + v = ucv_string_get(val); + n = strtoll(v, &e, base ? ucv_int64_get(base) : 10); + + if (e == v) + return ucv_double_new(NAN); + } + else { + n = ucv_to_integer(val); + } if (errno == EINVAL || errno == ERANGE) return ucv_double_new(NAN); |