Migrate last vestiges of fmt

This commit is contained in:
Benjamin Feng 2020-03-06 16:45:42 -06:00
parent 786216ca5a
commit ed7f30e1cd
2 changed files with 15 additions and 17 deletions

View File

@ -306,12 +306,12 @@ pub const Tokenizer = struct {
fn errorPosition(self: *Tokenizer, position: usize, bytes: []const u8, comptime fmt: []const u8, args: var) Error {
var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
std.fmt.format(&buffer, anyerror, std.Buffer.append, fmt, args) catch {};
try buffer.print(fmt, args);
try buffer.append(" '");
var out = makeOutput(std.Buffer.append, &buffer);
try printCharValues(&out, bytes);
try buffer.append("'");
std.fmt.format(&buffer, anyerror, std.Buffer.append, " at position {}", .{position - (bytes.len - 1)}) catch {};
try buffer.print(" at position {}", .{position - (bytes.len - 1)});
self.error_text = buffer.toSlice();
return Error.InvalidInput;
}
@ -319,10 +319,9 @@ pub const Tokenizer = struct {
fn errorIllegalChar(self: *Tokenizer, position: usize, char: u8, comptime fmt: []const u8, args: var) Error {
var buffer = try std.Buffer.initSize(&self.arena.allocator, 0);
try buffer.append("illegal char ");
var out = makeOutput(std.Buffer.append, &buffer);
try printUnderstandableChar(&out, char);
std.fmt.format(&buffer, anyerror, std.Buffer.append, " at position {}", .{position}) catch {};
if (fmt.len != 0) std.fmt.format(&buffer, anyerror, std.Buffer.append, ": " ++ fmt, args) catch {};
try printUnderstandableChar(&buffer, char);
try buffer.print(" at position {}", .{position});
if (fmt.len != 0) try buffer.print(": " ++ fmt, args);
self.error_text = buffer.toSlice();
return Error.InvalidInput;
}
@ -980,13 +979,13 @@ fn hexDump16(out: var, offset: usize, bytes: []const u8) !void {
fn printDecValue(out: var, value: u64, width: u8) !void {
var buffer: [20]u8 = undefined;
const len = std.fmt.formatIntBuf(buffer[0..], value, 10, false, width);
const len = std.fmtstream.formatIntBuf(buffer[0..], value, 10, false, width);
try out.write(buffer[0..len]);
}
fn printHexValue(out: var, value: u64, width: u8) !void {
var buffer: [16]u8 = undefined;
const len = std.fmt.formatIntBuf(buffer[0..], value, 16, false, width);
const len = std.fmtstream.formatIntBuf(buffer[0..], value, 16, false, width);
try out.write(buffer[0..len]);
}
@ -996,14 +995,13 @@ fn printCharValues(out: var, bytes: []const u8) !void {
}
}
fn printUnderstandableChar(out: var, char: u8) !void {
fn printUnderstandableChar(buffer: *std.Buffer, char: u8) !void {
if (!std.ascii.isPrint(char) or char == ' ') {
const output = @typeInfo(@TypeOf(out)).Pointer.child.output;
std.fmt.format(out.context, anyerror, output, "\\x{X:2}", .{char}) catch {};
try buffer.print("\\x{X:2}", .{char});
} else {
try out.write("'");
try out.write(&[_]u8{printable_char_tab[char]});
try out.write("'");
try buffer.append("'");
try buffer.appendByte(printable_char_tab[char]);
try buffer.append("'");
}
}

View File

@ -1,6 +1,6 @@
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
const fmt = @import("std").fmt;
const fmtstream = @import("std").fmtstream;
const ET = union(enum) {
SINT: i32,
@ -8,8 +8,8 @@ const ET = union(enum) {
pub fn print(a: *const ET, buf: []u8) anyerror!usize {
return switch (a.*) {
ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
ET.SINT => |x| fmtstream.formatIntBuf(buf, x, 10, false, fmtstream.FormatOptions{}),
ET.UINT => |x| fmtstream.formatIntBuf(buf, x, 10, false, fmtstream.FormatOptions{}),
};
}
};