summaryrefslogtreecommitdiff
path: root/src/fmt/format.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2019-01-29 22:24:36 -0500
committerRuss Cox <rsc@golang.org>2019-02-26 05:16:56 +0000
commitac51237affc016dd22f5b4f67dc8a2d09adf1fb2 (patch)
tree3a8ef2099ca6356ea1d69cab83f3755d8f09cdd2 /src/fmt/format.go
parente1a6d1fc08b2701ac9f67353cb52c51d52877669 (diff)
downloadgo-git-ac51237affc016dd22f5b4f67dc8a2d09adf1fb2.tar.gz
fmt: format 0b, 0o prefixes in %#b and %O
This CL modifies fmt's printer to implement %#b and %O to emit leading 0b and 0o prefixes on binary and octal. (%#o is already taken and emits "0377"; %O emits "0o377".) See golang.org/design/19308-number-literals for background. For #19308. For #12711. Vet update is #29986. Change-Id: I7c38a4484c48a03abe9f6d45c7d981c7c314f583 Reviewed-on: https://go-review.googlesource.com/c/160246 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/fmt/format.go')
-rw-r--r--src/fmt/format.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/fmt/format.go b/src/fmt/format.go
index 6d93908095..24e7e9551a 100644
--- a/src/fmt/format.go
+++ b/src/fmt/format.go
@@ -191,7 +191,7 @@ func (f *fmt) fmtUnicode(u uint64) {
}
// fmtInteger formats signed and unsigned integers.
-func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, digits string) {
+func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, verb rune, digits string) {
negative := isSigned && int64(u) < 0
if negative {
u = -u
@@ -275,6 +275,12 @@ func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, digits string) {
// Various prefixes: 0x, -, etc.
if f.sharp {
switch base {
+ case 2:
+ // Add a leading 0b.
+ i--
+ buf[i] = 'b'
+ i--
+ buf[i] = '0'
case 8:
if buf[i] != '0' {
i--
@@ -288,6 +294,12 @@ func (f *fmt) fmtInteger(u uint64, base int, isSigned bool, digits string) {
buf[i] = '0'
}
}
+ if verb == 'O' {
+ i--
+ buf[i] = 'o'
+ i--
+ buf[i] = '0'
+ }
if negative {
i--