commit 03f14c3102ccd3e1811ccf4fb7cae11a75d3018f (tree)
parent bc900cdeaf3434acc150a8db203eb5a485c09b67
Author: ~nue <nue@envs.net>
Date: Tue, 14 Jul 2020 02:27:58 -0400
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)
Diffstat:
1 file changed, 8 insertions(+), 0 deletions(-)
diff --git 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" {