commit ed43f94582f39d7a317397c7421e24faa068c59a
parent 652f7aa04033bb6091378168fb45ce6970f72473
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 2 Feb 2021 20:17:44 -0500
strconv, fmt: expand numeric support
Diffstat:
4 files changed, 40 insertions(+), 112 deletions(-)
diff --git a/fmt/fmt.ha b/fmt/fmt.ha
@@ -55,12 +55,14 @@ export fn fprintf(
};
fn format(out: *io::stream, arg: formattable) void = match (arg) {
- // TODO: Use types::numeric (and strconv::numtos) when match is
- // more fleshed out
- i: int => {
- let s = strconv::itos(i);
+ s: str => io::write(out, strings::to_utf8(s)),
+ v: *void => format(out, v: uintptr), // TODO: Hexadecimal
+ p: uintptr => {
+ let s = strconv::uptrtos(p);
+ io::write(out, strings::to_utf8(s));
+ },
+ n: types::numeric => {
+ let s = strconv::numerictos(n);
io::write(out, strings::to_utf8(s));
},
- s: str => io::write(out, strings::to_utf8(s)),
- * => abort("TODO: implement more formattable values"),
};
diff --git a/strconv/itos.ha b/strconv/itos.ha
@@ -4,13 +4,6 @@ use types;
// Converts an i64 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::i64tos(1234);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::i64tos(1234);
-// let b = strconv::i64tos(4321);
-// io::printf("%s %s", a, b); // 4321 4321
export fn i64tos(i: i64) const str = {
static assert(types::I64_MAX == 9223372036854775807i64);
static let buf: [22]u8 = [0u8...]; // 20 chars plus NUL and -
@@ -48,47 +41,19 @@ export fn i64tos(i: i64) const str = {
// Converts an i8 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::i8tos(123);
-// io::printf("%s", a); // 123
-//
-// let a = strconv::i8tos(123);
-// let b = strconv::i8tos(321);
-// io::printf("%s %s", a, b); // 321 321
export fn i8tos(i: i8) const str = i64tos(i: i64);
// Converts an i16 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::i16tos(1234);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::i16tos(1234);
-// let b = strconv::i16tos(4321);
-// io::printf("%s %s", a, b); // 4321 4321
export fn i16tos(i: i16) const str = i64tos(i: i64);
// Converts an i32 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::i32tos(1234);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::i32tos(1234);
-// let b = strconv::i32tos(4321);
-// io::printf("%s %s", a, b); // 4321 4321
export fn i32tos(i: i32) const str = i64tos(i: i64);
// Converts an int to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::itos(1234);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::itos(1234);
-// let b = strconv::itos(4321);
-// io::printf("%s %s", a, b); // 4321 4321
export fn itos(i: int) const str = i64tos(i: i64);
diff --git a/strconv/numeric.ha b/strconv/numeric.ha
@@ -1,16 +1,8 @@
use types;
-// Converts any types::signed to a string in base 10. The return value is
+// Converts any [types::signed] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
-// [strings::dup] to duplicate the result, or [strconv::itosb] to pass your own
-// string buffer.
-//
-// let a = strconv::signedtos(123);
-// io::printf("%s", a); // 123
-//
-// let a = strconv::signedtos(123);
-// let b = strconv::signedtos(321);
-// io::printf("%s %s", a, b); // 321 321
+// [strings::dup] to duplicate the result.
export fn signedtos(n: types::signed) const str = {
return match (n) {
i: int => itos(i),
@@ -21,25 +13,43 @@ export fn signedtos(n: types::signed) const str = {
};
};
-// Converts any types::unsigned to a string in base 10. The return value is
+// Converts any [types::unsigned] to a string in base 10. The return value is
// statically allocated and will be overwritten on subsequent calls; see
-// [strings::dup] to duplicate the result, or [strconv::itosb] to pass your own
-// string buffer.
-//
-// let a = strconv::unsignedtos(123);
-// io::printf("%s", a); // 123
-//
-// let a = strconv::unsignedtos(123);
-// let b = strconv::unsignedtos(321);
-// io::printf("%s %s", a, b); // 321 321
-export fn unsignedtos(n: (...types::unsigned | uintptr)) const str = {
+// [strings::dup] to duplicate the result.
+export fn unsignedtos(n: types::unsigned) const str = {
return match (n) {
u: size => ztos(u),
u: uint => utos(u),
- u: uintptr => uptrtos(u),
u: u8 => u8tos(u),
u: u16 => u16tos(u),
u: u32 => u32tos(u),
u: u64 => u64tos(u),
};
};
+
+// Converts any [types::integer] to a string in base 10. The return value is
+// statically allocated and will be overwritten on subsequent calls; see
+// [strings::dup] to duplicate the result.
+export fn integertos(n: types::integer) const str = {
+ return match (n) {
+ s: types::signed => signedtos(s),
+ u: types::unsigned => unsignedtos(u),
+ };
+};
+
+// Converts any [types::floating] to a string in base 10. The return value is
+// statically allocated and will be overwritten on subsequent calls; see
+// [strings::dup] to duplicate the result.
+export fn floatingtos(n: types::floating) const str = {
+ abort(); // TODO
+};
+
+// Converts any [types::numeric] to a string in base 10. The return value is
+// statically allocated and will be overwritten on subsequent calls; see
+// [strings::dup] to duplicate the result.
+export fn numerictos(n: types::numeric) const str = {
+ return match (n) {
+ i: types::integer => integertos(i),
+ f: types::floating => floatingtos(f),
+ };
+};
diff --git a/strconv/utos.ha b/strconv/utos.ha
@@ -4,13 +4,6 @@ use types;
// Converts a u64 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::u64tos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::u64tos(1234u);
-// let b = strconv::u64tos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn u64tos(u: u64) const str = {
static assert(types::U64_MAX == 18446744073709551615u64);
static let buf: [21]u8 = [0u8...]; // 20 digits plus NUL
@@ -41,71 +34,29 @@ export fn u64tos(u: u64) const str = {
// Converts a u8 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::u64tos(123u);
-// io::printf("%s", a); // 123
-//
-// let a = strconv::u64tos(123u);
-// let b = strconv::u64tos(321u);
-// io::printf("%s %s", a, b); // 321 321
export fn u8tos(u: u8) const str = u64tos(u: u64);
// Converts a u16 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::u16tos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::u16tos(1234u);
-// let b = strconv::u16tos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn u16tos(u: u16) const str = u64tos(u: u64);
// Converts a u32 to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::u32tos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::u32tos(1234u);
-// let b = strconv::u32tos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn u32tos(u: u32) const str = u64tos(u: u64);
// Converts a uint to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::utos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::utos(1234u);
-// let b = strconv::utos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn utos(u: uint) const str = u64tos(u: u64);
// Converts a size to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result, or [strconv::itosb] to pass your own string buffer.
-//
-// let a = strconv::ztos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::ztos(1234u);
-// let b = strconv::ztos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn ztos(z: size) const str = u64tos(z: u64);
// Converts a uintptr to a string, in base 10. The return value is statically
// allocated and will be overwritten on subsequent calls; see [strings::dup] to
// duplicate the result.
-//
-// let a = strconv::uptrtos(1234u);
-// io::printf("%s", a); // 1234
-//
-// let a = strconv::uptrtos(1234u);
-// let b = strconv::uptrtos(4321u);
-// io::printf("%s %s", a, b); // 4321 4321
export fn uptrtos(uptr: uintptr) const str = u64tos(uptr: u64);