authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-07-28 18:38:15-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-07-28 18:38:15-07:00
log47f06ffe0c6b5746004607241d171cdf1f380548
tree6b4487c57e21221e5576944d5b29d8eeda9674e5
parent3cdacb14fb3db5c3154a9e00622b0af1208a0ccd
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

remove raw param from writeReplacementString

as a result stop buffering the result of raw functions

1 files changed, 9 insertions(+), 18 deletions(-)

src/lib.zig+9-18
......@@ -127,17 +127,15 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
127127 const TI = @typeInfo(TO);
128128
129129 if (comptime extras.isZigString(TO)) {
130 return writeReplacementString(writer, repl.raw, opts.escaped, x);
130 if (repl.raw) return writer.writeAll(x);
131 return writeReplacementString(writer, opts.escaped, x);
131132 }
132133 if (TI == .int or TI == .float or TI == .comptime_int or TI == .comptime_float) {
133134 try writer.print("{d}", .{x});
134135 return;
135136 }
136137 if (comptime isArrayOf(u8)(TO)) {
137 if (repl.raw) {
138 try writer.writeAll(&x);
139 return;
140 }
138 if (repl.raw) return writer.writeAll(&x);
141139 const s = std.mem.trim(u8, &x, "\n");
142140 if (opts.escaped) try writeEscaped(s, writer);
143141 if (!opts.escaped) try writer.writeAll(s);
......@@ -157,7 +155,8 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
157155 }
158156 if (TI == .optional) {
159157 if (comptime extras.isZigString(std.meta.Child(TO))) {
160 return writeReplacementString(writer, repl.raw, opts.escaped, x.?);
158 if (repl.raw) return writer.writeAll(x.?);
159 return writeReplacementString(writer, opts.escaped, x.?);
161160 }
162161 }
163162 return x.nprint(writer);
......@@ -310,27 +309,23 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val
310309 var args: std.meta.Tuple(&types) = undefined;
311310 inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts);
312311 try @call(.auto, func, .{ alloc, &list } ++ args);
313 try writeReplacementString(writer, v.raw, opts.escaped, list.items);
312 try writeReplacementString(writer, opts.escaped, list.items);
314313 return;
315314 }
316315 if (v.raw) {
317316 if (@hasDecl(opts.Ctx, "pek_" ++ v.name)) @compileError("pek: attempted to call safe custom function: '" ++ v.name ++ "' but did not use '{" ++ v.name ++ "}'");
318317 const func = @field(opts.Ctx, "pek__" ++ v.name);
319 var list: nio.AllocatingWriter = .init(alloc);
320 defer list.deinit();
321318 const Tup = @typeInfo(@TypeOf(func)).@"fn".params[3].type.?;
322319 if (@typeInfo(Tup).@"struct".fields.len == 0) {
323320 // edge case branch because 'struct {}' is counted as a non-tuple
324 try @call(.auto, func, .{ alloc, &list, opts, Tup{} });
325 try writeReplacementString(writer, v.raw, opts.escaped, list.items);
321 try @call(.auto, func, .{ alloc, writer, opts, Tup{} });
326322 return;
327323 }
328324 comptime var types: [v.args.len]type = @splat(void);
329325 inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx));
330326 var args: std.meta.Tuple(&types) = undefined;
331327 inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts);
332 try @call(.auto, func, .{ alloc, &list, opts, args });
333 try writeReplacementString(writer, v.raw, opts.escaped, list.items);
328 try @call(.auto, func, .{ alloc, writer, opts, args });
334329 return;
335330 }
336331 comptime unreachable;
......@@ -423,11 +418,7 @@ pub fn writeEscaped(s: string, writer: anytype) !void {
423418 }
424419}
425420
426fn writeReplacementString(writer: anytype, raw: bool, escaped: bool, bytes: []const u8) !void {
427 if (raw) {
428 try writer.writeAll(bytes);
429 return;
430 }
421fn writeReplacementString(writer: anytype, escaped: bool, bytes: []const u8) !void {
431422 const s = std.mem.trim(u8, bytes, "\n");
432423 if (escaped) try writeEscaped(s, writer);
433424 if (!escaped) try writer.writeAll(s);