diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-10-23 04:31:11 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-10-23 04:31:11 +0000 |
commit | fb08d0057f91d420b6f48c112264fc87dc91b532 (patch) | |
tree | 46bb86f514fbf6bad82da48e69a18fb09d878834 /libgo/go/fmt/format.go | |
parent | f507227a181bb31fa87d23a082485f99f3ef9183 (diff) | |
download | gcc-fb08d0057f91d420b6f48c112264fc87dc91b532.tar.gz |
libgo: Update to current sources.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@192704 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/fmt/format.go')
-rw-r--r-- | libgo/go/fmt/format.go | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/libgo/go/fmt/format.go b/libgo/go/fmt/format.go index caf900d5c30..ce801162d6e 100644 --- a/libgo/go/fmt/format.go +++ b/libgo/go/fmt/format.go @@ -110,11 +110,11 @@ func (f *fmt) writePadding(n int, padding []byte) { // Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus) // clear flags afterwards. func (f *fmt) pad(b []byte) { - var padding []byte - var left, right int - if f.widPresent && f.wid != 0 { - padding, left, right = f.computePadding(len(b)) + if !f.widPresent || f.wid == 0 { + f.buf.Write(b) + return } + padding, left, right := f.computePadding(len(b)) if left > 0 { f.writePadding(left, padding) } @@ -127,11 +127,11 @@ func (f *fmt) pad(b []byte) { // append s to buf, padded on left (w > 0) or right (w < 0 or f.minus). // clear flags afterwards. func (f *fmt) padString(s string) { - var padding []byte - var left, right int - if f.widPresent && f.wid != 0 { - padding, left, right = f.computePadding(utf8.RuneCountInString(s)) + if !f.widPresent || f.wid == 0 { + f.buf.WriteString(s) + return } + padding, left, right := f.computePadding(utf8.RuneCountInString(s)) if left > 0 { f.writePadding(left, padding) } @@ -285,18 +285,41 @@ func (f *fmt) fmt_s(s string) { f.padString(s) } -// fmt_sx formats a string as a hexadecimal encoding of its bytes. -func (f *fmt) fmt_sx(s, digits string) { +// fmt_sbx formats a string or byte slice as a hexadecimal encoding of its bytes. +func (f *fmt) fmt_sbx(s string, b []byte, digits string) { + n := len(b) + if b == nil { + n = len(s) + } + x := digits[10] - 'a' + 'x' // TODO: Avoid buffer by pre-padding. - var b []byte - for i := 0; i < len(s); i++ { + var buf []byte + for i := 0; i < n; i++ { if i > 0 && f.space { - b = append(b, ' ') + buf = append(buf, ' ') + } + if f.sharp { + buf = append(buf, '0', x) + } + var c byte + if b == nil { + c = s[i] + } else { + c = b[i] } - v := s[i] - b = append(b, digits[v>>4], digits[v&0xF]) + buf = append(buf, digits[c>>4], digits[c&0xF]) } - f.pad(b) + f.pad(buf) +} + +// fmt_sx formats a string as a hexadecimal encoding of its bytes. +func (f *fmt) fmt_sx(s, digits string) { + f.fmt_sbx(s, nil, digits) +} + +// fmt_bx formats a byte slice as a hexadecimal encoding of its bytes. +func (f *fmt) fmt_bx(b []byte, digits string) { + f.fmt_sbx("", b, digits) } // fmt_q formats a string as a double-quoted, escaped Go string constant. |