aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-23 11:38:54 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-23 11:38:54 -0400
commitdceb9255736c69538293ee551272cda1b03a9fd3 (patch)
treefdb4e6947b80db01fa85b6de0c15670dddf83c52 /builtins
parentad51b208b4924d04f1d6a804178a67994b4f9e59 (diff)
Bugfix for parsing ints
Diffstat (limited to 'builtins')
-rw-r--r--builtins/integers.c12
-rw-r--r--builtins/integers.h2
2 files changed, 8 insertions, 6 deletions
diff --git a/builtins/integers.c b/builtins/integers.c
index 87d1f0ad..4a7b5c3a 100644
--- a/builtins/integers.c
+++ b/builtins/integers.c
@@ -315,18 +315,20 @@ public Range_t Int$to(Int_t from, Int_t to) {
return (Range_t){from, to, Int$compare(&to, &from, &$Int) >= 0 ? (Int_t){.small=(1<<2)|1} : (Int_t){.small=(-1>>2)|1}};
}
-public Int_t Int$from_text(CORD text) {
+public Int_t Int$from_text(CORD text, bool *success) {
const char *str = CORD_to_const_char_star(text);
mpz_t i;
+ int result;
if (strncmp(str, "0x", 2) == 0) {
- mpz_init_set_str(i, str + 2, 16);
+ result = mpz_init_set_str(i, str + 2, 16);
} else if (strncmp(str, "0o", 2) == 0) {
- mpz_init_set_str(i, str + 2, 8);
+ result = mpz_init_set_str(i, str + 2, 8);
} else if (strncmp(str, "0b", 2) == 0) {
- mpz_init_set_str(i, str + 2, 2);
+ result = mpz_init_set_str(i, str + 2, 2);
} else {
- mpz_init_set_str(i, str, 10);
+ result = mpz_init_set_str(i, str, 10);
}
+ if (success) *success = (result == 0);
return Int$from_mpz(i);
}
diff --git a/builtins/integers.h b/builtins/integers.h
index b4cb2113..e5a662cc 100644
--- a/builtins/integers.h
+++ b/builtins/integers.h
@@ -82,7 +82,7 @@ CORD Int$octal(Int_t i, Int_t digits, bool prefix);
void Int$init_random(long seed);
Int_t Int$random(Int_t min, Int_t max);
Range_t Int$to(Int_t from, Int_t to);
-Int_t Int$from_text(CORD text);
+Int_t Int$from_text(CORD text, bool *success);
Int_t Int$abs(Int_t x);
Int_t Int$power(Int_t base, Int_t exponent);
Int_t Int$sqrt(Int_t i);