From 2c294676b52f2ba62172fa778a198f92d6a969f0 Mon Sep 17 00:00:00 2001 From: Vexu Date: Tue, 22 Sep 2020 15:28:37 +0300 Subject: [PATCH] use new format specifier in translate-c and std lib --- lib/std/build.zig | 12 ++++----- src/translate_c.zig | 66 +++------------------------------------------ 2 files changed, 9 insertions(+), 69 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 0a12b10db4..4e082ff9ad 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -1767,7 +1767,7 @@ pub const LibExeObjStep = struct { const out = self.build_options_contents.outStream(); switch (T) { []const []const u8 => { - out.print("pub const {}: []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| { out.writeAll(" ") catch unreachable; std.zig.renderStringLiteral(slice, out) catch unreachable; @@ -1777,13 +1777,13 @@ pub const LibExeObjStep = struct { return; }, []const u8 => { - out.print("pub const {}: []const u8 = ", .{name}) catch unreachable; + out.print("pub const {z}: []const u8 = ", .{name}) catch unreachable; std.zig.renderStringLiteral(value, out) catch unreachable; out.writeAll(";\n") catch unreachable; return; }, ?[]const u8 => { - out.print("pub const {}: ?[]const u8 = ", .{name}) catch unreachable; + 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; @@ -1796,15 +1796,15 @@ pub const LibExeObjStep = struct { } switch (@typeInfo(T)) { .Enum => |enum_info| { - out.print("pub const {} = enum {{\n", .{@typeName(T)}) catch unreachable; + out.print("pub const {z} = enum {{\n", .{@typeName(T)}) catch unreachable; inline for (enum_info.fields) |field| { - out.print(" {},\n", .{field.name}) catch unreachable; + out.print(" {z},\n", .{field.name}) catch unreachable; } out.writeAll("};\n") catch unreachable; }, else => {}, } - out.print("pub const {} = {};\n", .{ name, value }) catch unreachable; + out.print("pub const {z} = {};\n", .{ name, value }) catch unreachable; } /// The value is the path in the cache dir. diff --git a/src/translate_c.zig b/src/translate_c.zig index 982467aa8f..1efa50d9fa 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -1972,16 +1972,7 @@ fn transStringLiteral( const bytes_ptr = stmt.getString_bytes_begin_size(&len); const str = bytes_ptr[0..len]; - var char_buf: [4]u8 = undefined; - len = 0; - for (str) |c| len += escapeChar(c, &char_buf).len; - - const buf = try rp.c.arena.alloc(u8, len + "\"\"".len); - buf[0] = '"'; - writeEscapedString(buf[1..], str); - buf[buf.len - 1] = '"'; - - const token = try appendToken(rp.c, .StringLiteral, buf); + const token = try appendTokenFmt(rp.c, .StringLiteral, "\"{Z}\"", .{str}); const node = try rp.c.arena.create(ast.Node.OneToken); node.* = .{ .base = .{ .tag = .StringLiteral }, @@ -1999,41 +1990,6 @@ fn transStringLiteral( } } -fn escapedStringLen(s: []const u8) usize { - var len: usize = 0; - var char_buf: [4]u8 = undefined; - for (s) |c| len += escapeChar(c, &char_buf).len; - return len; -} - -fn writeEscapedString(buf: []u8, s: []const u8) void { - var char_buf: [4]u8 = undefined; - var i: usize = 0; - for (s) |c| { - const escaped = escapeChar(c, &char_buf); - mem.copy(u8, buf[i..], escaped); - i += escaped.len; - } -} - -// Returns either a string literal or a slice of `buf`. -fn escapeChar(c: u8, char_buf: *[4]u8) []const u8 { - return 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)) - std.fmt.bufPrint(char_buf, "\\x{x:0>2}", .{c}) catch unreachable - else - std.fmt.bufPrint(char_buf, "{c}", .{c}) catch unreachable, - }; -} - fn transCCast( rp: RestorePoint, scope: *Scope, @@ -2922,8 +2878,7 @@ fn transCharLiteral( if (val > 255) break :blk try transCreateNodeInt(rp.c, val); } - var char_buf: [4]u8 = undefined; - const token = try appendTokenFmt(rp.c, .CharLiteral, "'{}'", .{escapeChar(@intCast(u8, val), &char_buf)}); + const token = try appendTokenFmt(rp.c, .CharLiteral, "'{Z}'", .{@intCast(u8, val)}); const node = try rp.c.arena.create(ast.Node.OneToken); node.* = .{ .base = .{ .tag = .CharLiteral }, @@ -5247,23 +5202,8 @@ fn isZigPrimitiveType(name: []const u8) bool { mem.eql(u8, name, "c_ulonglong"); } -fn isValidZigIdentifier(name: []const u8) bool { - for (name) |c, i| { - switch (c) { - '_', 'a'...'z', 'A'...'Z' => {}, - '0'...'9' => if (i == 0) return false, - else => return false, - } - } - return true; -} - fn appendIdentifier(c: *Context, name: []const u8) !ast.TokenIndex { - if (!isValidZigIdentifier(name) or std.zig.Token.getKeyword(name) != null) { - return appendTokenFmt(c, .Identifier, "@\"{}\"", .{name}); - } else { - return appendTokenFmt(c, .Identifier, "{}", .{name}); - } + return appendTokenFmt(c, .Identifier, "{z}", .{name}); } fn transCreateNodeIdentifier(c: *Context, name: []const u8) !*ast.Node {