summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib.c17
-rw-r--r--tests/custom/03_stdlib/08_int8
-rw-r--r--tests/custom/04_bugs/29_empty_string_as_number8
3 files changed, 23 insertions, 10 deletions
diff --git a/lib.c b/lib.c
index 6793972..4d37531 100644
--- a/lib.c
+++ b/lib.c
@@ -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);
diff --git a/tests/custom/03_stdlib/08_int b/tests/custom/03_stdlib/08_int
index a6b5923..12db299 100644
--- a/tests/custom/03_stdlib/08_int
+++ b/tests/custom/03_stdlib/08_int
@@ -30,13 +30,13 @@ Returns `NaN` if the conversion result is out of range.
0,
123,
456,
- 0,
- "NaN",
"NaN",
- 4096,
"NaN",
- 127,
"NaN",
+ 0,
+ 0,
+ 177,
+ 145,
-96
]
-- End --
diff --git a/tests/custom/04_bugs/29_empty_string_as_number b/tests/custom/04_bugs/29_empty_string_as_number
index 675f8a1..51a93b2 100644
--- a/tests/custom/04_bugs/29_empty_string_as_number
+++ b/tests/custom/04_bugs/29_empty_string_as_number
@@ -1,10 +1,9 @@
-When an empty string was casted to a number, e.g. explicitly through `+`
-or `int()` or implicitly through numerical calculations, it was incorrectly
-treated as `NaN` and not `0`.
+When an empty string was explicitly casted to a number through `+` or
+implicitly through numerical calculations, it was incorrectly treated
+as `NaN` and not `0`.
-- Testcase --
{{ +"" }}
-{{ int("") }}
{{ "" + 0 }}
{{ "" - 0.0 }}
-- End --
@@ -13,5 +12,4 @@ treated as `NaN` and not `0`.
0
0
0
-0
-- End --