commit 62eee8d4f453be39abe95e6a59232d7930923012
parent 746ac6b31a60216eba145b01b0cf5906758cecfb
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 26 Feb 2021 12:21:28 -0500
fmt: implement some basic width settings
Diffstat:
M | fmt/fmt.ha | | | 38 | +++++++++++++++++++++++++++++++++----- |
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/fmt/fmt.ha b/fmt/fmt.ha
@@ -171,7 +171,7 @@ export fn fprintf(
* => abort("Invalid format string"),
};
- format(s, arg, &mod);
+ n += format(s, arg, &mod)?;
} else if (r == '}') {
match (strings::next(&iter)) {
void => abort("Invalid format string (hanging '}')"),
@@ -187,11 +187,37 @@ export fn fprintf(
return n;
};
-fn format(
+fn format(out: *io::stream, arg: formattable, mod: *modifiers) (size | io::error) = {
+ let z = format_raw(io::empty, arg, mod)?;
+
+ let pad: []u8 = [];
+ if (z < mod.width: size) {
+ pad = utf8::encode_rune(switch (mod.padding) {
+ padding::ZEROES => '0',
+ * => ' ',
+ });
+ };
+
+ if (mod.padding == padding::ALIGN_RIGHT) {
+ format_raw(out, arg, mod);
+ };
+
+ for (z < mod.width: size) {
+ z += io::write(out, pad)?;
+ };
+
+ if (mod.padding != padding::ALIGN_RIGHT) {
+ format_raw(out, arg, mod);
+ };
+
+ return z;
+};
+
+fn format_raw(
out: *io::stream,
arg: formattable,
mod: *modifiers,
-) void = match (arg) {
+) (size | io::error) = match (arg) {
s: str => io::write(out, strings::to_utf8(s)),
r: rune => io::write(out, utf8::encode_rune(r)),
n: types::numeric => {
@@ -206,13 +232,15 @@ fn format(
v: *void => {
let s = strconv::uptrtosb(v: uintptr,
strconv::base::HEX_LOWER);
- io::write(out, strings::to_utf8("0x"));
- io::write(out, strings::to_utf8(s));
+ let n = io::write(out, strings::to_utf8("0x"))?;
+ n += io::write(out, strings::to_utf8(s))?;
+ n;
},
null => format(out, "(null)", mod),
},
};
+
fn scan_uint(iter: *strings::iterator) uint = {
let num: []u8 = [];
defer free(num);