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 {...@@ -65,7 +65,7 @@ pub const Buffer = struct {
65 }65 }
6666
67 pub fn allocPrint(allocator: *Allocator, comptime format: []const u8, args: var) !Buffer {67 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) {
69 error.Overflow => return error.OutOfMemory,69 error.Overflow => return error.OutOfMemory,
70 };70 };
71 var self = try Buffer.initSize(allocator, size);71 var self = try Buffer.initSize(allocator, size);
...@@ -150,10 +150,6 @@ pub const Buffer = struct {...@@ -150,10 +150,6 @@ pub const Buffer = struct {
150 mem.copy(u8, self.list.toSlice(), m);150 mem.copy(u8, self.list.toSlice(), m);
151 }151 }
152152
153 pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void {
154 return self.outStream().print(fmt, args);
155 }
156
157 pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {153 pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) {
158 return .{ .context = self };154 return .{ .context = self };
159 }155 }
...@@ -212,7 +208,7 @@ test "Buffer.print" {...@@ -212,7 +208,7 @@ test "Buffer.print" {
212 var buf = try Buffer.init(testing.allocator, "");208 var buf = try Buffer.init(testing.allocator, "");
213 defer buf.deinit();209 defer buf.deinit();
214210
215 try buf.print("Hello {} the {}", .{ 2, "world" });211 try buf.outStream().print("Hello {} the {}", .{ 2, "world" });
216 testing.expect(buf.eql("Hello 2 the world"));212 testing.expect(buf.eql("Hello 2 the world"));
217}213}
218214
lib/std/fifo.zig+12-14
...@@ -293,20 +293,18 @@ pub fn LinearFifo(...@@ -293,20 +293,18 @@ pub fn LinearFifo(
293293
294 pub usingnamespace if (T == u8)294 pub usingnamespace if (T == u8)
295 struct {295 struct {
296 pub fn print(self: *Self, comptime format: []const u8, args: var) !void {296 const OutStream = std.io.OutStream(*Self, Error, appendWrite);
297 // TODO: maybe expose this stream as a method?297 const Error = error{OutOfMemory};
298 const FifoStream = struct {298
299 const OutStream = std.io.OutStream(*Self, Error, write);299 /// Same as `write` except it returns the number of bytes written, which is always the same
300 const Error = error{OutOfMemory};300 /// as `bytes.len`. The purpose of this function existing is to match `std.io.OutStream` API.
301301 pub fn appendWrite(fifo: *Self, bytes: []const u8) Error!usize {
302 fn write(fifo: *Self, bytes: []const u8) Error!usize {302 try fifo.write(bytes);
303 try fifo.write(bytes);303 return bytes.len;
304 return bytes.len;304 }
305 }
306 };
307305
308 var out_stream = FifoStream.OutStream{ .context = self };306 pub fn outStream(self: *Self) OutStream {
309 try out_stream.print(format, args);307 return .{ .context = self };
310 }308 }
311 }309 }
312 else310 else
...@@ -419,7 +417,7 @@ test "LinearFifo(u8, .Dynamic)" {...@@ -419,7 +417,7 @@ test "LinearFifo(u8, .Dynamic)" {
419 fifo.shrink(0);417 fifo.shrink(0);
420418
421 {419 {
422 try fifo.print("{}, {}!", .{ "Hello", "World" });420 try fifo.outStream().print("{}, {}!", .{ "Hello", "World" });
423 var result: [30]u8 = undefined;421 var result: [30]u8 = undefined;
424 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);422 testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]);
425 testing.expectEqual(@as(usize, 0), fifo.readableLength());423 testing.expectEqual(@as(usize, 0), fifo.readableLength());
lib/std/fmt.zig+10-14
...@@ -580,7 +580,7 @@ pub fn formatAsciiChar(...@@ -580,7 +580,7 @@ pub fn formatAsciiChar(
580 options: FormatOptions,580 options: FormatOptions,
581 out_stream: var,581 out_stream: var,
582) !void {582) !void {
583 return out_stream.writeAll(@as(*const [1]u8, &c)[0..]);583 return out_stream.writeAll(@as(*const [1]u8, &c));
584}584}
585585
586pub fn formatBuf(586pub fn formatBuf(
...@@ -592,9 +592,9 @@ pub fn formatBuf(...@@ -592,9 +592,9 @@ pub fn formatBuf(
592592
593 const width = options.width orelse 0;593 const width = options.width orelse 0;
594 var leftover_padding = if (width > buf.len) (width - buf.len) else return;594 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};
596 while (leftover_padding > 0) : (leftover_padding -= 1) {596 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);
598 }598 }
599}599}
600600
...@@ -1068,35 +1068,31 @@ fn digitToChar(digit: u8, uppercase: bool) u8 {...@@ -1068,35 +1068,31 @@ fn digitToChar(digit: u8, uppercase: bool) u8 {
10681068
1069pub const BufPrintError = error{1069pub const BufPrintError = error{
1070 /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.1070 /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
1071 BufferTooSmall,1071 NoSpaceLeft,
1072};1072};
1073pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {1073pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
1074 var fbs = std.io.fixedBufferStream(buf);1074 var fbs = std.io.fixedBufferStream(buf);
1075 format(fbs.outStream(), fmt, args) catch |err| switch (err) {1075 try format(fbs.outStream(), fmt, args);
1076 error.NoSpaceLeft => return error.BufferTooSmall,1076 return fbs.getWritten();
1077 };
1078 //TODO: should we change one of these return signatures?
1079 //return fbs.getWritten();
1080 return buf[0..fbs.pos];
1081}1077}
10821078
1083// Count the characters needed for format. Useful for preallocating memory1079// 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 {
1085 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);1081 var counting_stream = std.io.countingOutStream(std.io.null_out_stream);
1086 format(counting_stream.outStream(), fmt, args) catch |err| switch (err) {};1082 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;
1088}1084}
10891085
1090pub const AllocPrintError = error{OutOfMemory};1086pub const AllocPrintError = error{OutOfMemory};
10911087
1092pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: var) AllocPrintError![]u8 {1088pub 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) {
1094 // Output too long. Can't possibly allocate enough memory to display it.1090 // Output too long. Can't possibly allocate enough memory to display it.
1095 error.Overflow => return error.OutOfMemory,1091 error.Overflow => return error.OutOfMemory,
1096 };1092 };
1097 const buf = try allocator.alloc(u8, size);1093 const buf = try allocator.alloc(u8, size);
1098 return bufPrint(buf, fmt, args) catch |err| switch (err) {1094 return bufPrint(buf, fmt, args) catch |err| switch (err) {
1099 error.BufferTooSmall => unreachable, // we just counted the size above1095 error.NoSpaceLeft => unreachable, // we just counted the size above
1100 };1096 };
1101}1097}
11021098
lib/std/io/fixed_buffer_stream.zig+1-1
...@@ -103,7 +103,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type {...@@ -103,7 +103,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type {
103 return self.pos;103 return self.pos;
104 }104 }
105105
106 pub fn getWritten(self: Self) []const u8 {106 pub fn getWritten(self: Self) Buffer {
107 return self.buffer[0..self.pos];107 return self.buffer[0..self.pos];
108 }108 }
109109
lib/std/progress.zig+1-1
...@@ -190,7 +190,7 @@ pub const Progress = struct {...@@ -190,7 +190,7 @@ pub const Progress = struct {
190 end.* += amt;190 end.* += amt;
191 self.columns_written += amt;191 self.columns_written += amt;
192 } else |err| switch (err) {192 } else |err| switch (err) {
193 error.BufferTooSmall => {193 error.NoSpaceLeft => {
194 self.columns_written += self.output_buffer.len - end.*;194 self.columns_written += self.output_buffer.len - end.*;
195 end.* = self.output_buffer.len;195 end.* = self.output_buffer.len;
196 },196 },
lib/std/zig/cross_target.zig+6-6
...@@ -504,22 +504,22 @@ pub const CrossTarget = struct {...@@ -504,22 +504,22 @@ pub const CrossTarget = struct {
504 if (self.os_version_min != null or self.os_version_max != null) {504 if (self.os_version_min != null or self.os_version_max != null) {
505 switch (self.getOsVersionMin()) {505 switch (self.getOsVersionMin()) {
506 .none => {},506 .none => {},
507 .semver => |v| try result.print(".{}", .{v}),507 .semver => |v| try result.outStream().print(".{}", .{v}),
508 .windows => |v| try result.print(".{}", .{@tagName(v)}),508 .windows => |v| try result.outStream().print(".{}", .{@tagName(v)}),
509 }509 }
510 }510 }
511 if (self.os_version_max) |max| {511 if (self.os_version_max) |max| {
512 switch (max) {512 switch (max) {
513 .none => {},513 .none => {},
514 .semver => |v| try result.print("...{}", .{v}),514 .semver => |v| try result.outStream().print("...{}", .{v}),
515 .windows => |v| try result.print("...{}", .{@tagName(v)}),515 .windows => |v| try result.outStream().print("...{}", .{@tagName(v)}),
516 }516 }
517 }517 }
518518
519 if (self.glibc_version) |v| {519 if (self.glibc_version) |v| {
520 try result.print("-{}.{}", .{ @tagName(self.getAbi()), v });520 try result.outStream().print("-{}.{}", .{ @tagName(self.getAbi()), v });
521 } else if (self.abi) |abi| {521 } else if (self.abi) |abi| {
522 try result.print("-{}", .{@tagName(abi)});522 try result.outStream().print("-{}", .{@tagName(abi)});
523 }523 }
524524
525 return result.toOwnedSlice();525 return result.toOwnedSlice();
src-self-hosted/dep_tokenizer.zig+5-5
...@@ -306,12 +306,12 @@ pub const Tokenizer = struct {...@@ -306,12 +306,12 @@ pub const Tokenizer = struct {
306306
307 fn errorPosition(self: *Tokenizer, position: usize, bytes: []const u8, comptime fmt: []const u8, args: var) Error {307 fn errorPosition(self: *Tokenizer, position: usize, bytes: []const u8, comptime fmt: []const u8, args: var) Error {
308 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);308 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
309 try buffer.print(fmt, args);309 try buffer.outStream().print(fmt, args);
310 try buffer.append(" '");310 try buffer.append(" '");
311 var out = makeOutput(std.Buffer.append, &buffer);311 var out = makeOutput(std.Buffer.append, &buffer);
312 try printCharValues(&out, bytes);312 try printCharValues(&out, bytes);
313 try buffer.append("'");313 try buffer.append("'");
314 try buffer.print(" at position {}", .{position - (bytes.len - 1)});314 try buffer.outStream().print(" at position {}", .{position - (bytes.len - 1)});
315 self.error_text = buffer.toSlice();315 self.error_text = buffer.toSlice();
316 return Error.InvalidInput;316 return Error.InvalidInput;
317 }317 }
...@@ -320,8 +320,8 @@ pub const Tokenizer = struct {...@@ -320,8 +320,8 @@ pub const Tokenizer = struct {
320 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);320 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
321 try buffer.append("illegal char ");321 try buffer.append("illegal char ");
322 try printUnderstandableChar(&buffer, char);322 try printUnderstandableChar(&buffer, char);
323 try buffer.print(" at position {}", .{position});323 try buffer.outStream().print(" at position {}", .{position});
324 if (fmt.len != 0) try buffer.print(": " ++ fmt, args);324 if (fmt.len != 0) try buffer.outStream().print(": " ++ fmt, args);
325 self.error_text = buffer.toSlice();325 self.error_text = buffer.toSlice();
326 return Error.InvalidInput;326 return Error.InvalidInput;
327 }327 }
...@@ -997,7 +997,7 @@ fn printCharValues(out: var, bytes: []const u8) !void {...@@ -997,7 +997,7 @@ fn printCharValues(out: var, bytes: []const u8) !void {
997997
998fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void {998fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void {
999 if (!std.ascii.isPrint(char) or char == ' ') {999 if (!std.ascii.isPrint(char) or char == ' ') {
1000 try buffer.print("\\x{X:2}", .{char});1000 try buffer.outStream().print("\\x{X:2}", .{char});
1001 } else {1001 } else {
1002 try buffer.append("'");1002 try buffer.append("'");
1003 try buffer.appendByte(printable_char_tab[char]);1003 try buffer.appendByte(printable_char_tab[char]);
src-self-hosted/stage2.zig+3-3
...@@ -1019,7 +1019,7 @@ const Stage2Target = extern struct {...@@ -1019,7 +1019,7 @@ const Stage2Target = extern struct {
1019 .macosx,1019 .macosx,
1020 .netbsd,1020 .netbsd,
1021 .openbsd,1021 .openbsd,
1022 => try os_builtin_str_buffer.print(1022 => try os_builtin_str_buffer.outStream().print(
1023 \\ .semver = .{{1023 \\ .semver = .{{
1024 \\ .min = .{{1024 \\ .min = .{{
1025 \\ .major = {},1025 \\ .major = {},
...@@ -1043,7 +1043,7 @@ const Stage2Target = extern struct {...@@ -1043,7 +1043,7 @@ const Stage2Target = extern struct {
1043 target.os.version_range.semver.max.patch,1043 target.os.version_range.semver.max.patch,
1044 }),1044 }),
10451045
1046 .linux => try os_builtin_str_buffer.print(1046 .linux => try os_builtin_str_buffer.outStream().print(
1047 \\ .linux = .{{1047 \\ .linux = .{{
1048 \\ .range = .{{1048 \\ .range = .{{
1049 \\ .min = .{{1049 \\ .min = .{{
...@@ -1078,7 +1078,7 @@ const Stage2Target = extern struct {...@@ -1078,7 +1078,7 @@ const Stage2Target = extern struct {
1078 target.os.version_range.linux.glibc.patch,1078 target.os.version_range.linux.glibc.patch,
1079 }),1079 }),
10801080
1081 .windows => try os_builtin_str_buffer.print(1081 .windows => try os_builtin_str_buffer.outStream().print(
1082 \\ .windows = .{{1082 \\ .windows = .{{
1083 \\ .min = .{},1083 \\ .min = .{},
1084 \\ .max = .{},1084 \\ .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,...@@ -4755,7 +4755,7 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8,
4755 const start_index = c.source_buffer.len();4755 const start_index = c.source_buffer.len();
4756 errdefer c.source_buffer.shrink(start_index);4756 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);
4759 const end_index = c.source_buffer.len();4759 const end_index = c.source_buffer.len();
4760 const token_index = c.tree.tokens.len;4760 const token_index = c.tree.tokens.len;
4761 const new_token = try c.tree.tokens.addOne();4761 const new_token = try c.tree.tokens.addOne();