1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
use ascii;
use strings;
use strconv;
use types;
// Tagged union of the [[formattable]] types and [[mods]]. Used for
// functions which accept format strings.
export type field = (...formattable | *mods);
// Tagged union of all types which are formattable.
export type formattable = (...types::numeric | uintptr | str | rune | bool |
nullable *opaque | void);
// Negative modifier. Specifies for numerical arguments when to prepend a plus
// or minus sign or a blank space.
export type neg = enum {
NONE,
SPACE,
PLUS,
};
// Alignment modifier. Specifies how to align an argument within a given width.
export type alignment = enum {
RIGHT,
CENTER,
LEFT,
};
// Specifies how to format an argument.
export type mods = struct {
alignment: alignment,
pad: rune,
neg: neg,
width: size,
prec: size,
base: strconv::base,
ffmt: strconv::ffmt,
fflags: strconv::fflags,
};
type iterator = struct {
iter: strings::iterator,
args: []field,
idx: size,
checkunused: bool,
};
fn iter(fmt: str, args: []field) iterator = iterator {
iter = strings::iter(fmt),
args = args,
idx = 0,
checkunused = true,
};
fn next(it: *iterator) (str | (formattable, mods) | done) = {
let r = match (strings::next(&it.iter)) {
case done =>
return done;
case let r: rune =>
yield r;
};
switch (r) {
case '{' => void; // handled below
case '}' =>
match (strings::next(&it.iter)) {
case done =>
abort("Invalid format string (hanging '}')");
case let r: rune =>
assert(r == '}', "Invalid format string (hanging '}')");
};
return "}";
case =>
strings::prev(&it.iter);
let start = it.iter;
for (let r => strings::next(&it.iter)) {
if (r == '{' || r == '}') {
strings::prev(&it.iter);
break;
};
};
return strings::slice(&start, &it.iter);
};
r = getrune(it);
if (r == '{') {
return "{";
};
let idx = if (ascii::isdigit(r)) {
strings::prev(&it.iter);
it.checkunused = false;
defer r = getrune(it);
yield scan_sz(it);
} else {
defer it.idx += 1;
yield it.idx;
};
assert(idx < len(it.args), "Not enough parameters given");
let arg = it.args[idx] as formattable;
let mod = mods { ... };
switch (r) {
case ':' =>
scan_modifiers(it, &mod);
case '%' =>
r = getrune(it);
let idx = if (ascii::isdigit(r)) {
strings::prev(&it.iter);
it.checkunused = false;
defer r = getrune(it);
yield scan_sz(it);
} else {
defer it.idx += 1;
yield it.idx;
};
assert(idx < len(it.args), "Not enough parameters given");
mod = *(it.args[idx] as *mods);
assert(r == '}', "Invalid format string (didn't find '}' after modifier index)");
case '}' => void;
case => abort("Invalid format string");
};
return (arg, mod);
};
fn scan_modifiers(it: *iterator, mod: *mods) void = {
mod.pad = ' ';
for (true) switch (getrune(it)) {
// alignment
case '-' => mod.alignment = alignment::LEFT;
case '=' => mod.alignment = alignment::CENTER;
// padding
case '_' => mod.pad = getrune(it);
// negation
case ' ' => mod.neg = neg::SPACE;
case '+' => mod.neg = neg::PLUS;
// base
case 'x' => mod.base = strconv::base::HEX_LOWER;
case 'X' => mod.base = strconv::base::HEX_UPPER;
case 'o' => mod.base = strconv::base::OCT;
case 'b' => mod.base = strconv::base::BIN;
// ffmt
case 'e' => mod.ffmt = strconv::ffmt::E;
case 'f' => mod.ffmt = strconv::ffmt::F;
case 'g' => mod.ffmt = strconv::ffmt::G;
// fflags
case 'F' =>
switch (getrune(it)) {
case 's' => mod.fflags |= strconv::fflags::SHOW_POS;
case '.' => mod.fflags |= strconv::fflags::SHOW_POINT;
case 'U' => mod.fflags |= strconv::fflags::UPPERCASE;
case 'E' => mod.fflags |= strconv::fflags::UPPER_EXP;
case 'S' => mod.fflags |= strconv::fflags::SHOW_POS_EXP;
case '2' => mod.fflags |= strconv::fflags::SHOW_TWO_EXP_DIGITS;
case => abort("Invalid float flag");
};
// precision
case '.' => mod.prec = scan_sz(it);
// width
case '1', '2', '3', '4', '5', '6', '7', '8', '9' =>
strings::prev(it);
mod.width = scan_sz(it);
case =>
strings::prev(it);
break;
};
assert(getrune(it) == '}', "Invalid format string (unterminated '{')");
};
fn scan_sz(it: *iterator) size = {
let start = it.iter;
assert(ascii::isdigit(getrune(it)));
for (ascii::isdigit(getrune(it))) void;
strings::prev(&it.iter);
match (strconv::stoz(strings::slice(&start, &it.iter))) {
case strconv::invalid =>
abort("Invalid format string (invalid integer)");
case strconv::overflow =>
abort("Invalid format string (integer overflow)");
case let z: size =>
return z;
};
};
fn getrune(it: *iterator) rune = {
match (strings::next(&it.iter)) {
case done =>
abort("Invalid format string (unterminated '{')");
case let r: rune =>
return r;
};
};
|