~sircmpwn/hare-dev

hare: strconv: Add base::DEFAULT instead of base::DEC being equal 0 v2 APPLIED

Alexey Yerin: 1
 strconv: Add base::DEFAULT instead of base::DEC being equal 0

 6 files changed, 15 insertions(+), 9 deletions(-)
#1059426 alpine.yml success
#1059427 freebsd.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/hare-dev/patches/44847/mbox | git am -3
Learn more about email & git

[PATCH hare v2] strconv: Add base::DEFAULT instead of base::DEC being equal 0 Export this patch

Signed-off-by: Alexey Yerin <yyp@disroot.org>
---
v1 -> v2: fix tests

 strconv/itos.ha    | 4 ++--
 strconv/numeric.ha | 3 +++
 strconv/stoi.ha    | 4 ++--
 strconv/stou.ha    | 4 ++--
 strconv/types.ha   | 7 +++++--
 strconv/utos.ha    | 2 +-
 6 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/strconv/itos.ha b/strconv/itos.ha
index 8ce80640..0e5947dc 100644
--- 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
index e38e510e..b3526dfc 100644
--- 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
index 72732343..1a43fc22 100644
--- 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
index 34822b7e..6fa0d1c7 100644
--- 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
index a62e0ff0..ac57259a 100644
--- 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
index d55af8f9..429a25e1 100644
--- 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) {
-- 
2.42.0
thanks!

to git@git.sr.ht:~sircmpwn/hare
  d1b3e8fb..4014971a  master -> master
hare/patches: SUCCESS in 1m36s

[strconv: Add base::DEFAULT instead of base::DEC being equal 0][0] v2 from [Alexey Yerin][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/44847
[1]: mailto:yyp@disroot.org

✓ #1059426 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/1059426
✓ #1059427 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1059427
I think we should pass on this for now, and instead add @default and use
it here once that's implemented.