Commit d6311ff1 authored by Russ Cox's avatar Russ Cox

math/big: add %#b and %O integer formats

Matching fmt, %#b now prints an 0b prefix,
and %O prints octal with an 0o prefix.

See golang.org/design/19308-number-literals for background.

For #19308.
For #12711.

Change-Id: I139c5a9a1dfae15415621601edfa13c6a5f19cfc
Reviewed-on: https://go-review.googlesource.com/c/160250Reviewed-by: default avatarRob Pike <r@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 675503c5
...@@ -50,8 +50,9 @@ func writeMultiple(s fmt.State, text string, count int) { ...@@ -50,8 +50,9 @@ func writeMultiple(s fmt.State, text string, count int) {
var _ fmt.Formatter = intOne // *Int must implement fmt.Formatter var _ fmt.Formatter = intOne // *Int must implement fmt.Formatter
// Format implements fmt.Formatter. It accepts the formats // Format implements fmt.Formatter. It accepts the formats
// 'b' (binary), 'o' (octal), 'd' (decimal), 'x' (lowercase // 'b' (binary), 'o' (octal with 0 prefix), 'O' (octal with 0o prefix),
// hexadecimal), and 'X' (uppercase hexadecimal). // 'd' (decimal), 'x' (lowercase hexadecimal), and
// 'X' (uppercase hexadecimal).
// Also supported are the full suite of package fmt's format // Also supported are the full suite of package fmt's format
// flags for integral types, including '+' and ' ' for sign // flags for integral types, including '+' and ' ' for sign
// control, '#' for leading zero in octal and for hexadecimal, // control, '#' for leading zero in octal and for hexadecimal,
...@@ -66,7 +67,7 @@ func (x *Int) Format(s fmt.State, ch rune) { ...@@ -66,7 +67,7 @@ func (x *Int) Format(s fmt.State, ch rune) {
switch ch { switch ch {
case 'b': case 'b':
base = 2 base = 2
case 'o': case 'o', 'O':
base = 8 base = 8
case 'd', 's', 'v': case 'd', 's', 'v':
base = 10 base = 10
...@@ -98,6 +99,8 @@ func (x *Int) Format(s fmt.State, ch rune) { ...@@ -98,6 +99,8 @@ func (x *Int) Format(s fmt.State, ch rune) {
prefix := "" prefix := ""
if s.Flag('#') { if s.Flag('#') {
switch ch { switch ch {
case 'b': // binary
prefix = "0b"
case 'o': // octal case 'o': // octal
prefix = "0" prefix = "0"
case 'x': // hexadecimal case 'x': // hexadecimal
...@@ -106,6 +109,9 @@ func (x *Int) Format(s fmt.State, ch rune) { ...@@ -106,6 +109,9 @@ func (x *Int) Format(s fmt.State, ch rune) {
prefix = "0X" prefix = "0X"
} }
} }
if ch == 'O' {
prefix = "0o"
}
digits := x.abs.utoa(base) digits := x.abs.utoa(base)
if ch == 'X' { if ch == 'X' {
......
...@@ -214,8 +214,12 @@ var formatTests = []struct { ...@@ -214,8 +214,12 @@ var formatTests = []struct {
{"10", "%y", "%!y(big.Int=10)"}, {"10", "%y", "%!y(big.Int=10)"},
{"-10", "%y", "%!y(big.Int=-10)"}, {"-10", "%y", "%!y(big.Int=-10)"},
{"10", "%#b", "1010"}, {"10", "%#b", "0b1010"},
{"10", "%#o", "012"}, {"10", "%#o", "012"},
{"10", "%O", "0o12"},
{"-10", "%#b", "-0b1010"},
{"-10", "%#o", "-012"},
{"-10", "%O", "-0o12"},
{"10", "%#d", "10"}, {"10", "%#d", "10"},
{"10", "%#v", "10"}, {"10", "%#v", "10"},
{"10", "%#x", "0xa"}, {"10", "%#x", "0xa"},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment