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)
This commit is contained in:
~nue 2020-07-14 02:27:58 -04:00 committed by GitHub
parent bc900cdeaf
commit 03f14c3102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,6 +64,7 @@ fn peekIsAlign(comptime fmt: []const u8) bool {
/// - `e`: output floating point value in scientific notation /// - `e`: output floating point value in scientific notation
/// - `d`: output numeric value in decimal notation /// - `d`: output numeric value in decimal notation
/// - `b`: output integer value in binary 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. /// - `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. /// - `*`: 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")) { } else if (comptime std.mem.eql(u8, fmt, "X")) {
radix = 16; radix = 16;
uppercase = true; uppercase = true;
} else if (comptime std.mem.eql(u8, fmt, "o")) {
radix = 8;
uppercase = false;
} else { } else {
@compileError("Unknown format string: '" ++ fmt ++ "'"); @compileError("Unknown format string: '" ++ fmt ++ "'");
} }
@ -1240,6 +1244,10 @@ test "int.specifier" {
const value: u8 = 0b1100; const value: u8 = 0b1100;
try testFmt("u8: 0b1100\n", "u8: 0b{b}\n", .{value}); 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" { test "int.padded" {