commit dddfd14dd74dddc424978ca94e532cd5a45db61a
parent 5a839b17026d563370013410d8e99932d1e2b528
Author: Joe Finney <me@spxtr.net>
Date: Sat, 8 Jun 2024 21:52:56 +0100
Handle base::HEX_LOWER in stof and stou.
Signed-off-by: Joe Finney <me@spxtr.net>
Diffstat:
2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/strconv/stof.ha b/strconv/stof.ha
@@ -628,7 +628,11 @@ fn special(s: str) (f32 | void) = {
// nearest to zero with respective sign. Recognizes "Infinity", "+Infinity",
// "-Infinity", and "NaN", case insensitive.
export fn stof64(s: str, b: base = base::DEC) (f64 | invalid | overflow) = {
- if (b == base::DEFAULT) b = base::DEC;
+ if (b == base::DEFAULT) {
+ b = base::DEC;
+ } else if (b == base::HEX_LOWER) {
+ b = base::HEX;
+ };
assert(b == base::DEC || b == base::HEX);
if (len(s) == 0) {
@@ -670,7 +674,11 @@ export fn stof64(s: str, b: base = base::DEC) (f64 | invalid | overflow) = {
// nearest to zero with respective sign. Recognizes "Infinity", "+Infinity",
// "-Infinity", and "NaN", case insensitive.
export fn stof32(s: str, b: base = base::DEC) (f32 | invalid | overflow) = {
- if (b == base::DEFAULT) b = base::DEC;
+ if (b == base::DEFAULT) {
+ b = base::DEC;
+ } else if (b == base::HEX_LOWER) {
+ b = base::HEX;
+ };
assert(b == base::DEC || b == base::HEX);
if (len(s) == 0) {
@@ -767,7 +775,7 @@ export fn stof32(s: str, b: base = base::DEC) (f32 | invalid | overflow) = {
@test fn stofhex() void = {
assert(stof64("0p0", base::HEX)! == 0x0.0p0);
assert(stof64("1p0", base::HEX)! == 0x1.0p0);
- assert(stof64("-1p0", base::HEX)! == -0x1.0p0);
+ assert(stof64("-1p0", base::HEX_LOWER)! == -0x1.0p0);
assert(stof64("1.fp-2", base::HEX)! == 0x1.fp-2);
assert(stof64("1.fffffffffffffp+1023", base::HEX)!
== math::F64_MAX_NORMAL);
diff --git a/strconv/stou.ha b/strconv/stou.ha
@@ -18,6 +18,8 @@ fn rune_to_integer(r: rune) (u64 | void) = {
fn parseint(s: str, base: base) ((bool, u64) | invalid | overflow) = {
if (base == base::DEFAULT) {
base = base::DEC;
+ } else if (base == base::HEX_LOWER) {
+ base = base::HEX;
};
assert(base == 2 || base == 8 || base == 10 || base == 16);
@@ -129,6 +131,7 @@ export fn stoz(s: str, base: base = base::DEC) (size | invalid | overflow) =
};
@test fn stou_bases() void = {
+ assert(stou64("f", base::HEX_LOWER) as u64 == 0xf);
assert(stou64("7f", 16) as u64 == 0x7f);
assert(stou64("7F", 16) as u64 == 0x7f);
assert(stou64("37", 8) as u64 == 0o37);