Commit e8ca1b254d
lib/std/zig/string_literal.zig
@@ -127,33 +127,3 @@ test "parse" {
expect(eql(u8, "foo", try parse(alloc, "\"f\x6f\x6f\"", &bad_index)));
expect(eql(u8, "f💯", try parse(alloc, "\"f\u{1f4af}\"", &bad_index)));
}
-
-/// Writes a Zig-syntax escaped string literal to the stream. Includes the double quotes.
-pub fn render(utf8: []const u8, out_stream: anytype) !void {
- try out_stream.writeByte('"');
- for (utf8) |byte| switch (byte) {
- '\n' => try out_stream.writeAll("\\n"),
- '\r' => try out_stream.writeAll("\\r"),
- '\t' => try out_stream.writeAll("\\t"),
- '\\' => try out_stream.writeAll("\\\\"),
- '"' => try out_stream.writeAll("\\\""),
- ' ', '!', '#'...'[', ']'...'~' => try out_stream.writeByte(byte),
- else => try out_stream.print("\\x{x:0>2}", .{byte}),
- };
- try out_stream.writeByte('"');
-}
-
-test "render" {
- const expect = std.testing.expect;
- const eql = std.mem.eql;
-
- var fixed_buf_mem: [32]u8 = undefined;
-
- {
- var fbs = std.io.fixedBufferStream(&fixed_buf_mem);
- try render(" \\ hi \x07 \x11 \" derp", fbs.outStream());
- expect(eql(u8,
- \\" \\ hi \x07 \x11 \" derp"
- , fbs.getWritten()));
- }
-}
lib/std/build.zig
@@ -1769,24 +1769,19 @@ pub const LibExeObjStep = struct {
[]const []const u8 => {
out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
for (value) |slice| {
- out.writeAll(" ") catch unreachable;
- std.zig.renderStringLiteral(slice, out) catch unreachable;
- out.writeAll(",\n") catch unreachable;
+ out.print(" \"{Z}\",\n", .{slice}) catch unreachable;
}
out.writeAll("};\n") catch unreachable;
return;
},
[]const u8 => {
- out.print("pub const {z}: []const u8 = ", .{name}) catch unreachable;
- std.zig.renderStringLiteral(value, out) catch unreachable;
- out.writeAll(";\n") catch unreachable;
+ out.print("pub const {z}: []const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable;
return;
},
?[]const u8 => {
out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable;
if (value) |payload| {
- std.zig.renderStringLiteral(payload, out) catch unreachable;
- out.writeAll(";\n") catch unreachable;
+ out.print("\"{Z}\";\n", .{payload}) catch unreachable;
} else {
out.writeAll("null;\n") catch unreachable;
}
@@ -2017,9 +2012,7 @@ pub const LibExeObjStep = struct {
// Render build artifact options at the last minute, now that the path is known.
for (self.build_options_artifact_args.items) |item| {
const out = self.build_options_contents.writer();
- out.print("pub const {}: []const u8 = ", .{item.name}) catch unreachable;
- std.zig.renderStringLiteral(item.artifact.getOutputPath(), out) catch unreachable;
- out.writeAll(";\n") catch unreachable;
+ out.print("pub const {}: []const u8 = \"{Z}\";\n", .{ item.name, item.artifact.getOutputPath() }) catch unreachable;
}
const build_options_file = try fs.path.join(
lib/std/fmt.zig
@@ -552,7 +552,7 @@ pub fn formatIntValue(
} else {
@compileError("Cannot escape character with more than 8 bits");
}
- }else if (comptime std.mem.eql(u8, fmt, "b")) {
+ } else if (comptime std.mem.eql(u8, fmt, "b")) {
radix = 2;
uppercase = false;
} else if (comptime std.mem.eql(u8, fmt, "x")) {
@@ -695,27 +695,20 @@ pub fn formatZigEscapes(
options: FormatOptions,
writer: anytype,
) !void {
- for (bytes) |c| {
- const s: []const u8 = switch (c) {
- '\"' => "\\\"",
- '\'' => "\\'",
- '\\' => "\\\\",
- '\n' => "\\n",
- '\r' => "\\r",
- '\t' => "\\t",
- // Handle the remaining escapes Zig doesn't support by turning them
- // into their respective hex representation
- else => if (std.ascii.isCntrl(c)) {
- try writer.writeAll("\\x");
- try formatInt(c, 16, false, .{ .width = 2, .fill = '0' }, writer);
- continue;
- } else {
- try writer.writeByte(c);
- continue;
- },
- };
- try writer.writeAll(s);
- }
+ for (bytes) |byte| switch (byte) {
+ '\n' => try writer.writeAll("\\n"),
+ '\r' => try writer.writeAll("\\r"),
+ '\t' => try writer.writeAll("\\t"),
+ '\\' => try writer.writeAll("\\\\"),
+ '"' => try writer.writeAll("\\\""),
+ '\'' => try writer.writeAll("\\'"),
+ ' ', '!', '#'...'&', '('...'[', ']'...'~' => try writer.writeByte(byte),
+ // Use hex escapes for rest any unprintable characters.
+ else => {
+ try writer.writeAll("\\x");
+ try formatInt(byte, 16, false, .{ .width = 2, .fill = '0' }, writer);
+ },
+ };
}
/// Print a float in scientific notation to the specified precision. Null uses full precision.
@@ -1406,6 +1399,9 @@ test "escape invalid identifiers" {
try testFmt("@\"11\\\"23\"", "{z}", .{"11\"23"});
try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"});
try testFmt("\\x0f", "{Z}", .{0x0f});
+ try testFmt(
+ \\" \\ hi \x07 \x11 \" derp \'"
+ , "\"{Z}\"", .{" \\ hi \x07 \x11 \" derp '"});
}
test "pointer" {
lib/std/zig.zig
@@ -11,7 +11,6 @@ pub const Tokenizer = tokenizer.Tokenizer;
pub const parse = @import("zig/parse.zig").parse;
pub const parseStringLiteral = @import("zig/string_literal.zig").parse;
pub const render = @import("zig/render.zig").render;
-pub const renderStringLiteral = @import("zig/string_literal.zig").render;
pub const ast = @import("zig/ast.zig");
pub const system = @import("zig/system.zig");
pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget;
src/value.zig
@@ -350,7 +350,8 @@ pub const Value = extern union {
val = elem_ptr.array_ptr;
},
.empty_array => return out_stream.writeAll(".{}"),
- .enum_literal, .bytes => return std.zig.renderStringLiteral(self.cast(Payload.Bytes).?.data, out_stream),
+ .enum_literal => return out_stream.print(".{z}", .{self.cast(Payload.Bytes).?.data}),
+ .bytes => return out_stream.print("\"{Z}\"", .{self.cast(Payload.Bytes).?.data}),
.repeated => {
try out_stream.writeAll("(repeated) ");
val = val.cast(Payload.Repeated).?.val;
src/zir.zig
@@ -1216,17 +1216,17 @@ const Writer = struct {
try stream.writeByte('}');
},
bool => return stream.writeByte("01"[@boolToInt(param)]),
- []u8, []const u8 => return std.zig.renderStringLiteral(param, stream),
+ []u8, []const u8 => return stream.print("\"{Z}\"", .{param}),
BigIntConst, usize => return stream.print("{}", .{param}),
TypedValue => unreachable, // this is a special case
*IrModule.Decl => unreachable, // this is a special case
*Inst.Block => {
const name = self.block_table.get(param).?;
- return std.zig.renderStringLiteral(name, stream);
+ return stream.print("\"{Z}\"", .{name});
},
*Inst.Loop => {
const name = self.loop_table.get(param).?;
- return std.zig.renderStringLiteral(name, stream);
+ return stream.print("\"{Z}\"", .{name});
},
[][]const u8 => {
try stream.writeByte('[');
@@ -1234,7 +1234,7 @@ const Writer = struct {
if (i != 0) {
try stream.writeAll(", ");
}
- try std.zig.renderStringLiteral(str, stream);
+ try stream.print("\"{Z}\"", .{str});
}
try stream.writeByte(']');
},