commit 4f73b863072e31710541293bcbeac73103d1799f
parent a67d398a910a510700d21569dce2c7a81ae85243
Author: Sebastian <sebastian@sebsite.pw>
Date: Tue, 18 Jul 2023 18:56:47 -0400
strconv: recognize - and + in stou*
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/strconv/+test/stou_test.ha b/strconv/+test/stou_test.ha
@@ -7,10 +7,10 @@
assert(stou64("") as invalid == 0: invalid);
assert(stou64("abc") as invalid == 0: invalid);
assert(stou64("1a") as invalid == 1: invalid);
- assert(stou64("-1") as invalid == 0: invalid);
assert(stou64("18446744073709551616") is overflow);
assert(stou64("184467440737095516150") is overflow);
+ assert(stou64("-1") is overflow);
assert(stou64("0") as u64 == 0);
assert(stou64("1") as u64 == 1);
diff --git a/strconv/stou.ha b/strconv/stou.ha
@@ -28,6 +28,11 @@ export fn stou64b(s: str, base: base) (u64 | invalid | overflow) = {
if (len(s) == 0) {
return 0: invalid;
};
+ if (strings::hasprefix(s, "-")) {
+ return overflow;
+ } else if (strings::hasprefix(s, "+")) {
+ s = strings::trimprefix(s, "+");
+ };
let n = 0u64;
let iter = strings::iter(s);