| ... | @@ -127,17 +127,15 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val | ... | @@ -127,17 +127,15 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 127 | const TI = @typeInfo(TO); | 127 | const TI = @typeInfo(TO); |
| 128 | | 128 | |
| 129 | if (comptime extras.isZigString(TO)) { | 129 | 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); |
| 131 | } | 132 | } |
| 132 | if (TI == .int or TI == .float or TI == .comptime_int or TI == .comptime_float) { | 133 | if (TI == .int or TI == .float or TI == .comptime_int or TI == .comptime_float) { |
| 133 | try writer.print("{d}", .{x}); | 134 | try writer.print("{d}", .{x}); |
| 134 | return; | 135 | return; |
| 135 | } | 136 | } |
| 136 | if (comptime isArrayOf(u8)(TO)) { | 137 | if (comptime isArrayOf(u8)(TO)) { |
| 137 | if (repl.raw) { | 138 | if (repl.raw) return writer.writeAll(&x); |
| 138 | try writer.writeAll(&x); | | |
| 139 | return; | | |
| 140 | } | | |
| 141 | const s = std.mem.trim(u8, &x, "\n"); | 139 | const s = std.mem.trim(u8, &x, "\n"); |
| 142 | if (opts.escaped) try writeEscaped(s, writer); | 140 | if (opts.escaped) try writeEscaped(s, writer); |
| 143 | if (!opts.escaped) try writer.writeAll(s); | 141 | if (!opts.escaped) try writer.writeAll(s); |
| ... | @@ -157,7 +155,8 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val | ... | @@ -157,7 +155,8 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 157 | } | 155 | } |
| 158 | if (TI == .optional) { | 156 | if (TI == .optional) { |
| 159 | if (comptime extras.isZigString(std.meta.Child(TO))) { | 157 | 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.?); |
| 161 | } | 160 | } |
| 162 | } | 161 | } |
| 163 | return x.nprint(writer); | 162 | return x.nprint(writer); |
| ... | @@ -310,27 +309,23 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val | ... | @@ -310,27 +309,23 @@ fn doInner(alloc: std.mem.Allocator, writer: anytype, comptime value: astgen.Val |
| 310 | var args: std.meta.Tuple(&types) = undefined; | 309 | var args: std.meta.Tuple(&types) = undefined; |
| 311 | inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts); | 310 | inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts); |
| 312 | try @call(.auto, func, .{ alloc, &list } ++ args); | 311 | 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); |
| 314 | return; | 313 | return; |
| 315 | } | 314 | } |
| 316 | if (v.raw) { | 315 | if (v.raw) { |
| 317 | if (@hasDecl(opts.Ctx, "pek_" ++ v.name)) @compileError("pek: attempted to call safe custom function: '" ++ v.name ++ "' but did not use '{" ++ v.name ++ "}'"); | 316 | if (@hasDecl(opts.Ctx, "pek_" ++ v.name)) @compileError("pek: attempted to call safe custom function: '" ++ v.name ++ "' but did not use '{" ++ v.name ++ "}'"); |
| 318 | const func = @field(opts.Ctx, "pek__" ++ v.name); | 317 | const func = @field(opts.Ctx, "pek__" ++ v.name); |
| 319 | var list: nio.AllocatingWriter = .init(alloc); | | |
| 320 | defer list.deinit(); | | |
| 321 | const Tup = @typeInfo(@TypeOf(func)).@"fn".params[3].type.?; | 318 | const Tup = @typeInfo(@TypeOf(func)).@"fn".params[3].type.?; |
| 322 | if (@typeInfo(Tup).@"struct".fields.len == 0) { | 319 | if (@typeInfo(Tup).@"struct".fields.len == 0) { |
| 323 | // edge case branch because 'struct {}' is counted as a non-tuple | 320 | // edge case branch because 'struct {}' is counted as a non-tuple |
| 324 | try @call(.auto, func, .{ alloc, &list, opts, Tup{} }); | 321 | try @call(.auto, func, .{ alloc, writer, opts, Tup{} }); |
| 325 | try writeReplacementString(writer, v.raw, opts.escaped, list.items); | | |
| 326 | return; | 322 | return; |
| 327 | } | 323 | } |
| 328 | comptime var types: [v.args.len]type = @splat(void); | 324 | comptime var types: [v.args.len]type = @splat(void); |
| 329 | inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx)); | 325 | inline for (v.args, &types) |arg, *T| T.* = ResolveArg(arg, @TypeOf(data), @TypeOf(ctx)); |
| 330 | var args: std.meta.Tuple(&types) = undefined; | 326 | var args: std.meta.Tuple(&types) = undefined; |
| 331 | inline for (v.args, 0..) |arg, i| args[i] = try resolveArg(arg, alloc, data, ctx, opts); | 327 | 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 }); | 328 | try @call(.auto, func, .{ alloc, writer, opts, args }); |
| 333 | try writeReplacementString(writer, v.raw, opts.escaped, list.items); | | |
| 334 | return; | 329 | return; |
| 335 | } | 330 | } |
| 336 | comptime unreachable; | 331 | comptime unreachable; |
| ... | @@ -423,11 +418,7 @@ pub fn writeEscaped(s: string, writer: anytype) !void { | ... | @@ -423,11 +418,7 @@ pub fn writeEscaped(s: string, writer: anytype) !void { |
| 423 | } | 418 | } |
| 424 | } | 419 | } |
| 425 | | 420 | |
| 426 | fn writeReplacementString(writer: anytype, raw: bool, escaped: bool, bytes: []const u8) !void { | 421 | fn writeReplacementString(writer: anytype, escaped: bool, bytes: []const u8) !void { |
| 427 | if (raw) { | | |
| 428 | try writer.writeAll(bytes); | | |
| 429 | return; | | |
| 430 | } | | |
| 431 | const s = std.mem.trim(u8, bytes, "\n"); | 422 | const s = std.mem.trim(u8, bytes, "\n"); |
| 432 | if (escaped) try writeEscaped(s, writer); | 423 | if (escaped) try writeEscaped(s, writer); |
| 433 | if (!escaped) try writer.writeAll(s); | 424 | if (!escaped) try writer.writeAll(s); |