diff options
| author | Benjamin Feng <benjamin.feng@glassdoor.com> | 2020-02-29 12:25:12 -0600 |
|---|---|---|
| committer | Benjamin Feng <benjamin.feng@glassdoor.com> | 2020-03-12 09:06:12 -0500 |
| commit | 8241b96f78fa8ee33f9a73d4d296509d49b6f351 (patch) | |
| tree | 54cc76983bb1e76d981bf74d5f13257e4fbb5c57 | |
| parent | e1e9ff9546d0898764ff9c9383e57d0fa4e9bd0e (diff) | |
| download | zig-8241b96f78fa8ee33f9a73d4d296509d49b6f351.tar.gz zig-8241b96f78fa8ee33f9a73d4d296509d49b6f351.zip | |
Re-enable testFmt
| -rw-r--r-- | lib/std/fmtstream.zig | 77 |
1 files changed, 33 insertions, 44 deletions
diff --git a/lib/std/fmtstream.zig b/lib/std/fmtstream.zig index d64c6cdf7f..4a7d8041c0 100644 --- a/lib/std/fmtstream.zig +++ b/lib/std/fmtstream.zig @@ -1074,28 +1074,17 @@ fn digitToChar(digit: u8, uppercase: bool) u8 { }; } -// const BufPrintContext = struct { -// remaining: []u8, -// }; - -// fn bufPrintWrite(context: *BufPrintContext, bytes: []const u8) !void { -// if (context.remaining.len < bytes.len) { -// mem.copy(u8, context.remaining, bytes[0..context.remaining.len]); -// return error.BufferTooSmall; -// } -// mem.copy(u8, context.remaining, bytes); -// context.remaining = context.remaining[bytes.len..]; -// } - -// pub const BufPrintError = error{ -// /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes. -// BufferTooSmall, -// }; -// pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { -// var context = BufPrintContext{ .remaining = buf }; -// try format(&context, BufPrintError, bufPrintWrite, fmt, args); -// return buf[0 .. buf.len - context.remaining.len]; -// } +pub const BufPrintError = error{ + /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes. + BufferTooSmall, +}; +pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 { + var os = std.io.SliceOutStream.init(buf); + format(&os.stream, fmt, args) catch |err| switch (err) { + error.OutOfMemory => return error.BufferTooSmall, + }; + return buf[0..os.pos]; +} // pub const AllocPrintError = error{OutOfMemory}; @@ -1514,29 +1503,29 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options: // try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b}); // } -// test "bytes.hex" { -// const some_bytes = "\xCA\xFE\xBA\xBE"; -// try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes}); -// try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes}); -// //Test Slices -// try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]}); -// try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]}); -// const bytes_with_zeros = "\x00\x0E\xBA\xBE"; -// try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros}); -// } +test "bytes.hex" { + const some_bytes = "\xCA\xFE\xBA\xBE"; + try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes}); + try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes}); + //Test Slices + try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]}); + try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]}); + const bytes_with_zeros = "\x00\x0E\xBA\xBE"; + try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros}); +} -// fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void { -// var buf: [100]u8 = undefined; -// const result = try bufPrint(buf[0..], template, args); -// if (mem.eql(u8, result, expected)) return; - -// std.debug.warn("\n====== expected this output: =========\n", .{}); -// std.debug.warn("{}", .{expected}); -// std.debug.warn("\n======== instead found this: =========\n", .{}); -// std.debug.warn("{}", .{result}); -// std.debug.warn("\n======================================\n", .{}); -// return error.TestFailed; -// } +fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void { + var buf: [100]u8 = undefined; + const result = try bufPrint(buf[0..], template, args); + if (mem.eql(u8, result, expected)) return; + + std.debug.warn("\n====== expected this output: =========\n", .{}); + std.debug.warn("{}", .{expected}); + std.debug.warn("\n======== instead found this: =========\n", .{}); + std.debug.warn("{}", .{result}); + std.debug.warn("\n======================================\n", .{}); + return error.TestFailed; +} // pub fn trim(buf: []const u8) []const u8 { // var start: usize = 0; |
