var _ fmt.Formatter = intOne // *Int must implement fmt.Formatter
// Format implements fmt.Formatter. It accepts the formats
-// 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase
-// hexadecimal), and 'X' (uppercase hexadecimal).
+// 'b' (binary), 'o' (octal with 0 prefix), 'O' (octal with 0o prefix),
+// 'd' (decimal), 'x' (lowercase hexadecimal), and
+// 'X' (uppercase hexadecimal).
// Also supported are the full suite of package fmt's format
// flags for integral types, including '+' and ' ' for sign
// control, '#' for leading zero in octal and for hexadecimal,
switch ch {
case 'b':
base = 2
- case 'o':
+ case 'o', 'O':
base = 8
case 'd', 's', 'v':
base = 10
prefix := ""
if s.Flag('#') {
switch ch {
+ case 'b': // binary
+ prefix = "0b"
case 'o': // octal
prefix = "0"
case 'x': // hexadecimal
prefix = "0X"
}
}
+ if ch == 'O' {
+ prefix = "0o"
+ }
digits := x.abs.utoa(base)
if ch == 'X' {
{"10", "%y", "%!y(big.Int=10)"},
{"-10", "%y", "%!y(big.Int=-10)"},
- {"10", "%#b", "1010"},
+ {"10", "%#b", "0b1010"},
{"10", "%#o", "012"},
+ {"10", "%O", "0o12"},
+ {"-10", "%#b", "-0b1010"},
+ {"-10", "%#o", "-012"},
+ {"-10", "%O", "-0o12"},
{"10", "%#d", "10"},
{"10", "%#v", "10"},
{"10", "%#x", "0xa"},