authorgravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-02-29 12:25:12-06:00
committergravatar for benjamin.feng@glassdoor.comBenjamin Feng <benjamin.feng@glassdoor.com> 2020-03-12 09:06:12-05:00
log8241b96f78fa8ee33f9a73d4d296509d49b6f351
tree54cc76983bb1e76d981bf74d5f13257e4fbb5c57
parente1e9ff9546d0898764ff9c9383e57d0fa4e9bd0e

Re-enable testFmt


1 files changed, 33 insertions(+), 44 deletions(-)

lib/std/fmtstream.zig+33-44
...@@ -1074,28 +1074,17 @@ fn digitToChar(digit: u8, uppercase: bool) u8 {...@@ -1074,28 +1074,17 @@ fn digitToChar(digit: u8, uppercase: bool) u8 {
1074 };1074 };
1075}1075}
10761076
1077// const BufPrintContext = struct {1077pub const BufPrintError = error{
1078// remaining: []u8,1078 /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
1079// };1079 BufferTooSmall,
10801080};
1081// fn bufPrintWrite(context: *BufPrintContext, bytes: []const u8) !void {1081pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
1082// if (context.remaining.len < bytes.len) {1082 var os = std.io.SliceOutStream.init(buf);
1083// mem.copy(u8, context.remaining, bytes[0..context.remaining.len]);1083 format(&os.stream, fmt, args) catch |err| switch (err) {
1084// return error.BufferTooSmall;1084 error.OutOfMemory => return error.BufferTooSmall,
1085// }1085 };
1086// mem.copy(u8, context.remaining, bytes);1086 return buf[0..os.pos];
1087// context.remaining = context.remaining[bytes.len..];1087}
1088// }
1089
1090// pub const BufPrintError = error{
1091// /// As much as possible was written to the buffer, but it was too small to fit all the printed bytes.
1092// BufferTooSmall,
1093// };
1094// pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: var) BufPrintError![]u8 {
1095// var context = BufPrintContext{ .remaining = buf };
1096// try format(&context, BufPrintError, bufPrintWrite, fmt, args);
1097// return buf[0 .. buf.len - context.remaining.len];
1098// }
10991088
1100// pub const AllocPrintError = error{OutOfMemory};1089// pub const AllocPrintError = error{OutOfMemory};
11011090
...@@ -1514,29 +1503,29 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options:...@@ -1514,29 +1503,29 @@ fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, options:
1514// try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b});1503// try testFmt("B{ .a = A{ }, .c = 0 }", "{}", .{b});
1515// }1504// }
15161505
1517// test "bytes.hex" {1506test "bytes.hex" {
1518// const some_bytes = "\xCA\xFE\xBA\xBE";1507 const some_bytes = "\xCA\xFE\xBA\xBE";
1519// try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes});1508 try testFmt("lowercase: cafebabe\n", "lowercase: {x}\n", .{some_bytes});
1520// try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes});1509 try testFmt("uppercase: CAFEBABE\n", "uppercase: {X}\n", .{some_bytes});
1521// //Test Slices1510 //Test Slices
1522// try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]});1511 try testFmt("uppercase: CAFE\n", "uppercase: {X}\n", .{some_bytes[0..2]});
1523// try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]});1512 try testFmt("lowercase: babe\n", "lowercase: {x}\n", .{some_bytes[2..]});
1524// const bytes_with_zeros = "\x00\x0E\xBA\xBE";1513 const bytes_with_zeros = "\x00\x0E\xBA\xBE";
1525// try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros});1514 try testFmt("lowercase: 000ebabe\n", "lowercase: {x}\n", .{bytes_with_zeros});
1526// }1515}
15271516
1528// fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void {1517fn testFmt(expected: []const u8, comptime template: []const u8, args: var) !void {
1529// var buf: [100]u8 = undefined;1518 var buf: [100]u8 = undefined;
1530// const result = try bufPrint(buf[0..], template, args);1519 const result = try bufPrint(buf[0..], template, args);
1531// if (mem.eql(u8, result, expected)) return;1520 if (mem.eql(u8, result, expected)) return;
15321521
1533// std.debug.warn("\n====== expected this output: =========\n", .{});1522 std.debug.warn("\n====== expected this output: =========\n", .{});
1534// std.debug.warn("{}", .{expected});1523 std.debug.warn("{}", .{expected});
1535// std.debug.warn("\n======== instead found this: =========\n", .{});1524 std.debug.warn("\n======== instead found this: =========\n", .{});
1536// std.debug.warn("{}", .{result});1525 std.debug.warn("{}", .{result});
1537// std.debug.warn("\n======================================\n", .{});1526 std.debug.warn("\n======================================\n", .{});
1538// return error.TestFailed;1527 return error.TestFailed;
1539// }1528}
15401529
1541// pub fn trim(buf: []const u8) []const u8 {1530// pub fn trim(buf: []const u8) []const u8 {
1542// var start: usize = 0;1531// var start: usize = 0;