authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-17 17:12:58+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-17 23:20:38+03:00
loge8ca1b254d41d5711dc5294d99b8d81c74f36add
treed973ed668689d77f0f2df5e3943d6c5272adee78
parent2c294676b52f2ba62172fa778a198f92d6a969f0
signaturelock-open Commit is signed but in an unrecognized format.

std: remove renderStringLiteral in favor of std.fmt specifier


6 files changed, 28 insertions(+), 69 deletions(-)

lib/std/build.zig+4-11
......@@ -1769,24 +1769,19 @@ pub const LibExeObjStep = struct {
17691769 []const []const u8 => {
17701770 out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
17711771 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;
17751773 }
17761774 out.writeAll("};\n") catch unreachable;
17771775 return;
17781776 },
17791777 []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;
17831779 return;
17841780 },
17851781 ?[]const u8 => {
17861782 out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable;
17871783 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;
17901785 } else {
17911786 out.writeAll("null;\n") catch unreachable;
17921787 }
......@@ -2017,9 +2012,7 @@ pub const LibExeObjStep = struct {
20172012 // Render build artifact options at the last minute, now that the path is known.
20182013 for (self.build_options_artifact_args.items) |item| {
20192014 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;
20232016 }
20242017
20252018 const build_options_file = try fs.path.join(
lib/std/fmt.zig+18-22
......@@ -552,7 +552,7 @@ pub fn formatIntValue(
552552 } else {
553553 @compileError("Cannot escape character with more than 8 bits");
554554 }
555 }else if (comptime std.mem.eql(u8, fmt, "b")) {
555 } else if (comptime std.mem.eql(u8, fmt, "b")) {
556556 radix = 2;
557557 uppercase = false;
558558 } else if (comptime std.mem.eql(u8, fmt, "x")) {
......@@ -695,27 +695,20 @@ pub fn formatZigEscapes(
695695 options: FormatOptions,
696696 writer: anytype,
697697) !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 };
719712}
720713
721714/// Print a float in scientific notation to the specified precision. Null uses full precision.
......@@ -1406,6 +1399,9 @@ test "escape invalid identifiers" {
14061399 try testFmt("@\"11\\\"23\"", "{z}", .{"11\"23"});
14071400 try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"});
14081401 try testFmt("\\x0f", "{Z}", .{0x0f});
1402 try testFmt(
1403 \\" \\ hi \x07 \x11 \" derp \'"
1404 , "\"{Z}\"", .{" \\ hi \x07 \x11 \" derp '"});
14091405}
14101406
14111407test "pointer" {
lib/std/zig.zig-1
......@@ -11,7 +11,6 @@ pub const Tokenizer = tokenizer.Tokenizer;
1111pub const parse = @import("zig/parse.zig").parse;
1212pub const parseStringLiteral = @import("zig/string_literal.zig").parse;
1313pub const render = @import("zig/render.zig").render;
14pub const renderStringLiteral = @import("zig/string_literal.zig").render;
1514pub const ast = @import("zig/ast.zig");
1615pub const system = @import("zig/system.zig");
1716pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget;
lib/std/zig/string_literal.zig-30
......@@ -127,33 +127,3 @@ test "parse" {
127127 expect(eql(u8, "foo", try parse(alloc, "\"f\x6f\x6f\"", &bad_index)));
128128 expect(eql(u8, "f💯", try parse(alloc, "\"f\u{1f4af}\"", &bad_index)));
129129}
130
131/// Writes a Zig-syntax escaped string literal to the stream. Includes the double quotes.
132pub 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
146test "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 {
350350 val = elem_ptr.array_ptr;
351351 },
352352 .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}),
354355 .repeated => {
355356 try out_stream.writeAll("(repeated) ");
356357 val = val.cast(Payload.Repeated).?.val;
src/zir.zig+4-4
......@@ -1216,17 +1216,17 @@ const Writer = struct {
12161216 try stream.writeByte('}');
12171217 },
12181218 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}),
12201220 BigIntConst, usize => return stream.print("{}", .{param}),
12211221 TypedValue => unreachable, // this is a special case
12221222 *IrModule.Decl => unreachable, // this is a special case
12231223 *Inst.Block => {
12241224 const name = self.block_table.get(param).?;
1225 return std.zig.renderStringLiteral(name, stream);
1225 return stream.print("\"{Z}\"", .{name});
12261226 },
12271227 *Inst.Loop => {
12281228 const name = self.loop_table.get(param).?;
1229 return std.zig.renderStringLiteral(name, stream);
1229 return stream.print("\"{Z}\"", .{name});
12301230 },
12311231 [][]const u8 => {
12321232 try stream.writeByte('[');
......@@ -1234,7 +1234,7 @@ const Writer = struct {
12341234 if (i != 0) {
12351235 try stream.writeAll(", ");
12361236 }
1237 try std.zig.renderStringLiteral(str, stream);
1237 try stream.print("\"{Z}\"", .{str});
12381238 }
12391239 try stream.writeByte(']');
12401240 },