diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-07-10 12:04:27 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-10 12:04:27 +0200 |
| commit | 1a998886c863a1829d649f196093f1058cd9cf13 (patch) | |
| tree | db1b3b0a043a113cfb6544e8103cb64f8e4e4d8f /lib/std/Build/Cache | |
| parent | 5b4b033236a7bed19c90faf7fefbc1990911cfef (diff) | |
| parent | 10d6db5d7d1fb62ee2915a7ad2c7feb771ea3bbb (diff) | |
| download | zig-1a998886c863a1829d649f196093f1058cd9cf13.tar.gz zig-1a998886c863a1829d649f196093f1058cd9cf13.zip | |
Merge pull request #24329 from ziglang/writergate
Deprecates all existing std.io readers and writers in favor of the newly
provided std.io.Reader and std.io.Writer which are non-generic and have the
buffer above the vtable - in other words the buffer is in the interface, not
the implementation. This means that although Reader and Writer are no longer
generic, they are still transparent to optimization; all of the interface
functions have a concrete hot path operating on the buffer, and only make
vtable calls when the buffer is full.
Diffstat (limited to 'lib/std/Build/Cache')
| -rw-r--r-- | lib/std/Build/Cache/DepTokenizer.zig | 201 | ||||
| -rw-r--r-- | lib/std/Build/Cache/Directory.zig | 10 | ||||
| -rw-r--r-- | lib/std/Build/Cache/Path.zig | 73 |
3 files changed, 84 insertions, 200 deletions
diff --git a/lib/std/Build/Cache/DepTokenizer.zig b/lib/std/Build/Cache/DepTokenizer.zig index a1e64c006d..8221f92dba 100644 --- a/lib/std/Build/Cache/DepTokenizer.zig +++ b/lib/std/Build/Cache/DepTokenizer.zig @@ -7,6 +7,7 @@ state: State = .lhs, const std = @import("std"); const testing = std.testing; const assert = std.debug.assert; +const Allocator = std.mem.Allocator; pub fn next(self: *Tokenizer) ?Token { var start = self.index; @@ -362,7 +363,7 @@ pub const Token = union(enum) { }; /// Resolve escapes in target or prereq. Only valid with .target_must_resolve or .prereq_must_resolve. - pub fn resolve(self: Token, writer: anytype) @TypeOf(writer).Error!void { + pub fn resolve(self: Token, gpa: Allocator, list: *std.ArrayListUnmanaged(u8)) error{OutOfMemory}!void { switch (self) { .target_must_resolve => |bytes| { var state: enum { start, escape, dollar } = .start; @@ -372,27 +373,27 @@ pub const Token = union(enum) { switch (c) { '\\' => state = .escape, '$' => state = .dollar, - else => try writer.writeByte(c), + else => try list.append(gpa, c), } }, .escape => { switch (c) { ' ', '#', '\\' => {}, '$' => { - try writer.writeByte('\\'); + try list.append(gpa, '\\'); state = .dollar; continue; }, - else => try writer.writeByte('\\'), + else => try list.append(gpa, '\\'), } - try writer.writeByte(c); + try list.append(gpa, c); state = .start; }, .dollar => { - try writer.writeByte('$'); + try list.append(gpa, '$'); switch (c) { '$' => {}, - else => try writer.writeByte(c), + else => try list.append(gpa, c), } state = .start; }, @@ -406,19 +407,19 @@ pub const Token = union(enum) { .start => { switch (c) { '\\' => state = .escape, - else => try writer.writeByte(c), + else => try list.append(gpa, c), } }, .escape => { switch (c) { ' ' => {}, '\\' => { - try writer.writeByte(c); + try list.append(gpa, c); continue; }, - else => try writer.writeByte('\\'), + else => try list.append(gpa, '\\'), } - try writer.writeByte(c); + try list.append(gpa, c); state = .start; }, } @@ -428,20 +429,20 @@ pub const Token = union(enum) { } } - pub fn printError(self: Token, writer: anytype) @TypeOf(writer).Error!void { + pub fn printError(self: Token, gpa: Allocator, list: *std.ArrayListUnmanaged(u8)) error{OutOfMemory}!void { switch (self) { .target, .target_must_resolve, .prereq, .prereq_must_resolve => unreachable, // not an error .incomplete_quoted_prerequisite, .incomplete_target, => |index_and_bytes| { - try writer.print("{s} '", .{self.errStr()}); + try list.print(gpa, "{s} '", .{self.errStr()}); if (self == .incomplete_target) { const tmp = Token{ .target_must_resolve = index_and_bytes.bytes }; - try tmp.resolve(writer); + try tmp.resolve(gpa, list); } else { - try printCharValues(writer, index_and_bytes.bytes); + try printCharValues(gpa, list, index_and_bytes.bytes); } - try writer.print("' at position {d}", .{index_and_bytes.index}); + try list.print(gpa, "' at position {d}", .{index_and_bytes.index}); }, .invalid_target, .bad_target_escape, @@ -450,9 +451,9 @@ pub const Token = union(enum) { .incomplete_escape, .expected_colon, => |index_and_char| { - try writer.writeAll("illegal char "); - try printUnderstandableChar(writer, index_and_char.char); - try writer.print(" at position {d}: {s}", .{ index_and_char.index, self.errStr() }); + try list.appendSlice(gpa, "illegal char "); + try printUnderstandableChar(gpa, list, index_and_char.char); + try list.print(gpa, " at position {d}: {s}", .{ index_and_char.index, self.errStr() }); }, } } @@ -1026,41 +1027,41 @@ fn depTokenizer(input: []const u8, expect: []const u8) !void { defer arena_allocator.deinit(); var it: Tokenizer = .{ .bytes = input }; - var buffer = std.ArrayList(u8).init(arena); - var resolve_buf = std.ArrayList(u8).init(arena); + var buffer: std.ArrayListUnmanaged(u8) = .empty; + var resolve_buf: std.ArrayListUnmanaged(u8) = .empty; var i: usize = 0; while (it.next()) |token| { - if (i != 0) try buffer.appendSlice("\n"); + if (i != 0) try buffer.appendSlice(arena, "\n"); switch (token) { .target, .prereq => |bytes| { - try buffer.appendSlice(@tagName(token)); - try buffer.appendSlice(" = {"); + try buffer.appendSlice(arena, @tagName(token)); + try buffer.appendSlice(arena, " = {"); for (bytes) |b| { - try buffer.append(printable_char_tab[b]); + try buffer.append(arena, printable_char_tab[b]); } - try buffer.appendSlice("}"); + try buffer.appendSlice(arena, "}"); }, .target_must_resolve => { - try buffer.appendSlice("target = {"); - try token.resolve(resolve_buf.writer()); + try buffer.appendSlice(arena, "target = {"); + try token.resolve(arena, &resolve_buf); for (resolve_buf.items) |b| { - try buffer.append(printable_char_tab[b]); + try buffer.append(arena, printable_char_tab[b]); } resolve_buf.items.len = 0; - try buffer.appendSlice("}"); + try buffer.appendSlice(arena, "}"); }, .prereq_must_resolve => { - try buffer.appendSlice("prereq = {"); - try token.resolve(resolve_buf.writer()); + try buffer.appendSlice(arena, "prereq = {"); + try token.resolve(arena, &resolve_buf); for (resolve_buf.items) |b| { - try buffer.append(printable_char_tab[b]); + try buffer.append(arena, printable_char_tab[b]); } resolve_buf.items.len = 0; - try buffer.appendSlice("}"); + try buffer.appendSlice(arena, "}"); }, else => { - try buffer.appendSlice("ERROR: "); - try token.printError(buffer.writer()); + try buffer.appendSlice(arena, "ERROR: "); + try token.printError(arena, &buffer); break; }, } @@ -1072,134 +1073,18 @@ fn depTokenizer(input: []const u8, expect: []const u8) !void { return; } - const out = std.io.getStdErr().writer(); - - try out.writeAll("\n"); - try printSection(out, "<<<< input", input); - try printSection(out, "==== expect", expect); - try printSection(out, ">>>> got", buffer.items); - try printRuler(out); - - try testing.expect(false); -} - -fn printSection(out: anytype, label: []const u8, bytes: []const u8) !void { - try printLabel(out, label, bytes); - try hexDump(out, bytes); - try printRuler(out); - try out.writeAll(bytes); - try out.writeAll("\n"); -} - -fn printLabel(out: anytype, label: []const u8, bytes: []const u8) !void { - var buf: [80]u8 = undefined; - const text = try std.fmt.bufPrint(buf[0..], "{s} {d} bytes ", .{ label, bytes.len }); - try out.writeAll(text); - var i: usize = text.len; - const end = 79; - while (i < end) : (i += 1) { - try out.writeAll(&[_]u8{label[0]}); - } - try out.writeAll("\n"); -} - -fn printRuler(out: anytype) !void { - var i: usize = 0; - const end = 79; - while (i < end) : (i += 1) { - try out.writeAll("-"); - } - try out.writeAll("\n"); -} - -fn hexDump(out: anytype, bytes: []const u8) !void { - const n16 = bytes.len >> 4; - var line: usize = 0; - var offset: usize = 0; - while (line < n16) : (line += 1) { - try hexDump16(out, offset, bytes[offset..][0..16]); - offset += 16; - } - - const n = bytes.len & 0x0f; - if (n > 0) { - try printDecValue(out, offset, 8); - try out.writeAll(":"); - try out.writeAll(" "); - const end1 = @min(offset + n, offset + 8); - for (bytes[offset..end1]) |b| { - try out.writeAll(" "); - try printHexValue(out, b, 2); - } - const end2 = offset + n; - if (end2 > end1) { - try out.writeAll(" "); - for (bytes[end1..end2]) |b| { - try out.writeAll(" "); - try printHexValue(out, b, 2); - } - } - const short = 16 - n; - var i: usize = 0; - while (i < short) : (i += 1) { - try out.writeAll(" "); - } - if (end2 > end1) { - try out.writeAll(" |"); - } else { - try out.writeAll(" |"); - } - try printCharValues(out, bytes[offset..end2]); - try out.writeAll("|\n"); - offset += n; - } - - try printDecValue(out, offset, 8); - try out.writeAll(":"); - try out.writeAll("\n"); + try testing.expectEqualStrings(expect, buffer.items); } -fn hexDump16(out: anytype, offset: usize, bytes: []const u8) !void { - try printDecValue(out, offset, 8); - try out.writeAll(":"); - try out.writeAll(" "); - for (bytes[0..8]) |b| { - try out.writeAll(" "); - try printHexValue(out, b, 2); - } - try out.writeAll(" "); - for (bytes[8..16]) |b| { - try out.writeAll(" "); - try printHexValue(out, b, 2); - } - try out.writeAll(" |"); - try printCharValues(out, bytes); - try out.writeAll("|\n"); -} - -fn printDecValue(out: anytype, value: u64, width: u8) !void { - var buffer: [20]u8 = undefined; - const len = std.fmt.formatIntBuf(buffer[0..], value, 10, .lower, .{ .width = width, .fill = '0' }); - try out.writeAll(buffer[0..len]); -} - -fn printHexValue(out: anytype, value: u64, width: u8) !void { - var buffer: [16]u8 = undefined; - const len = std.fmt.formatIntBuf(buffer[0..], value, 16, .lower, .{ .width = width, .fill = '0' }); - try out.writeAll(buffer[0..len]); -} - -fn printCharValues(out: anytype, bytes: []const u8) !void { - for (bytes) |b| { - try out.writeAll(&[_]u8{printable_char_tab[b]}); - } +fn printCharValues(gpa: Allocator, list: *std.ArrayListUnmanaged(u8), bytes: []const u8) !void { + for (bytes) |b| try list.append(gpa, printable_char_tab[b]); } -fn printUnderstandableChar(out: anytype, char: u8) !void { +fn printUnderstandableChar(gpa: Allocator, list: *std.ArrayListUnmanaged(u8), char: u8) !void { if (std.ascii.isPrint(char)) { - try out.print("'{c}'", .{char}); + try list.print(gpa, "'{c}'", .{char}); } else { - try out.print("\\x{X:0>2}", .{char}); + try list.print(gpa, "\\x{X:0>2}", .{char}); } } diff --git a/lib/std/Build/Cache/Directory.zig b/lib/std/Build/Cache/Directory.zig index 4de1cc18f1..14a5e8a24d 100644 --- a/lib/std/Build/Cache/Directory.zig +++ b/lib/std/Build/Cache/Directory.zig @@ -1,5 +1,6 @@ const Directory = @This(); const std = @import("../../std.zig"); +const assert = std.debug.assert; const fs = std.fs; const fmt = std.fmt; const Allocator = std.mem.Allocator; @@ -55,14 +56,7 @@ pub fn closeAndFree(self: *Directory, gpa: Allocator) void { self.* = undefined; } -pub fn format( - self: Directory, - comptime fmt_string: []const u8, - options: fmt.FormatOptions, - writer: anytype, -) !void { - _ = options; - if (fmt_string.len != 0) fmt.invalidFmtError(fmt_string, self); +pub fn format(self: Directory, writer: *std.io.Writer) std.io.Writer.Error!void { if (self.path) |p| { try writer.writeAll(p); try writer.writeAll(fs.path.sep_str); diff --git a/lib/std/Build/Cache/Path.zig b/lib/std/Build/Cache/Path.zig index 8822fb64be..a0a58067fc 100644 --- a/lib/std/Build/Cache/Path.zig +++ b/lib/std/Build/Cache/Path.zig @@ -1,3 +1,10 @@ +const Path = @This(); +const std = @import("../../std.zig"); +const assert = std.debug.assert; +const fs = std.fs; +const Allocator = std.mem.Allocator; +const Cache = std.Build.Cache; + root_dir: Cache.Directory, /// The path, relative to the root dir, that this `Path` represents. /// Empty string means the root_dir is the path. @@ -133,38 +140,42 @@ pub fn makePath(p: Path, sub_path: []const u8) !void { } pub fn toString(p: Path, allocator: Allocator) Allocator.Error![]u8 { - return std.fmt.allocPrint(allocator, "{}", .{p}); + return std.fmt.allocPrint(allocator, "{f}", .{p}); } pub fn toStringZ(p: Path, allocator: Allocator) Allocator.Error![:0]u8 { - return std.fmt.allocPrintZ(allocator, "{}", .{p}); -} - -pub fn format( - self: Path, - comptime fmt_string: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, -) !void { - if (fmt_string.len == 1) { - // Quote-escape the string. - const stringEscape = std.zig.stringEscape; - const f = switch (fmt_string[0]) { - 'q' => "", - '\'' => "\'", - else => @compileError("unsupported format string: " ++ fmt_string), - }; - if (self.root_dir.path) |p| { - try stringEscape(p, f, options, writer); - if (self.sub_path.len > 0) try stringEscape(fs.path.sep_str, f, options, writer); - } - if (self.sub_path.len > 0) { - try stringEscape(self.sub_path, f, options, writer); - } - return; + return std.fmt.allocPrintSentinel(allocator, "{f}", .{p}, 0); +} + +pub fn fmtEscapeString(path: Path) std.fmt.Formatter(Path, formatEscapeString) { + return .{ .data = path }; +} + +pub fn formatEscapeString(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void { + if (path.root_dir.path) |p| { + try std.zig.stringEscape(p, writer); + if (path.sub_path.len > 0) try std.zig.stringEscape(fs.path.sep_str, writer); } - if (fmt_string.len > 0) - std.fmt.invalidFmtError(fmt_string, self); + if (path.sub_path.len > 0) { + try std.zig.stringEscape(path.sub_path, writer); + } +} + +pub fn fmtEscapeChar(path: Path) std.fmt.Formatter(Path, formatEscapeChar) { + return .{ .data = path }; +} + +pub fn formatEscapeChar(path: Path, writer: *std.io.Writer) std.io.Writer.Error!void { + if (path.root_dir.path) |p| { + try std.zig.charEscape(p, writer); + if (path.sub_path.len > 0) try std.zig.charEscape(fs.path.sep_str, writer); + } + if (path.sub_path.len > 0) { + try std.zig.charEscape(path.sub_path, writer); + } +} + +pub fn format(self: Path, writer: *std.io.Writer) std.io.Writer.Error!void { if (std.fs.path.isAbsolute(self.sub_path)) { try writer.writeAll(self.sub_path); return; @@ -223,9 +234,3 @@ pub const TableAdapter = struct { return a.eql(b); } }; - -const Path = @This(); -const std = @import("../../std.zig"); -const fs = std.fs; -const Allocator = std.mem.Allocator; -const Cache = std.Build.Cache; |
