@@ -94,7 +94,7 @@ fn getFortunesHtml(allocator: std.mem.Allocator, pool: *pg.Pool) ![]const u8 {
9494 for (fortunes ) | ft | {
9595 try writer .print ("<tr><td>{d}</td><td>{s}</td></tr>" , .{
9696 ft .id ,
97- try deescapeHtml (allocator , ft .message ),
97+ try escape_html (allocator , ft .message ),
9898 });
9999 }
100100
@@ -131,51 +131,15 @@ fn cmpFortuneByMessage(_: void, a: Fortune, b: Fortune) bool {
131131 return std .mem .order (u8 , a .message , b .message ).compare (std .math .CompareOperator .lt );
132132}
133133
134- fn deescapeHtml (allocator : std.mem.Allocator , input : []const u8 ) ! []const u8 {
134+ fn escape_html (allocator : std.mem.Allocator , input : []const u8 ) ! []const u8 {
135135 var output = try std .ArrayListUnmanaged (u8 ).initCapacity (allocator , 0 );
136136 defer output .deinit (allocator );
137137
138- var i : usize = 0 ;
139- while (i < input .len ) {
140- if (std .mem .startsWith (u8 , input [i .. ], " " )) {
141- try output .append (allocator , ' ' );
142- i += 5 ;
143- } else if (std .mem .startsWith (u8 , input [i .. ], """ )) {
144- try output .append (allocator , '"' );
145- i += 5 ;
146- } else if (std .mem .startsWith (u8 , input [i .. ], "&" )) {
147- try output .append (allocator , '&' );
148- i += 5 ;
149- } else if (std .mem .startsWith (u8 , input [i .. ], "'" )) {
150- try output .append (allocator , '\' ' );
151- i += 5 ;
152- } else if (std .mem .startsWith (u8 , input [i .. ], "(" )) {
153- try output .append (allocator , '(' );
154- i += 5 ;
155- } else if (std .mem .startsWith (u8 , input [i .. ], ")" )) {
156- try output .append (allocator , ')' );
157- i += 5 ;
158- } else if (std .mem .startsWith (u8 , input [i .. ], "+" )) {
159- try output .append (allocator , '+' );
160- i += 5 ;
161- } else if (std .mem .startsWith (u8 , input [i .. ], "," )) {
162- try output .append (allocator , ',' );
163- i += 5 ;
164- } else if (std .mem .startsWith (u8 , input [i .. ], "." )) {
165- try output .append (allocator , '.' );
166- i += 5 ;
167- } else if (std .mem .startsWith (u8 , input [i .. ], "/" )) {
168- try output .append (allocator , '/' );
169- i += 5 ;
170- } else if (std .mem .startsWith (u8 , input [i .. ], ":" )) {
171- try output .append (allocator , ':' );
172- i += 5 ;
173- } else if (std .mem .startsWith (u8 , input [i .. ], ";" )) {
174- try output .append (allocator , ';' );
175- i += 5 ;
176- } else {
177- try output .append (allocator , input [i ]);
178- i += 1 ;
138+ for (input ) | char | {
139+ switch (char ) {
140+ '<' = > try output .appendSlice (allocator , "<" ),
141+ '>' = > try output .appendSlice (allocator , ">" ),
142+ else = > try output .append (allocator , char ),
179143 }
180144 }
181145
0 commit comments