| author | |
| committer | |
| log | e8ca1b254d41d5711dc5294d99b8d81c74f36add |
| tree | d973ed668689d77f0f2df5e3943d6c5272adee78 |
| parent | 2c294676b52f2ba62172fa778a198f92d6a969f0 |
| signature |
6 files changed, 28 insertions(+), 69 deletions(-)
lib/std/build.zig+4-11| ... | ... | @@ -1769,24 +1769,19 @@ pub const LibExeObjStep = struct { |
| 1769 | 1769 | []const []const u8 => { |
| 1770 | 1770 | out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable; |
| 1771 | 1771 | for (value) |slice| { |
| 1772 | out.writeAll(" ") catch unreachable; | |
| 1773 | std.zig.renderStringLiteral(slice, out) catch unreachable; | |
| 1774 | out.writeAll(",\n") catch unreachable; | |
| 1772 | out.print(" \"{Z}\",\n", .{slice}) catch unreachable; | |
| 1775 | 1773 | } |
| 1776 | 1774 | out.writeAll("};\n") catch unreachable; |
| 1777 | 1775 | return; |
| 1778 | 1776 | }, |
| 1779 | 1777 | []const u8 => { |
| 1780 | out.print("pub const {z}: []const u8 = ", .{name}) catch unreachable; | |
| 1781 | std.zig.renderStringLiteral(value, out) catch unreachable; | |
| 1782 | out.writeAll(";\n") catch unreachable; | |
| 1778 | out.print("pub const {z}: []const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable; | |
| 1783 | 1779 | return; |
| 1784 | 1780 | }, |
| 1785 | 1781 | ?[]const u8 => { |
| 1786 | 1782 | out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable; |
| 1787 | 1783 | if (value) |payload| { |
| 1788 | std.zig.renderStringLiteral(payload, out) catch unreachable; | |
| 1789 | out.writeAll(";\n") catch unreachable; | |
| 1784 | out.print("\"{Z}\";\n", .{payload}) catch unreachable; | |
| 1790 | 1785 | } else { |
| 1791 | 1786 | out.writeAll("null;\n") catch unreachable; |
| 1792 | 1787 | } |
| ... | ... | @@ -2017,9 +2012,7 @@ pub const LibExeObjStep = struct { |
| 2017 | 2012 | // Render build artifact options at the last minute, now that the path is known. |
| 2018 | 2013 | for (self.build_options_artifact_args.items) |item| { |
| 2019 | 2014 | const out = self.build_options_contents.writer(); |
| 2020 | out.print("pub const {}: []const u8 = ", .{item.name}) catch unreachable; | |
| 2021 | std.zig.renderStringLiteral(item.artifact.getOutputPath(), out) catch unreachable; | |
| 2022 | out.writeAll(";\n") catch unreachable; | |
| 2015 | out.print("pub const {}: []const u8 = \"{Z}\";\n", .{ item.name, item.artifact.getOutputPath() }) catch unreachable; | |
| 2023 | 2016 | } |
| 2024 | 2017 | |
| 2025 | 2018 | const build_options_file = try fs.path.join( |
lib/std/fmt.zig+18-22| ... | ... | @@ -552,7 +552,7 @@ pub fn formatIntValue( |
| 552 | 552 | } else { |
| 553 | 553 | @compileError("Cannot escape character with more than 8 bits"); |
| 554 | 554 | } |
| 555 | }else if (comptime std.mem.eql(u8, fmt, "b")) { | |
| 555 | } else if (comptime std.mem.eql(u8, fmt, "b")) { | |
| 556 | 556 | radix = 2; |
| 557 | 557 | uppercase = false; |
| 558 | 558 | } else if (comptime std.mem.eql(u8, fmt, "x")) { |
| ... | ... | @@ -695,27 +695,20 @@ pub fn formatZigEscapes( |
| 695 | 695 | options: FormatOptions, |
| 696 | 696 | writer: anytype, |
| 697 | 697 | ) !void { |
| 698 | for (bytes) |c| { | |
| 699 | const s: []const u8 = switch (c) { | |
| 700 | '\"' => "\\\"", | |
| 701 | '\'' => "\\'", | |
| 702 | '\\' => "\\\\", | |
| 703 | '\n' => "\\n", | |
| 704 | '\r' => "\\r", | |
| 705 | '\t' => "\\t", | |
| 706 | // Handle the remaining escapes Zig doesn't support by turning them | |
| 707 | // into their respective hex representation | |
| 708 | else => if (std.ascii.isCntrl(c)) { | |
| 709 | try writer.writeAll("\\x"); | |
| 710 | try formatInt(c, 16, false, .{ .width = 2, .fill = '0' }, writer); | |
| 711 | continue; | |
| 712 | } else { | |
| 713 | try writer.writeByte(c); | |
| 714 | continue; | |
| 715 | }, | |
| 716 | }; | |
| 717 | try writer.writeAll(s); | |
| 718 | } | |
| 698 | for (bytes) |byte| switch (byte) { | |
| 699 | '\n' => try writer.writeAll("\\n"), | |
| 700 | '\r' => try writer.writeAll("\\r"), | |
| 701 | '\t' => try writer.writeAll("\\t"), | |
| 702 | '\\' => try writer.writeAll("\\\\"), | |
| 703 | '"' => try writer.writeAll("\\\""), | |
| 704 | '\'' => try writer.writeAll("\\'"), | |
| 705 | ' ', '!', '#'...'&', '('...'[', ']'...'~' => try writer.writeByte(byte), | |
| 706 | // Use hex escapes for rest any unprintable characters. | |
| 707 | else => { | |
| 708 | try writer.writeAll("\\x"); | |
| 709 | try formatInt(byte, 16, false, .{ .width = 2, .fill = '0' }, writer); | |
| 710 | }, | |
| 711 | }; | |
| 719 | 712 | } |
| 720 | 713 | |
| 721 | 714 | /// Print a float in scientific notation to the specified precision. Null uses full precision. |
| ... | ... | @@ -1406,6 +1399,9 @@ test "escape invalid identifiers" { |
| 1406 | 1399 | try testFmt("@\"11\\\"23\"", "{z}", .{"11\"23"}); |
| 1407 | 1400 | try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"}); |
| 1408 | 1401 | try testFmt("\\x0f", "{Z}", .{0x0f}); |
| 1402 | try testFmt( | |
| 1403 | \\" \\ hi \x07 \x11 \" derp \'" | |
| 1404 | , "\"{Z}\"", .{" \\ hi \x07 \x11 \" derp '"}); | |
| 1409 | 1405 | } |
| 1410 | 1406 | |
| 1411 | 1407 | test "pointer" { |
lib/std/zig.zig-1| ... | ... | @@ -11,7 +11,6 @@ pub const Tokenizer = tokenizer.Tokenizer; |
| 11 | 11 | pub const parse = @import("zig/parse.zig").parse; |
| 12 | 12 | pub const parseStringLiteral = @import("zig/string_literal.zig").parse; |
| 13 | 13 | pub const render = @import("zig/render.zig").render; |
| 14 | pub const renderStringLiteral = @import("zig/string_literal.zig").render; | |
| 15 | 14 | pub const ast = @import("zig/ast.zig"); |
| 16 | 15 | pub const system = @import("zig/system.zig"); |
| 17 | 16 | pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget; |
lib/std/zig/string_literal.zig-30| ... | ... | @@ -127,33 +127,3 @@ test "parse" { |
| 127 | 127 | expect(eql(u8, "foo", try parse(alloc, "\"f\x6f\x6f\"", &bad_index))); |
| 128 | 128 | expect(eql(u8, "f💯", try parse(alloc, "\"f\u{1f4af}\"", &bad_index))); |
| 129 | 129 | } |
| 130 | ||
| 131 | /// Writes a Zig-syntax escaped string literal to the stream. Includes the double quotes. | |
| 132 | pub fn render(utf8: []const u8, out_stream: anytype) !void { | |
| 133 | try out_stream.writeByte('"'); | |
| 134 | for (utf8) |byte| switch (byte) { | |
| 135 | '\n' => try out_stream.writeAll("\\n"), | |
| 136 | '\r' => try out_stream.writeAll("\\r"), | |
| 137 | '\t' => try out_stream.writeAll("\\t"), | |
| 138 | '\\' => try out_stream.writeAll("\\\\"), | |
| 139 | '"' => try out_stream.writeAll("\\\""), | |
| 140 | ' ', '!', '#'...'[', ']'...'~' => try out_stream.writeByte(byte), | |
| 141 | else => try out_stream.print("\\x{x:0>2}", .{byte}), | |
| 142 | }; | |
| 143 | try out_stream.writeByte('"'); | |
| 144 | } | |
| 145 | ||
| 146 | test "render" { | |
| 147 | const expect = std.testing.expect; | |
| 148 | const eql = std.mem.eql; | |
| 149 | ||
| 150 | var fixed_buf_mem: [32]u8 = undefined; | |
| 151 | ||
| 152 | { | |
| 153 | var fbs = std.io.fixedBufferStream(&fixed_buf_mem); | |
| 154 | try render(" \\ hi \x07 \x11 \" derp", fbs.outStream()); | |
| 155 | expect(eql(u8, | |
| 156 | \\" \\ hi \x07 \x11 \" derp" | |
| 157 | , fbs.getWritten())); | |
| 158 | } | |
| 159 | } |
src/value.zig+2-1| ... | ... | @@ -350,7 +350,8 @@ pub const Value = extern union { |
| 350 | 350 | val = elem_ptr.array_ptr; |
| 351 | 351 | }, |
| 352 | 352 | .empty_array => return out_stream.writeAll(".{}"), |
| 353 | .enum_literal, .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream), | |
| 353 | .enum_literal => return out_stream.print(".{z}", .{self.cast(Payload.Bytes).?.data}), | |
| 354 | .bytes => return out_stream.print("\"{Z}\"", .{self.cast(Payload.Bytes).?.data}), | |
| 354 | 355 | .repeated => { |
| 355 | 356 | try out_stream.writeAll("(repeated) "); |
| 356 | 357 | val = val.cast(Payload.Repeated).?.val; |
src/zir.zig+4-4| ... | ... | @@ -1216,17 +1216,17 @@ const Writer = struct { |
| 1216 | 1216 | try stream.writeByte('}'); |
| 1217 | 1217 | }, |
| 1218 | 1218 | bool => return stream.writeByte("01"[@boolToInt(param)]), |
| 1219 | []u8, []const u8 => return std.zig.renderStringLiteral(param, stream), | |
| 1219 | []u8, []const u8 => return stream.print("\"{Z}\"", .{param}), | |
| 1220 | 1220 | BigIntConst, usize => return stream.print("{}", .{param}), |
| 1221 | 1221 | TypedValue => unreachable, // this is a special case |
| 1222 | 1222 | *IrModule.Decl => unreachable, // this is a special case |
| 1223 | 1223 | *Inst.Block => { |
| 1224 | 1224 | const name = self.block_table.get(param).?; |
| 1225 | return std.zig.renderStringLiteral(name, stream); | |
| 1225 | return stream.print("\"{Z}\"", .{name}); | |
| 1226 | 1226 | }, |
| 1227 | 1227 | *Inst.Loop => { |
| 1228 | 1228 | const name = self.loop_table.get(param).?; |
| 1229 | return std.zig.renderStringLiteral(name, stream); | |
| 1229 | return stream.print("\"{Z}\"", .{name}); | |
| 1230 | 1230 | }, |
| 1231 | 1231 | [][]const u8 => { |
| 1232 | 1232 | try stream.writeByte('['); |
| ... | ... | @@ -1234,7 +1234,7 @@ const Writer = struct { |
| 1234 | 1234 | if (i != 0) { |
| 1235 | 1235 | try stream.writeAll(", "); |
| 1236 | 1236 | } |
| 1237 | try std.zig.renderStringLiteral(str, stream); | |
| 1237 | try stream.print("\"{Z}\"", .{str}); | |
| 1238 | 1238 | } |
| 1239 | 1239 | try stream.writeByte(']'); |
| 1240 | 1240 | }, |