commit 4014971aa011a793bc60f2c3f74b388c65c057ad
parent d1b3e8fbe541da75c653cf85ae6053b68f7cb26a
Author: Alexey Yerin <yyp@disroot.org>
Date: Mon, 18 Sep 2023 22:05:55 +0300
strconv: Add base::DEFAULT instead of base::DEC being equal 0
Signed-off-by: Alexey Yerin <yyp@disroot.org>
Diffstat:
6 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/strconv/itos.ha b/strconv/itos.ha
@@ -11,8 +11,8 @@ use strings;
// duplicate the result.
export fn i64tosb(i: i64, b: base) const str = {
static assert(types::I64_MAX == 9223372036854775807);
- if (b == base::DEC) {
- b = 10;
+ if (b == base::DEFAULT) {
+ b = base::DEC;
};
if (i >= 0) return u64tosb(i: u64, b);
diff --git a/strconv/numeric.ha b/strconv/numeric.ha
@@ -72,6 +72,9 @@ export fn integertos(n: types::integer) const str = integertosb(n, base::DEC);
// is statically allocated and will be overwritten on subsequent calls; see
// [[strings::dup]] to duplicate the result.
export fn floatingtosb(n: types::floating, b: base) const str = {
+ if (b == base::DEFAULT) {
+ b = base::DEC;
+ };
assert(b == base::DEC);
match (n) {
case let f: f32 =>
diff --git a/strconv/stoi.ha b/strconv/stoi.ha
@@ -23,8 +23,8 @@ export fn stoi64b(s: str, base: base) (i64 | invalid | overflow) = {
} else if (b[0] == '+') {
start = 1;
};
- if (base == base::DEC) {
- base = 10;
+ if (base == base::DEFAULT) {
+ base = base::DEC;
};
let u = stou64b(strings::fromutf8_unsafe(b[start..]), base);
let n = u?;
diff --git a/strconv/stou.ha b/strconv/stou.ha
@@ -20,8 +20,8 @@ fn rune_to_integer(r: rune) (u64 | void) = {
// non-numeric characters, or if it's empty, [[invalid]] is returned. If the
// number is too large to be represented by a u64, [[overflow]] is returned.
export fn stou64b(s: str, base: base) (u64 | invalid | overflow) = {
- if (base == base::DEC) {
- base = 10;
+ if (base == base::DEFAULT) {
+ base = base::DEC;
};
assert(base == 2 || base == 8 || base == 10 || base == 16);
diff --git a/strconv/types.ha b/strconv/types.ha
@@ -16,12 +16,15 @@ export type error = !(invalid | overflow);
// The valid numeric bases for numeric conversions.
export type base = enum uint {
+ // Default base - decimal
+ DEFAULT = 0,
+
// Base 2, binary
BIN = 2,
// Base 8, octal
OCT = 8,
- // Base 10, decimal (default)
- DEC = 0,
+ // Base 10, decimal
+ DEC = 10,
// Base 16, UPPERCASE hexadecimal
HEX_UPPER = 16,
// Alias for HEX_UPPER
diff --git a/strconv/utos.ha b/strconv/utos.ha
@@ -24,7 +24,7 @@ export fn u64tosb(u: u64, b: base) const str = {
yield &lut_lower;
};
- const b: uint = if (b == base::DEC) 10 else b;
+ const b: uint = if (b == base::DEFAULT) base::DEC else b;
let s = types::string { data = &buf, ... };
if (u == 0) {