commit 580380fa449328ed07e933e7f9140f0cf988c278
parent efeba759edc509df57f4136a7846433ce8685e0d
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 12 Feb 2021 10:45:44 -0500
fmt: print pointers in hex
Diffstat:
2 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/fmt/fmt.ha b/fmt/fmt.ha
@@ -71,14 +71,6 @@ export @noreturn fn fatal(fmt: str, args: formattable...) void = {
os::exit(1);
};
-type base = enum {
- DECIMAL,
- LOWER_HEX,
- UPPER_HEX,
- OCTAL,
- BINARY,
-};
-
type negation = enum {
NONE,
SPACE,
@@ -96,7 +88,7 @@ type modifiers = struct {
negation: negation,
width: uint,
precision: uint,
- base: base,
+ base: strconv::base,
};
// Formats text for printing and writes it to an [io::stream].
@@ -135,7 +127,7 @@ export fn fprintf(
args[i - 1];
};
- let mod = modifiers { ... };
+ let mod = modifiers { base = strconv::base::DEC, ... };
r = match (strings::next(&iter)) {
void => abort("Invalid format string (unterminated '{')"),
r: rune => r,
@@ -196,17 +188,19 @@ fn format(
s: str => io::write(out, strings::to_utf8(s)),
r: rune => io::write(out, utf8::encode_rune(r)),
n: types::numeric => {
- let s = strconv::numerictos(n);
+ let s = strconv::numerictosb(n, mod.base);
io::write(out, strings::to_utf8(s));
},
p: uintptr => {
- let s = strconv::uptrtos(p);
+ let s = strconv::uptrtosb(p, mod.base);
io::write(out, strings::to_utf8(s));
},
v: nullable *void => match (v) {
v: *void => {
- let mod = modifiers { base = base::LOWER_HEX, ... };
- format(out, v: uintptr, &mod);
+ let s = strconv::uptrtosb(v: uintptr,
+ strconv::base::HEX_LOWER);
+ io::write(out, strings::to_utf8("0x"));
+ io::write(out, strings::to_utf8(s));
},
null => format(out, "(null)", mod),
},
diff --git a/strconv/utos.ha b/strconv/utos.ha
@@ -62,7 +62,12 @@ export fn utosb(u: uint, b: base) const str = u64tosb(u: uint, b);
// Converts a size to a string, in the given base. The return value is
// statically allocated and will be overwritten on subsequent calls; see
// [strings::dup] to duplicate the result.
-export fn ztosb(u: size, b: base) const str = u64tosb(u: uint, b);
+export fn ztosb(u: size, b: base) const str = u64tosb(u: u64, b);
+
+// Converts a size to a string, in the given base. The return value is
+// statically allocated and will be overwritten on subsequent calls; see
+// [strings::dup] to duplicate the result.
+export fn uptrtosb(uptr: uintptr, b: base) const str = u64tosb(uptr: u64, b);
// 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