std: remove renderStringLiteral in favor of std.fmt specifier

This commit is contained in:
Vexu 2020-10-17 17:12:58 +03:00
parent 2c294676b5
commit e8ca1b254d
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
6 changed files with 28 additions and 69 deletions

View File

@ -1769,24 +1769,19 @@ pub const LibExeObjStep = struct {
[]const []const u8 => { []const []const u8 => {
out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable; out.print("pub const {z}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
for (value) |slice| { for (value) |slice| {
out.writeAll(" ") catch unreachable; out.print(" \"{Z}\",\n", .{slice}) catch unreachable;
std.zig.renderStringLiteral(slice, out) catch unreachable;
out.writeAll(",\n") catch unreachable;
} }
out.writeAll("};\n") catch unreachable; out.writeAll("};\n") catch unreachable;
return; return;
}, },
[]const u8 => { []const u8 => {
out.print("pub const {z}: []const u8 = ", .{name}) catch unreachable; out.print("pub const {z}: []const u8 = \"{Z}\";\n", .{ name, value }) catch unreachable;
std.zig.renderStringLiteral(value, out) catch unreachable;
out.writeAll(";\n") catch unreachable;
return; return;
}, },
?[]const u8 => { ?[]const u8 => {
out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable; out.print("pub const {z}: ?[]const u8 = ", .{name}) catch unreachable;
if (value) |payload| { if (value) |payload| {
std.zig.renderStringLiteral(payload, out) catch unreachable; out.print("\"{Z}\";\n", .{payload}) catch unreachable;
out.writeAll(";\n") catch unreachable;
} else { } else {
out.writeAll("null;\n") catch unreachable; 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. // Render build artifact options at the last minute, now that the path is known.
for (self.build_options_artifact_args.items) |item| { for (self.build_options_artifact_args.items) |item| {
const out = self.build_options_contents.writer(); const out = self.build_options_contents.writer();
out.print("pub const {}: []const u8 = ", .{item.name}) catch unreachable; out.print("pub const {}: []const u8 = \"{Z}\";\n", .{ item.name, item.artifact.getOutputPath() }) catch unreachable;
std.zig.renderStringLiteral(item.artifact.getOutputPath(), out) catch unreachable;
out.writeAll(";\n") catch unreachable;
} }
const build_options_file = try fs.path.join( const build_options_file = try fs.path.join(

View File

@ -552,7 +552,7 @@ pub fn formatIntValue(
} else { } else {
@compileError("Cannot escape character with more than 8 bits"); @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; radix = 2;
uppercase = false; uppercase = false;
} else if (comptime std.mem.eql(u8, fmt, "x")) { } else if (comptime std.mem.eql(u8, fmt, "x")) {
@ -695,27 +695,20 @@ pub fn formatZigEscapes(
options: FormatOptions, options: FormatOptions,
writer: anytype, writer: anytype,
) !void { ) !void {
for (bytes) |c| { for (bytes) |byte| switch (byte) {
const s: []const u8 = switch (c) { '\n' => try writer.writeAll("\\n"),
'\"' => "\\\"", '\r' => try writer.writeAll("\\r"),
'\'' => "\\'", '\t' => try writer.writeAll("\\t"),
'\\' => "\\\\", '\\' => try writer.writeAll("\\\\"),
'\n' => "\\n", '"' => try writer.writeAll("\\\""),
'\r' => "\\r", '\'' => try writer.writeAll("\\'"),
'\t' => "\\t", ' ', '!', '#'...'&', '('...'[', ']'...'~' => try writer.writeByte(byte),
// Handle the remaining escapes Zig doesn't support by turning them // Use hex escapes for rest any unprintable characters.
// into their respective hex representation else => {
else => if (std.ascii.isCntrl(c)) {
try writer.writeAll("\\x"); try writer.writeAll("\\x");
try formatInt(c, 16, false, .{ .width = 2, .fill = '0' }, writer); try formatInt(byte, 16, false, .{ .width = 2, .fill = '0' }, writer);
continue;
} else {
try writer.writeByte(c);
continue;
}, },
}; };
try writer.writeAll(s);
}
} }
/// Print a float in scientific notation to the specified precision. Null uses full precision. /// 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\\\"23\"", "{z}", .{"11\"23"});
try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"}); try testFmt("@\"11\\x0f23\"", "{z}", .{"11\x0F23"});
try testFmt("\\x0f", "{Z}", .{0x0f}); try testFmt("\\x0f", "{Z}", .{0x0f});
try testFmt(
\\" \\ hi \x07 \x11 \" derp \'"
, "\"{Z}\"", .{" \\ hi \x07 \x11 \" derp '"});
} }
test "pointer" { test "pointer" {

View File

@ -11,7 +11,6 @@ pub const Tokenizer = tokenizer.Tokenizer;
pub const parse = @import("zig/parse.zig").parse; pub const parse = @import("zig/parse.zig").parse;
pub const parseStringLiteral = @import("zig/string_literal.zig").parse; pub const parseStringLiteral = @import("zig/string_literal.zig").parse;
pub const render = @import("zig/render.zig").render; 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 ast = @import("zig/ast.zig");
pub const system = @import("zig/system.zig"); pub const system = @import("zig/system.zig");
pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget; pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget;

View File

@ -127,33 +127,3 @@ test "parse" {
expect(eql(u8, "foo", try parse(alloc, "\"f\x6f\x6f\"", &bad_index))); expect(eql(u8, "foo", try parse(alloc, "\"f\x6f\x6f\"", &bad_index)));
expect(eql(u8, "f💯", try parse(alloc, "\"f\u{1f4af}\"", &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()));
}
}

View File

@ -350,7 +350,8 @@ pub const Value = extern union {
val = elem_ptr.array_ptr; val = elem_ptr.array_ptr;
}, },
.empty_array => return out_stream.writeAll(".{}"), .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 => { .repeated => {
try out_stream.writeAll("(repeated) "); try out_stream.writeAll("(repeated) ");
val = val.cast(Payload.Repeated).?.val; val = val.cast(Payload.Repeated).?.val;

View File

@ -1216,17 +1216,17 @@ const Writer = struct {
try stream.writeByte('}'); try stream.writeByte('}');
}, },
bool => return stream.writeByte("01"[@boolToInt(param)]), 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}), BigIntConst, usize => return stream.print("{}", .{param}),
TypedValue => unreachable, // this is a special case TypedValue => unreachable, // this is a special case
*IrModule.Decl => unreachable, // this is a special case *IrModule.Decl => unreachable, // this is a special case
*Inst.Block => { *Inst.Block => {
const name = self.block_table.get(param).?; const name = self.block_table.get(param).?;
return std.zig.renderStringLiteral(name, stream); return stream.print("\"{Z}\"", .{name});
}, },
*Inst.Loop => { *Inst.Loop => {
const name = self.loop_table.get(param).?; const name = self.loop_table.get(param).?;
return std.zig.renderStringLiteral(name, stream); return stream.print("\"{Z}\"", .{name});
}, },
[][]const u8 => { [][]const u8 => {
try stream.writeByte('['); try stream.writeByte('[');
@ -1234,7 +1234,7 @@ const Writer = struct {
if (i != 0) { if (i != 0) {
try stream.writeAll(", "); try stream.writeAll(", ");
} }
try std.zig.renderStringLiteral(str, stream); try stream.print("\"{Z}\"", .{str});
} }
try stream.writeByte(']'); try stream.writeByte(']');
}, },