| ... | ... | @@ -8157,8 +8157,8 @@ fn compareOperatorC(operator: std.math.CompareOperator) []const u8 { |
| 8157 | 8157 | const StringLiteral = struct { |
| 8158 | 8158 | len: usize, |
| 8159 | 8159 | cur_len: usize, |
| 8160 | | start_count: usize, |
| 8161 | 8160 | w: *Writer, |
| 8161 | first: bool, |
| 8162 | 8162 | |
| 8163 | 8163 | // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal, |
| 8164 | 8164 | // regardless of the length of the string literal initializing it. Array initializer syntax is |
| ... | ... | @@ -8175,8 +8175,8 @@ const StringLiteral = struct { |
| 8175 | 8175 | return .{ |
| 8176 | 8176 | .cur_len = 0, |
| 8177 | 8177 | .len = len, |
| 8178 | | .start_count = w.count, |
| 8179 | 8178 | .w = w, |
| 8179 | .first = true, |
| 8180 | 8180 | }; |
| 8181 | 8181 | } |
| 8182 | 8182 | |
| ... | ... | @@ -8196,38 +8196,73 @@ const StringLiteral = struct { |
| 8196 | 8196 | } |
| 8197 | 8197 | } |
| 8198 | 8198 | |
| 8199 | | fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!void { |
| 8199 | fn writeStringLiteralChar(sl: *StringLiteral, c: u8) Writer.Error!usize { |
| 8200 | const w = sl.w; |
| 8200 | 8201 | switch (c) { |
| 8201 | | 7 => try sl.w.writeAll("\\a"), |
| 8202 | | 8 => try sl.w.writeAll("\\b"), |
| 8203 | | '\t' => try sl.w.writeAll("\\t"), |
| 8204 | | '\n' => try sl.w.writeAll("\\n"), |
| 8205 | | 11 => try sl.w.writeAll("\\v"), |
| 8206 | | 12 => try sl.w.writeAll("\\f"), |
| 8207 | | '\r' => try sl.w.writeAll("\\r"), |
| 8208 | | '"', '\'', '?', '\\' => try sl.w.print("\\{c}", .{c}), |
| 8209 | | else => switch (c) { |
| 8210 | | ' '...'~' => try sl.w.writeByte(c), |
| 8211 | | else => try sl.w.print("\\{o:0>3}", .{c}), |
| 8202 | 7 => { |
| 8203 | try w.writeAll("\\a"); |
| 8204 | return 2; |
| 8205 | }, |
| 8206 | 8 => { |
| 8207 | try w.writeAll("\\b"); |
| 8208 | return 2; |
| 8209 | }, |
| 8210 | '\t' => { |
| 8211 | try w.writeAll("\\t"); |
| 8212 | return 2; |
| 8213 | }, |
| 8214 | '\n' => { |
| 8215 | try w.writeAll("\\n"); |
| 8216 | return 2; |
| 8217 | }, |
| 8218 | 11 => { |
| 8219 | try w.writeAll("\\v"); |
| 8220 | return 2; |
| 8221 | }, |
| 8222 | 12 => { |
| 8223 | try w.writeAll("\\f"); |
| 8224 | return 2; |
| 8225 | }, |
| 8226 | '\r' => { |
| 8227 | try w.writeAll("\\r"); |
| 8228 | return 2; |
| 8229 | }, |
| 8230 | '"', '\'', '?', '\\' => { |
| 8231 | try w.print("\\{c}", .{c}); |
| 8232 | return 2; |
| 8233 | }, |
| 8234 | ' '...'!', '#'...'&', '('...'>', '@'...'[', ']'...'~' => { |
| 8235 | try w.writeByte(c); |
| 8236 | return 1; |
| 8237 | }, |
| 8238 | else => { |
| 8239 | var buf: [4]u8 = undefined; |
| 8240 | const printed = std.fmt.bufPrint(&buf, "\\{o:0>3}", .{c}) catch unreachable; |
| 8241 | try w.writeAll(printed); |
| 8242 | return printed.len; |
| 8212 | 8243 | }, |
| 8213 | 8244 | } |
| 8214 | 8245 | } |
| 8215 | 8246 | |
| 8216 | 8247 | pub fn writeChar(sl: *StringLiteral, c: u8) Writer.Error!void { |
| 8217 | 8248 | if (sl.len <= max_string_initializer_len) { |
| 8218 | | if (sl.cur_len == 0 and sl.w.count - sl.start_count > 1) |
| 8219 | | try sl.w.writeAll("\"\""); |
| 8249 | if (sl.cur_len == 0 and !sl.first) try sl.w.writeAll("\"\""); |
| 8220 | 8250 | |
| 8221 | | const count = sl.w.count; |
| 8222 | | try sl.writeStringLiteralChar(c); |
| 8223 | | const char_len = sl.w.count - count; |
| 8251 | const char_len = try sl.writeStringLiteralChar(c); |
| 8224 | 8252 | assert(char_len <= max_char_len); |
| 8225 | 8253 | sl.cur_len += char_len; |
| 8226 | 8254 | |
| 8227 | | if (sl.cur_len >= max_literal_len) sl.cur_len = 0; |
| 8255 | if (sl.cur_len >= max_literal_len) { |
| 8256 | sl.cur_len = 0; |
| 8257 | sl.first = false; |
| 8258 | } |
| 8228 | 8259 | } else { |
| 8229 | | if (sl.w.count - sl.start_count > 1) try sl.w.writeByte(','); |
| 8230 | | try sl.w.print("'\\x{x}'", .{c}); |
| 8260 | if (!sl.first) try sl.w.writeByte(','); |
| 8261 | var buf: [6]u8 = undefined; |
| 8262 | const printed = std.fmt.bufPrint(&buf, "'\\x{x}'", .{c}) catch unreachable; |
| 8263 | try sl.w.writeAll(printed); |
| 8264 | sl.cur_len += printed.len; |
| 8265 | sl.first = false; |
| 8231 | 8266 | } |
| 8232 | 8267 | } |
| 8233 | 8268 | }; |