authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-13 11:55:50-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-13 12:02:58-04:00
log4905102901e7d798860f8346faeae505a7268968
tree49fbf58b5b43ebaffc71eabb7aaa1eb4197044f4
parent2dd920ee394d06b4a215720b6bf7f355dacfd96f
signaturelock-open Commit is signed but in an unrecognized format.

fix all the TODOs from the pull request

* `std.Buffer.print` is removed; use `buffer.outStream().print` * `std.fmt.count` returns a `u64` * `std.Fifo.print` is removed; use `fifo.outStream().print` * `std.fmt.bufPrint` error is renamed from `BufferTooSmall` to `NoSpaceLeft` to match `std.os.write`. * `std.io.FixedBufferStream.getWritten` returns mutable buffer if the buffer is mutable.

9 files changed, 41 insertions(+), 51 deletions(-)

lib/std/buffer.zig+2-6
......@@ -65,7 +65,7 @@ pub const Buffer = struct {
6565 }
6666
6767 pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: var) !Buffer {
68 const size = std.fmt.count(format, args) catch |err| switch (err) {
68 const size = std.math.cast(usize, std.fmt.count(format, args)) catch |err| switch (err) {
6969 error.Overflow => return error.OutOfMemory,
7070 };
7171 var self = try Buffer.initSize(allocator, size);
......@@ -150,10 +150,6 @@ pub const Buffer = struct {
150150 mem.copy(u8, self.list.toSlice(), m);
151151 }
152152
153 pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void {
154 return self.outStream().print(fmt, args);
155 }
156
157153 pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {
158154 return .{ .context = self };
159155 }
......@@ -212,7 +208,7 @@ test "Buffer.print" {
212208 var buf = try Buffer.init(testing.allocator, "");
213209 defer buf.deinit();
214210
215 try buf.print("Hello {} the {}", .{ 2, "world" });
211 try buf.outStream().print("Hello {} the {}", .{ 2, "world" });
216212 testing.expect(buf.eql("Hello 2 the world"));
217213}
218214
lib/std/fifo.zig+12-14
......@@ -293,20 +293,18 @@ pub fn LinearFifo(
293293
294294 pub usingnamespace if (T == u8)
295295 struct {
296 pub fn print(self: *Self, comptime format: []const u8, args: var) !void {
297 // TODO: maybe expose this stream as a method?
298 const FifoStream = struct {
299 const OutStream = std.io.OutStream(*Self, Error, write);
300 const Error = error{OutOfMemory};
301
302 fn write(fifo: *Self, bytes: []const u8) Error!usize {
303 try fifo.write(bytes);
304 return bytes.len;
305 }
306 };
296 const OutStream = std.io.OutStream(*Self, Error, appendWrite);
297 const Error = error{OutOfMemory};
298
299 /// Same as `write` except it returns the number of bytes written, which is always the same
300 /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
301 pub fn appendWrite(fifo: *Self, bytes: []const u8) Error!usize {
302 try fifo.write(bytes);
303 return bytes.len;
304 }
307305
308 var out_stream = FifoStream.OutStream{ .context = self };
309 try out_stream.print(format, args);
306 pub fn outStream(self: *Self) OutStream {
307 return .{ .context = self };
310308 }
311309 }
312310 else
......@@ -419,7 +417,7 @@ test "LinearFifo(u8, .Dynamic)" {
419417 fifo.shrink(0);
420418
421419 {
422 try fifo.print("{}, {}!", .{ "Hello", "World" });
420 try fifo.outStream().print("{}, {}!", .{ "Hello", "World" });
423421 var result: [30]u8 = undefined;
424422 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);
425423 testing.expectEqual(@as(usize, 0), fifo.readableLength());
lib/std/fmt.zig+10-14
......@@ -580,7 +580,7 @@ pub fn formatAsciiChar(
580580 options: FormatOptions,
581581 out_stream: var,
582582) !void {
583 return out_stream.writeAll(@as(*const [1]u8, &c)[0..]);
583 return out_stream.writeAll(@as(*const [1]u8, &c));
584584}
585585
586586pub fn formatBuf(
......@@ -592,9 +592,9 @@ pub fn formatBuf(
592592
593593 const width = options.width orelse 0;
594594 var leftover_padding = if (width > buf.len) (width - buf.len) else return;
595 const pad_byte: u8 = options.fill;
595 const pad_byte = [1]u8{options.fill};
596596 while (leftover_padding > 0) : (leftover_padding -= 1) {
597 try out_stream.writeAll(@as(*const [1]u8, &pad_byte)[0..1]);
597 try out_stream.writeAll(&pad_byte);
598598 }
599599}
600600
......@@ -1068,35 +1068,31 @@ fn digitToChar(digit: u8, uppercase: bool) u8 {
10681068
10691069pub const BufPrintError = error{
10701070 /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
1071 BufferTooSmall,
1071 NoSpaceLeft,
10721072};
10731073pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
10741074 var fbs = std.io.fixedBufferStream(buf);
1075 format(fbs.outStream(), fmt, args) catch |err| switch (err) {
1076 error.NoSpaceLeft => return error.BufferTooSmall,
1077 };
1078 //TODO: should we change one of these return signatures?
1079 //return fbs.getWritten();
1080 return buf[0..fbs.pos];
1075 try format(fbs.outStream(), fmt, args);
1076 return fbs.getWritten();
10811077}
10821078
10831079// Count the characters needed for format. Useful for preallocating memory
1084pub fn count(comptime fmt: []const u8, args: var) !usize {
1080pub fn count(comptime fmt: []const u8, args: var) u64 {
10851081 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
10861082 format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {};
1087 return std.math.cast(usize, counting_stream.bytes_written);
1083 return counting_stream.bytes_written;
10881084}
10891085
10901086pub const AllocPrintError = error{OutOfMemory};
10911087
10921088pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![]u8 {
1093 const size = count(fmt, args) catch |err| switch (err) {
1089 const size = math.cast(usize, count(fmt, args)) catch |err| switch (err) {
10941090 // Output too long. Can't possibly allocate enough memory to display it.
10951091 error.Overflow => return error.OutOfMemory,
10961092 };
10971093 const buf = try allocator.alloc(u8, size);
10981094 return bufPrint(buf, fmt, args) catch |err| switch (err) {
1099 error.BufferTooSmall => unreachable, // we just counted the size above
1095 error.NoSpaceLeft => unreachable, // we just counted the size above
11001096 };
11011097}
11021098
lib/std/io/fixed_buffer_stream.zig+1-1
......@@ -103,7 +103,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
103103 return self.pos;
104104 }
105105
106 pub fn getWritten(self: Self) []const u8 {
106 pub fn getWritten(self: Self) Buffer {
107107 return self.buffer[0..self.pos];
108108 }
109109
lib/std/progress.zig+1-1
......@@ -190,7 +190,7 @@ pub const Progress = struct {
190190 end.* += amt;
191191 self.columns_written += amt;
192192 } else |err| switch (err) {
193 error.BufferTooSmall => {
193 error.NoSpaceLeft => {
194194 self.columns_written += self.output_buffer.len - end.*;
195195 end.* = self.output_buffer.len;
196196 },
lib/std/zig/cross_target.zig+6-6
......@@ -504,22 +504,22 @@ pub const CrossTarget = struct {
504504 if (self.os_version_min != null or self.os_version_max != null) {
505505 switch (self.getOsVersionMin()) {
506506 .none => {},
507 .semver => |v| try result.print(".{}", .{v}),
508 .windows => |v| try result.print(".{}", .{@tagName(v)}),
507 .semver => |v| try result.outStream().print(".{}", .{v}),
508 .windows => |v| try result.outStream().print(".{}", .{@tagName(v)}),
509509 }
510510 }
511511 if (self.os_version_max) |max| {
512512 switch (max) {
513513 .none => {},
514 .semver => |v| try result.print("...{}", .{v}),
515 .windows => |v| try result.print("...{}", .{@tagName(v)}),
514 .semver => |v| try result.outStream().print("...{}", .{v}),
515 .windows => |v| try result.outStream().print("...{}", .{@tagName(v)}),
516516 }
517517 }
518518
519519 if (self.glibc_version) |v| {
520 try result.print("-{}.{}", .{ @tagName(self.getAbi()), v });
520 try result.outStream().print("-{}.{}", .{ @tagName(self.getAbi()), v });
521521 } else if (self.abi) |abi| {
522 try result.print("-{}", .{@tagName(abi)});
522 try result.outStream().print("-{}", .{@tagName(abi)});
523523 }
524524
525525 return result.toOwnedSlice();
src-self-hosted/dep_tokenizer.zig+5-5
......@@ -306,12 +306,12 @@ pub const Tokenizer = struct {
306306
307307 fn errorPosition(self: *Tokenizer, position: usize, bytes: []const u8, comptime fmt: []const u8, args: var) Error {
308308 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
309 try buffer.print(fmt, args);
309 try buffer.outStream().print(fmt, args);
310310 try buffer.append(" '");
311311 var out = makeOutput(std.Buffer.append, &buffer);
312312 try printCharValues(&out, bytes);
313313 try buffer.append("'");
314 try buffer.print(" at position {}", .{position - (bytes.len - 1)});
314 try buffer.outStream().print(" at position {}", .{position - (bytes.len - 1)});
315315 self.error_text = buffer.toSlice();
316316 return Error.InvalidInput;
317317 }
......@@ -320,8 +320,8 @@ pub const Tokenizer = struct {
320320 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
321321 try buffer.append("illegal char ");
322322 try printUnderstandableChar(&buffer, char);
323 try buffer.print(" at position {}", .{position});
324 if (fmt.len != 0) try buffer.print(": " ++ fmt, args);
323 try buffer.outStream().print(" at position {}", .{position});
324 if (fmt.len != 0) try buffer.outStream().print(": " ++ fmt, args);
325325 self.error_text = buffer.toSlice();
326326 return Error.InvalidInput;
327327 }
......@@ -997,7 +997,7 @@ fn printCharValues(out: var, bytes: []const u8) !void {
997997
998998fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void {
999999 if (!std.ascii.isPrint(char) or char == ' ') {
1000 try buffer.print("\\x{X:2}", .{char});
1000 try buffer.outStream().print("\\x{X:2}", .{char});
10011001 } else {
10021002 try buffer.append("'");
10031003 try buffer.appendByte(printable_char_tab[char]);
src-self-hosted/stage2.zig+3-3
......@@ -1019,7 +1019,7 @@ const Stage2Target = extern struct {
10191019 .macosx,
10201020 .netbsd,
10211021 .openbsd,
1022 => try os_builtin_str_buffer.print(
1022 => try os_builtin_str_buffer.outStream().print(
10231023 \\ .semver = .{{
10241024 \\ .min = .{{
10251025 \\ .major = {},
......@@ -1043,7 +1043,7 @@ const Stage2Target = extern struct {
10431043 target.os.version_range.semver.max.patch,
10441044 }),
10451045
1046 .linux => try os_builtin_str_buffer.print(
1046 .linux => try os_builtin_str_buffer.outStream().print(
10471047 \\ .linux = .{{
10481048 \\ .range = .{{
10491049 \\ .min = .{{
......@@ -1078,7 +1078,7 @@ const Stage2Target = extern struct {
10781078 target.os.version_range.linux.glibc.patch,
10791079 }),
10801080
1081 .windows => try os_builtin_str_buffer.print(
1081 .windows => try os_builtin_str_buffer.outStream().print(
10821082 \\ .windows = .{{
10831083 \\ .min = .{},
10841084 \\ .max = .{},
src-self-hosted/translate_c.zig+1-1
......@@ -4755,7 +4755,7 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8,
47554755 const start_index = c.source_buffer.len();
47564756 errdefer c.source_buffer.shrink(start_index);
47574757
4758 try c.source_buffer.print(format, args);
4758 try c.source_buffer.outStream().print(format, args);
47594759 const end_index = c.source_buffer.len();
47604760 const token_index = c.tree.tokens.len;
47614761 const new_token = try c.tree.tokens.addOne();