| author | |
| committer | |
| log | 4905102901e7d798860f8346faeae505a7268968 |
| tree | 49fbf58b5b43ebaffc71eabb7aaa1eb4197044f4 |
| parent | 2dd920ee394d06b4a215720b6bf7f355dacfd96f |
| signature |
* `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 | 65 | } |
| 66 | 66 | |
| 67 | 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 | 69 | error.Overflow => return error.OutOfMemory, |
| 70 | 70 | }; |
| 71 | 71 | var self = try Buffer.initSize(allocator, size); |
| ... | ... | @@ -150,10 +150,6 @@ pub const Buffer = struct { |
| 150 | 150 | mem.copy(u8, self.list.toSlice(), m); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | pub fn print(self: *Buffer, comptime fmt: []const u8, args: var) !void { | |
| 154 | return self.outStream().print(fmt, args); | |
| 155 | } | |
| 156 | ||
| 157 | 153 | pub fn outStream(self: *Buffer) std.io.OutStream(*Buffer, error{OutOfMemory}, appendWrite) { |
| 158 | 154 | return .{ .context = self }; |
| 159 | 155 | } |
| ... | ... | @@ -212,7 +208,7 @@ test "Buffer.print" { |
| 212 | 208 | var buf = try Buffer.init(testing.allocator, ""); |
| 213 | 209 | defer buf.deinit(); |
| 214 | 210 | |
| 215 | try buf.print("Hello {} the {}", .{ 2, "world" }); | |
| 211 | try buf.outStream().print("Hello {} the {}", .{ 2, "world" }); | |
| 216 | 212 | testing.expect(buf.eql("Hello 2 the world")); |
| 217 | 213 | } |
| 218 | 214 |
lib/std/fifo.zig+12-14| ... | ... | @@ -293,20 +293,18 @@ pub fn LinearFifo( |
| 293 | 293 | |
| 294 | 294 | pub usingnamespace if (T == u8) |
| 295 | 295 | 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 | } | |
| 307 | 305 | |
| 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 }; | |
| 310 | 308 | } |
| 311 | 309 | } |
| 312 | 310 | else |
| ... | ... | @@ -419,7 +417,7 @@ test "LinearFifo(u8, .Dynamic)" { |
| 419 | 417 | fifo.shrink(0); |
| 420 | 418 | |
| 421 | 419 | { |
| 422 | try fifo.print("{}, {}!", .{ "Hello", "World" }); | |
| 420 | try fifo.outStream().print("{}, {}!", .{ "Hello", "World" }); | |
| 423 | 421 | var result: [30]u8 = undefined; |
| 424 | 422 | testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]); |
| 425 | 423 | testing.expectEqual(@as(usize, 0), fifo.readableLength()); |
lib/std/fmt.zig+10-14| ... | ... | @@ -580,7 +580,7 @@ pub fn formatAsciiChar( |
| 580 | 580 | options: FormatOptions, |
| 581 | 581 | out_stream: var, |
| 582 | 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 | } |
| 585 | 585 | |
| 586 | 586 | pub fn formatBuf( |
| ... | ... | @@ -592,9 +592,9 @@ pub fn formatBuf( |
| 592 | 592 | |
| 593 | 593 | const width = options.width orelse 0; |
| 594 | 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 | 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 | } |
| 600 | 600 | |
| ... | ... | @@ -1068,35 +1068,31 @@ fn digitToChar(digit: u8, uppercase: bool) u8 { |
| 1068 | 1068 | |
| 1069 | 1069 | pub const BufPrintError = error{ |
| 1070 | 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 | }; |
| 1073 | 1073 | pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { |
| 1074 | 1074 | 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(); | |
| 1081 | 1077 | } |
| 1082 | 1078 | |
| 1083 | 1079 | // Count the characters needed for format. Useful for preallocating memory |
| 1084 | pub fn count(comptime fmt: []const u8, args: var) !usize { | |
| 1080 | pub fn count(comptime fmt: []const u8, args: var) u64 { | |
| 1085 | 1081 | var counting_stream = std.io.countingOutStream(std.io.null_out_stream); |
| 1086 | 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 | } |
| 1089 | 1085 | |
| 1090 | 1086 | pub const AllocPrintError = error{OutOfMemory}; |
| 1091 | 1087 | |
| 1092 | 1088 | pub 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 | 1090 | // Output too long. Can't possibly allocate enough memory to display it. |
| 1095 | 1091 | error.Overflow => return error.OutOfMemory, |
| 1096 | 1092 | }; |
| 1097 | 1093 | const buf = try allocator.alloc(u8, size); |
| 1098 | 1094 | 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 | |
| 1100 | 1096 | }; |
| 1101 | 1097 | } |
| 1102 | 1098 |
lib/std/io/fixed_buffer_stream.zig+1-1| ... | ... | @@ -103,7 +103,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type { |
| 103 | 103 | return self.pos; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | pub fn getWritten(self: Self) []const u8 { | |
| 106 | pub fn getWritten(self: Self) Buffer { | |
| 107 | 107 | return self.buffer[0..self.pos]; |
| 108 | 108 | } |
| 109 | 109 |
lib/std/progress.zig+1-1| ... | ... | @@ -190,7 +190,7 @@ pub const Progress = struct { |
| 190 | 190 | end.* += amt; |
| 191 | 191 | self.columns_written += amt; |
| 192 | 192 | } else |err| switch (err) { |
| 193 | error.BufferTooSmall => { | |
| 193 | error.NoSpaceLeft => { | |
| 194 | 194 | self.columns_written += self.output_buffer.len - end.*; |
| 195 | 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 | 504 | if (self.os_version_min != null or self.os_version_max != null) { |
| 505 | 505 | switch (self.getOsVersionMin()) { |
| 506 | 506 | .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)}), | |
| 509 | 509 | } |
| 510 | 510 | } |
| 511 | 511 | if (self.os_version_max) |max| { |
| 512 | 512 | switch (max) { |
| 513 | 513 | .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)}), | |
| 516 | 516 | } |
| 517 | 517 | } |
| 518 | 518 | |
| 519 | 519 | if (self.glibc_version) |v| { |
| 520 | try result.print("-{}.{}", .{ @tagName(self.getAbi()), v }); | |
| 520 | try result.outStream().print("-{}.{}", .{ @tagName(self.getAbi()), v }); | |
| 521 | 521 | } else if (self.abi) |abi| { |
| 522 | try result.print("-{}", .{@tagName(abi)}); | |
| 522 | try result.outStream().print("-{}", .{@tagName(abi)}); | |
| 523 | 523 | } |
| 524 | 524 | |
| 525 | 525 | return result.toOwnedSlice(); |
src-self-hosted/dep_tokenizer.zig+5-5| ... | ... | @@ -306,12 +306,12 @@ pub const Tokenizer = struct { |
| 306 | 306 | |
| 307 | 307 | fn errorPosition(self: *Tokenizer, position: usize, bytes: []const u8, comptime fmt: []const u8, args: var) Error { |
| 308 | 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 | 310 | try buffer.append(" '"); |
| 311 | 311 | var out = makeOutput(std.Buffer.append, &buffer); |
| 312 | 312 | try printCharValues(&out, bytes); |
| 313 | 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 | 315 | self.error_text = buffer.toSlice(); |
| 316 | 316 | return Error.InvalidInput; |
| 317 | 317 | } |
| ... | ... | @@ -320,8 +320,8 @@ pub const Tokenizer = struct { |
| 320 | 320 | var buffer = try std.Buffer.initSize(&self.arena.allocator, 0); |
| 321 | 321 | try buffer.append("illegal char "); |
| 322 | 322 | 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); | |
| 325 | 325 | self.error_text = buffer.toSlice(); |
| 326 | 326 | return Error.InvalidInput; |
| 327 | 327 | } |
| ... | ... | @@ -997,7 +997,7 @@ fn printCharValues(out: var, bytes: []const u8) !void { |
| 997 | 997 | |
| 998 | 998 | fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void { |
| 999 | 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 | 1001 | } else { |
| 1002 | 1002 | try buffer.append("'"); |
| 1003 | 1003 | try buffer.appendByte(printable_char_tab[char]); |
src-self-hosted/stage2.zig+3-3| ... | ... | @@ -1019,7 +1019,7 @@ const Stage2Target = extern struct { |
| 1019 | 1019 | .macosx, |
| 1020 | 1020 | .netbsd, |
| 1021 | 1021 | .openbsd, |
| 1022 | => try os_builtin_str_buffer.print( | |
| 1022 | => try os_builtin_str_buffer.outStream().print( | |
| 1023 | 1023 | \\ .semver = .{{ |
| 1024 | 1024 | \\ .min = .{{ |
| 1025 | 1025 | \\ .major = {}, |
| ... | ... | @@ -1043,7 +1043,7 @@ const Stage2Target = extern struct { |
| 1043 | 1043 | target.os.version_range.semver.max.patch, |
| 1044 | 1044 | }), |
| 1045 | 1045 | |
| 1046 | .linux => try os_builtin_str_buffer.print( | |
| 1046 | .linux => try os_builtin_str_buffer.outStream().print( | |
| 1047 | 1047 | \\ .linux = .{{ |
| 1048 | 1048 | \\ .range = .{{ |
| 1049 | 1049 | \\ .min = .{{ |
| ... | ... | @@ -1078,7 +1078,7 @@ const Stage2Target = extern struct { |
| 1078 | 1078 | target.os.version_range.linux.glibc.patch, |
| 1079 | 1079 | }), |
| 1080 | 1080 | |
| 1081 | .windows => try os_builtin_str_buffer.print( | |
| 1081 | .windows => try os_builtin_str_buffer.outStream().print( | |
| 1082 | 1082 | \\ .windows = .{{ |
| 1083 | 1083 | \\ .min = .{}, |
| 1084 | 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 | 4755 | const start_index = c.source_buffer.len(); |
| 4756 | 4756 | errdefer c.source_buffer.shrink(start_index); |
| 4757 | 4757 | |
| 4758 | try c.source_buffer.print(format, args); | |
| 4758 | try c.source_buffer.outStream().print(format, args); | |
| 4759 | 4759 | const end_index = c.source_buffer.len(); |
| 4760 | 4760 | const token_index = c.tree.tokens.len; |
| 4761 | 4761 | const new_token = try c.tree.tokens.addOne(); |