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