From 03f14c3102ccd3e1811ccf4fb7cae11a75d3018f Mon Sep 17 00:00:00 2001 From: ~nue Date: Tue, 14 Jul 2020 02:27:58 -0400 Subject: [PATCH] Added octal formatting fo `fmt` functions. (#5867) * Added octal formatting (specifier "o") to `formatIntValue` function. * Added octal specifier test case in `fmt.zig` (under the "int.specifier" case) --- lib/std/fmt.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 7e288170af..c9ba3b3470 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -64,6 +64,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool { /// - `e`: output floating point value in scientific notation /// - `d`: output numeric value in decimal notation /// - `b`: output integer value in binary notation +/// - `o`: output integer value in octal notation /// - `c`: output integer as an ASCII character. Integer type must have 8 bits at max. /// - `*`: output the address of the value instead of the value itself. /// @@ -543,6 +544,9 @@ pub fn formatIntValue( } else if (comptime std.mem.eql(u8, fmt, "X")) { radix = 16; uppercase = true; + } else if (comptime std.mem.eql(u8, fmt, "o")) { + radix = 8; + uppercase = false; } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } @@ -1240,6 +1244,10 @@ test "int.specifier" { const value: u8 = 0b1100; try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value}); } + { + const value: u16 = 0o1234; + try testFmt("u16: 0o1234\n", "u16: 0o{o}\n", .{value}); + } } test "int.padded" {