From db72cc51c1b21203f4c2b0e690a7d2cab91c2f1f Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 25 Apr 2023 23:23:39 -0700 Subject: [PATCH] add writeEscaped() public helper function --- src/lib.zig | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib.zig b/src/lib.zig index efef6c0b2746ea1fc831a0da547b5932acb7b218..2b968fdb39c5826cc7e05e937c5ad368f71de9c9 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -84,13 +84,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp return; } const s = std.mem.trimRight(u8, x, "\n"); - for (s) |c| { - if (entityLookupBefore(&[_]u8{c})) |ent| { - try writer.writeAll(ent.entity); - } else { - try writer.writeAll(&[_]u8{c}); - } - } + try writeEscaped(s, writer); return; } if (TI == .Int or TI == .Float or TI == .ComptimeInt or TI == .ComptimeFloat) { @@ -209,6 +203,16 @@ fn Field(comptime T: type, comptime field_name: string) type { @compileError(std.fmt.comptimePrint("pek: unknown field {s} on type {s}", .{ field_name, @typeName(T) })); } +pub fn writeEscaped(s: string, writer: anytype) !void { + for (s) |c| { + if (entityLookupBefore(&[_]u8{c})) |ent| { + try writer.writeAll(ent.entity); + } else { + try writer.writeAll(&[_]u8{c}); + } + } +} + fn entityLookupBefore(in: string) ?htmlentities.Entity { for (htmlentities.ENTITIES) |e| { if (!std.mem.endsWith(u8, e.entity, ";")) { -- 2.54.0