authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 23:23:39 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-04-25 23:23:39 -07:00
logdb72cc51c1b21203f4c2b0e690a7d2cab91c2f1f
treede12c48fbf06de6f83342985eeb3548285f23fa8
parent1736776c117879aa4b0328459893740859e4801c

add writeEscaped() public helper function


1 files changed, 11 insertions(+), 7 deletions(-)

src/lib.zig+11-7
...@@ -84,13 +84,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp...@@ -84,13 +84,7 @@ inline fn do(comptime Ctx: type, alloc: std.mem.Allocator, writer: anytype, comp
84 return;84 return;
85 }85 }
86 const s = std.mem.trimRight(u8, x, "\n");86 const s = std.mem.trimRight(u8, x, "\n");
87 for (s) |c| {87 try writeEscaped(s, writer);
88 if (entityLookupBefore(&[_]u8{c})) |ent| {
89 try writer.writeAll(ent.entity);
90 } else {
91 try writer.writeAll(&[_]u8{c});
92 }
93 }
94 return;88 return;
95 }89 }
96 if (TI == .Int or TI == .Float or TI == .ComptimeInt or TI == .ComptimeFloat) {90 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 {...@@ -209,6 +203,16 @@ fn Field(comptime T: type, comptime field_name: string) type {
209 @compileError(std.fmt.comptimePrint("pek: unknown field {s} on type {s}", .{ field_name, @typeName(T) }));203 @compileError(std.fmt.comptimePrint("pek: unknown field {s} on type {s}", .{ field_name, @typeName(T) }));
210}204}
211205
206pub fn writeEscaped(s: string, writer: anytype) !void {
207 for (s) |c| {
208 if (entityLookupBefore(&[_]u8{c})) |ent| {
209 try writer.writeAll(ent.entity);
210 } else {
211 try writer.writeAll(&[_]u8{c});
212 }
213 }
214}
215
212fn entityLookupBefore(in: string) ?htmlentities.Entity {216fn entityLookupBefore(in: string) ?htmlentities.Entity {
213 for (htmlentities.ENTITIES) |e| {217 for (htmlentities.ENTITIES) |e| {
214 if (!std.mem.endsWith(u8, e.entity, ";")) {218 if (!std.mem.endsWith(u8, e.entity, ";")) {