commit df58525ba5493bd23d74902555a2cb25257b5105
parent c15ce212f16de8dbfe4221b1f4bec7159129f956
Author: Sebastian <sebastian@sebsite.pw>
Date: Fri, 29 Sep 2023 22:20:02 -0400
hare::unparse: use strings::tokenizer for comments
Rather than using strings::split, so we don't need to allocate.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/hare/unparse/decl.ha b/hare/unparse/decl.ha
@@ -160,16 +160,18 @@ export fn decl(
fn comment(ctx: *context, syn: *synfunc, s: str) (size | io::error) = {
let n = 0z;
- const lines = strings::split(s, "\n");
- defer free(lines);
- assert(len(lines) > 0);
- for (let i = 0z; i < len(lines) - 1; i += 1) {
+ let s = strings::trimsuffix(s, "\n");
+ let s = strings::tokenize(s, "\n");
+ for (true) match (strings::next_token(&s)) {
+ case void =>
+ break;
+ case let line: str =>
for (let j = 0z; j < ctx.indent; j += 1) {
n += fmt::fprint(ctx.out, "\t")?;
ctx.linelen += 8;
};
n += syn(ctx, "//", synkind::COMMENT)?;
- n += syn(ctx, lines[i], synkind::COMMENT)?;
+ n += syn(ctx, line, synkind::COMMENT)?;
n += fmt::fprintln(ctx.out)?;
ctx.linelen = 0;
};