authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-06 16:45:42-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 10:41:09-05:00
loged7f30e1cd0c00c82c511ad826fe8d8b60b2f57f
treef9c012d90e6bb2973c34b3adcf7eabb0957b8f3b
parent786216ca5a7cc992d1be895702d37798bc85bf14

Migrate last vestiges of fmt


2 files changed, 15 insertions(+), 17 deletions(-)

src-self-hosted/dep_tokenizer.zig+12-14
......@@ -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 std.fmt.format(&buffer, anyerror, std.Buffer.append, fmt, args) catch {};
309 try buffer.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 std.fmt.format(&buffer, anyerror, std.Buffer.append, " at position {}", .{position - (bytes.len - 1)}) catch {};
314 try buffer.print(" at position {}", .{position - (bytes.len - 1)});
315315 self.error_text = buffer.toSlice();
316316 return Error.InvalidInput;
317317 }
......@@ -319,10 +319,9 @@ pub const Tokenizer = struct {
319319 fn errorIllegalChar(self: *Tokenizer, position: usize, char: u8, comptime fmt: []const u8, args: var) Error {
320320 var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
321321 try buffer.append("illegal char ");
322 var out = makeOutput(std.Buffer.append, &buffer);
323 try printUnderstandableChar(&out, char);
324 std.fmt.format(&buffer, anyerror, std.Buffer.append, " at position {}", .{position}) catch {};
325 if (fmt.len != 0) std.fmt.format(&buffer, anyerror, std.Buffer.append, ": " ++ fmt, args) catch {};
322 try printUnderstandableChar(&buffer, char);
323 try buffer.print(" at position {}", .{position});
324 if (fmt.len != 0) try buffer.print(": " ++ fmt, args);
326325 self.error_text = buffer.toSlice();
327326 return Error.InvalidInput;
328327 }
......@@ -980,13 +979,13 @@ fn hexDump16(out: var, offset: usize, bytes: []const u8) !void {
980979
981980fn printDecValue(out: var, value: u64, width: u8) !void {
982981 var buffer: [20]u8 = undefined;
983 const len = std.fmt.formatIntBuf(buffer[0..], value, 10, false, width);
982 const len = std.fmtstream.formatIntBuf(buffer[0..], value, 10, false, width);
984983 try out.write(buffer[0..len]);
985984}
986985
987986fn printHexValue(out: var, value: u64, width: u8) !void {
988987 var buffer: [16]u8 = undefined;
989 const len = std.fmt.formatIntBuf(buffer[0..], value, 16, false, width);
988 const len = std.fmtstream.formatIntBuf(buffer[0..], value, 16, false, width);
990989 try out.write(buffer[0..len]);
991990}
992991
......@@ -996,14 +995,13 @@ fn printCharValues(out: var, bytes: []const u8) !void {
996995 }
997996}
998997
999fn printUnderstandableChar(out: var, char: u8) !void {
998fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void {
1000999 if (!std.ascii.isPrint(char) or char == ' ') {
1001 const output = @typeInfo(@TypeOf(out)).Pointer.child.output;
1002 std.fmt.format(out.context, anyerror, output, "\\x{X:2}", .{char}) catch {};
1000 try buffer.print("\\x{X:2}", .{char});
10031001 } else {
1004 try out.write("'");
1005 try out.write(&[_]u8{printable_char_tab[char]});
1006 try out.write("'");
1002 try buffer.append("'");
1003 try buffer.appendByte(printable_char_tab[char]);
1004 try buffer.append("'");
10071005 }
10081006}
10091007
test/stage1/behavior/enum_with_members.zig+3-3
......@@ -1,6 +1,6 @@
11const expect = @import("std").testing.expect;
22const mem = @import("std").mem;
3const fmt = @import("std").fmt;
3const fmtstream = @import("std").fmtstream;
44
55const ET = union(enum) {
66 SINT: i32,
......@@ -8,8 +8,8 @@ const ET = union(enum) {
88
99 pub fn print(a: *const ET, buf: []u8) anyerror!usize {
1010 return switch (a.*) {
11 ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
12 ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
11 ET.SINT => |x| fmtstream.formatIntBuf(buf, x, 10, false, fmtstream.FormatOptions{}),
12 ET.UINT => |x| fmtstream.formatIntBuf(buf, x, 10, false, fmtstream.FormatOptions{}),
1313 };
1414 }
1515};