Commit 50a1d89a authored by Russ Cox's avatar Russ Cox

fmt: rename buffer.WriteByte to writeByte

Renaming the method makes clear, both to readers and to vet,
that this method is not the implementation of io.ByteWriter.

Working toward making the tree vet-safe instead of having
so many exceptions in cmd/vet/all/whitelist.

For #31916.

Change-Id: I79da062ca6469b62a6b9e284c6cf2413c7425249
Reviewed-on: https://go-review.googlesource.com/c/go/+/176109
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarAustin Clements <austin@google.com>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent a44c3edb
......@@ -23,9 +23,8 @@ cmd/compile/internal/gc/testdata/short_test.go: unreachable code
// These cases are basically ok.
// Errors are handled reasonably and there's no clear need for interface satisfaction.
// Except for the runtime/pprof case, the API is not exported.
fmt/print.go: method WriteByte(c byte) should have signature WriteByte(byte) error
// Also non-standard, but this method is on an unexported type, so it's
// Also on-standard, but this method is on an unexported type, so it's
// irrelevant.
encoding/gob/encode.go: method WriteByte(c byte) should have signature WriteByte(byte) error
......
......@@ -156,10 +156,10 @@ loop:
break
}
if w.fmt.needColon || !p.fmt.plusV {
w.buf.WriteByte(':')
w.buf.writeByte(':')
w.fmt.needColon = false
}
w.buf.WriteString(sep)
w.buf.writeString(sep)
w.fmt.inDetail = false
w.fmt.needNewline = false
}
......@@ -195,24 +195,24 @@ func (p *errPPState) Write(b []byte) (n int, err error) {
for i, c := range b {
if p.fmt.needNewline {
if p.fmt.inDetail && p.fmt.needColon {
p.buf.WriteByte(':')
p.buf.writeByte(':')
p.fmt.needColon = false
}
p.buf.Write(detailSep)
p.buf.write(detailSep)
p.fmt.needNewline = false
}
if c == '\n' {
p.buf.Write(b[k:i])
p.buf.write(b[k:i])
k = i + 1
p.fmt.needNewline = true
}
}
p.buf.Write(b[k:])
p.buf.write(b[k:])
if !p.fmt.inDetail {
p.fmt.needColon = true
}
} else if !p.fmt.inDetail {
p.buf.Write(b)
p.buf.write(b)
}
return len(b), nil
......
......@@ -94,17 +94,17 @@ func (f *fmt) writePadding(n int) {
// pad appends b to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *fmt) pad(b []byte) {
if !f.widPresent || f.wid == 0 {
f.buf.Write(b)
f.buf.write(b)
return
}
width := f.wid - utf8.RuneCount(b)
if !f.minus {
// left padding
f.writePadding(width)
f.buf.Write(b)
f.buf.write(b)
} else {
// right padding
f.buf.Write(b)
f.buf.write(b)
f.writePadding(width)
}
}
......@@ -112,17 +112,17 @@ func (f *fmt) pad(b []byte) {
// padString appends s to f.buf, padded on left (!f.minus) or right (f.minus).
func (f *fmt) padString(s string) {
if !f.widPresent || f.wid == 0 {
f.buf.WriteString(s)
f.buf.writeString(s)
return
}
width := f.wid - utf8.RuneCountInString(s)
if !f.minus {
// left padding
f.writePadding(width)
f.buf.WriteString(s)
f.buf.writeString(s)
} else {
// right padding
f.buf.WriteString(s)
f.buf.writeString(s)
f.writePadding(width)
}
}
......@@ -574,9 +574,9 @@ func (f *fmt) fmtFloat(v float64, size int, verb rune, prec int) {
// If we're zero padding to the left we want the sign before the leading zeros.
// Achieve this by writing the sign out and then padding the unsigned number.
if f.zero && f.widPresent && f.wid > len(num) {
f.buf.WriteByte(num[0])
f.buf.writeByte(num[0])
f.writePadding(f.wid - len(num))
f.buf.Write(num[1:])
f.buf.write(num[1:])
return
}
f.pad(num)
......
......@@ -75,19 +75,19 @@ type GoStringer interface {
// Use simple []byte instead of bytes.Buffer to avoid large dependency.
type buffer []byte
func (b *buffer) Write(p []byte) {
func (b *buffer) write(p []byte) {
*b = append(*b, p...)
}
func (b *buffer) WriteString(s string) {
func (b *buffer) writeString(s string) {
*b = append(*b, s...)
}
func (b *buffer) WriteByte(c byte) {
func (b *buffer) writeByte(c byte) {
*b = append(*b, c)
}
func (bp *buffer) WriteRune(r rune) {
func (bp *buffer) writeRune(r rune) {
if r < utf8.RuneSelf {
*bp = append(*bp, byte(r))
return
......@@ -179,14 +179,14 @@ func (p *pp) Flag(b int) bool {
// Implement Write so we can call Fprintf on a pp (through State), for
// recursive use in custom verbs.
func (p *pp) Write(b []byte) (ret int, err error) {
p.buf.Write(b)
p.buf.write(b)
return len(b), nil
}
// Implement WriteString so that we can call io.WriteString
// on a pp (through state), for efficiency.
func (p *pp) WriteString(s string) (ret int, err error) {
p.buf.WriteString(s)
p.buf.writeString(s)
return len(s), nil
}
......@@ -314,32 +314,32 @@ func parsenum(s string, start, end int) (num int, isnum bool, newi int) {
func (p *pp) unknownType(v reflect.Value) {
if !v.IsValid() {
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
return
}
p.buf.WriteByte('?')
p.buf.WriteString(v.Type().String())
p.buf.WriteByte('?')
p.buf.writeByte('?')
p.buf.writeString(v.Type().String())
p.buf.writeByte('?')
}
func (p *pp) badVerb(verb rune) {
p.erroring = true
p.buf.WriteString(percentBangString)
p.buf.WriteRune(verb)
p.buf.WriteByte('(')
p.buf.writeString(percentBangString)
p.buf.writeRune(verb)
p.buf.writeByte('(')
switch {
case p.arg != nil:
p.buf.WriteString(reflect.TypeOf(p.arg).String())
p.buf.WriteByte('=')
p.buf.writeString(reflect.TypeOf(p.arg).String())
p.buf.writeByte('=')
p.printArg(p.arg, 'v')
case p.value.IsValid():
p.buf.WriteString(p.value.Type().String())
p.buf.WriteByte('=')
p.buf.writeString(p.value.Type().String())
p.buf.writeByte('=')
p.printValue(p.value, 'v', 0)
default:
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
}
p.buf.WriteByte(')')
p.buf.writeByte(')')
p.erroring = false
}
......@@ -421,12 +421,12 @@ func (p *pp) fmtComplex(v complex128, size int, verb rune) {
switch verb {
case 'v', 'b', 'g', 'G', 'x', 'X', 'f', 'F', 'e', 'E':
oldPlus := p.fmt.plus
p.buf.WriteByte('(')
p.buf.writeByte('(')
p.fmtFloat(real(v), size/2, verb)
// Imaginary part always has a sign.
p.fmt.plus = true
p.fmtFloat(imag(v), size/2, verb)
p.buf.WriteString("i)")
p.buf.writeString("i)")
p.fmt.plus = oldPlus
default:
p.badVerb(verb)
......@@ -458,28 +458,28 @@ func (p *pp) fmtBytes(v []byte, verb rune, typeString string) {
switch verb {
case 'v', 'd':
if p.fmt.sharpV {
p.buf.WriteString(typeString)
p.buf.writeString(typeString)
if v == nil {
p.buf.WriteString(nilParenString)
p.buf.writeString(nilParenString)
return
}
p.buf.WriteByte('{')
p.buf.writeByte('{')
for i, c := range v {
if i > 0 {
p.buf.WriteString(commaSpaceString)
p.buf.writeString(commaSpaceString)
}
p.fmt0x64(uint64(c), true)
}
p.buf.WriteByte('}')
p.buf.writeByte('}')
} else {
p.buf.WriteByte('[')
p.buf.writeByte('[')
for i, c := range v {
if i > 0 {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
p.fmt.fmtInteger(uint64(c), 10, unsigned, verb, ldigits)
}
p.buf.WriteByte(']')
p.buf.writeByte(']')
}
case 's':
p.fmt.fmtBs(v)
......@@ -507,15 +507,15 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune) {
switch verb {
case 'v':
if p.fmt.sharpV {
p.buf.WriteByte('(')
p.buf.WriteString(value.Type().String())
p.buf.WriteString(")(")
p.buf.writeByte('(')
p.buf.writeString(value.Type().String())
p.buf.writeString(")(")
if u == 0 {
p.buf.WriteString(nilString)
p.buf.writeString(nilString)
} else {
p.fmt0x64(uint64(u), true)
}
p.buf.WriteByte(')')
p.buf.writeByte(')')
} else {
if u == 0 {
p.fmt.padString(nilAngleString)
......@@ -538,7 +538,7 @@ func (p *pp) catchPanic(arg interface{}, verb rune, method string) {
// Stringer that fails to guard against nil or a nil pointer for a
// value receiver, and in either case, "<nil>" is a nice result.
if v := reflect.ValueOf(arg); v.Kind() == reflect.Ptr && v.IsNil() {
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
return
}
// Otherwise print a concise panic message. Most of the time the panic
......@@ -552,15 +552,15 @@ func (p *pp) catchPanic(arg interface{}, verb rune, method string) {
// For this output we want default behavior.
p.fmt.clearflags()
p.buf.WriteString(percentBangString)
p.buf.WriteRune(verb)
p.buf.WriteString(panicString)
p.buf.WriteString(method)
p.buf.WriteString(" method: ")
p.buf.writeString(percentBangString)
p.buf.writeRune(verb)
p.buf.writeString(panicString)
p.buf.writeString(method)
p.buf.writeString(" method: ")
p.panicking = true
p.printArg(err, 'v')
p.panicking = false
p.buf.WriteByte(')')
p.buf.writeByte(')')
p.fmt.fmtFlags = oldFlags
}
......@@ -713,11 +713,11 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
switch f := value; value.Kind() {
case reflect.Invalid:
if depth == 0 {
p.buf.WriteString(invReflectString)
p.buf.writeString(invReflectString)
} else {
switch verb {
case 'v':
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
default:
p.badVerb(verb)
}
......@@ -740,63 +740,63 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
p.fmtString(f.String(), verb)
case reflect.Map:
if p.fmt.sharpV {
p.buf.WriteString(f.Type().String())
p.buf.writeString(f.Type().String())
if f.IsNil() {
p.buf.WriteString(nilParenString)
p.buf.writeString(nilParenString)
return
}
p.buf.WriteByte('{')
p.buf.writeByte('{')
} else {
p.buf.WriteString(mapString)
p.buf.writeString(mapString)
}
sorted := fmtsort.Sort(f)
for i, key := range sorted.Key {
if i > 0 {
if p.fmt.sharpV {
p.buf.WriteString(commaSpaceString)
p.buf.writeString(commaSpaceString)
} else {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
}
p.printValue(key, verb, depth+1)
p.buf.WriteByte(':')
p.buf.writeByte(':')
p.printValue(sorted.Value[i], verb, depth+1)
}
if p.fmt.sharpV {
p.buf.WriteByte('}')
p.buf.writeByte('}')
} else {
p.buf.WriteByte(']')
p.buf.writeByte(']')
}
case reflect.Struct:
if p.fmt.sharpV {
p.buf.WriteString(f.Type().String())
p.buf.writeString(f.Type().String())
}
p.buf.WriteByte('{')
p.buf.writeByte('{')
for i := 0; i < f.NumField(); i++ {
if i > 0 {
if p.fmt.sharpV {
p.buf.WriteString(commaSpaceString)
p.buf.writeString(commaSpaceString)
} else {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
}
if p.fmt.plusV || p.fmt.sharpV {
if name := f.Type().Field(i).Name; name != "" {
p.buf.WriteString(name)
p.buf.WriteByte(':')
p.buf.writeString(name)
p.buf.writeByte(':')
}
}
p.printValue(getField(f, i), verb, depth+1)
}
p.buf.WriteByte('}')
p.buf.writeByte('}')
case reflect.Interface:
value := f.Elem()
if !value.IsValid() {
if p.fmt.sharpV {
p.buf.WriteString(f.Type().String())
p.buf.WriteString(nilParenString)
p.buf.writeString(f.Type().String())
p.buf.writeString(nilParenString)
} else {
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
}
} else {
p.printValue(value, verb, depth+1)
......@@ -826,28 +826,28 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
}
}
if p.fmt.sharpV {
p.buf.WriteString(f.Type().String())
p.buf.writeString(f.Type().String())
if f.Kind() == reflect.Slice && f.IsNil() {
p.buf.WriteString(nilParenString)
p.buf.writeString(nilParenString)
return
}
p.buf.WriteByte('{')
p.buf.writeByte('{')
for i := 0; i < f.Len(); i++ {
if i > 0 {
p.buf.WriteString(commaSpaceString)
p.buf.writeString(commaSpaceString)
}
p.printValue(f.Index(i), verb, depth+1)
}
p.buf.WriteByte('}')
p.buf.writeByte('}')
} else {
p.buf.WriteByte('[')
p.buf.writeByte('[')
for i := 0; i < f.Len(); i++ {
if i > 0 {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
p.printValue(f.Index(i), verb, depth+1)
}
p.buf.WriteByte(']')
p.buf.writeByte(']')
}
case reflect.Ptr:
// pointer to array or slice or struct? ok at top level
......@@ -855,7 +855,7 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
if depth == 0 && f.Pointer() != 0 {
switch a := f.Elem(); a.Kind() {
case reflect.Array, reflect.Slice, reflect.Struct, reflect.Map:
p.buf.WriteByte('&')
p.buf.writeByte('&')
p.printValue(a, verb, depth+1)
return
}
......@@ -943,15 +943,15 @@ func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum
}
func (p *pp) badArgNum(verb rune) {
p.buf.WriteString(percentBangString)
p.buf.WriteRune(verb)
p.buf.WriteString(badIndexString)
p.buf.writeString(percentBangString)
p.buf.writeRune(verb)
p.buf.writeString(badIndexString)
}
func (p *pp) missingArg(verb rune) {
p.buf.WriteString(percentBangString)
p.buf.WriteRune(verb)
p.buf.WriteString(missingString)
p.buf.writeString(percentBangString)
p.buf.writeRune(verb)
p.buf.writeString(missingString)
}
func (p *pp) doPrintf(format string, a []interface{}) {
......@@ -967,7 +967,7 @@ formatLoop:
i++
}
if i > lasti {
p.buf.WriteString(format[lasti:i])
p.buf.writeString(format[lasti:i])
}
if i >= end {
// done processing format string
......@@ -1025,7 +1025,7 @@ formatLoop:
p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum)
if !p.fmt.widPresent {
p.buf.WriteString(badWidthString)
p.buf.writeString(badWidthString)
}
// We have a negative width, so take its value and ensure
......@@ -1059,7 +1059,7 @@ formatLoop:
p.fmt.precPresent = false
}
if !p.fmt.precPresent {
p.buf.WriteString(badPrecString)
p.buf.writeString(badPrecString)
}
afterIndex = false
} else {
......@@ -1076,7 +1076,7 @@ formatLoop:
}
if i >= end {
p.buf.WriteString(noVerbString)
p.buf.writeString(noVerbString)
break
}
......@@ -1088,7 +1088,7 @@ formatLoop:
switch {
case verb == '%': // Percent does not absorb operands and ignores f.wid and f.prec.
p.buf.WriteByte('%')
p.buf.writeByte('%')
case !p.goodArgNum:
p.badArgNum(verb)
case argNum >= len(a): // No argument left over to print for the current verb.
......@@ -1112,20 +1112,20 @@ formatLoop:
// been used and arguably OK if they're not.
if !p.reordered && argNum < len(a) {
p.fmt.clearflags()
p.buf.WriteString(extraString)
p.buf.writeString(extraString)
for i, arg := range a[argNum:] {
if i > 0 {
p.buf.WriteString(commaSpaceString)
p.buf.writeString(commaSpaceString)
}
if arg == nil {
p.buf.WriteString(nilAngleString)
p.buf.writeString(nilAngleString)
} else {
p.buf.WriteString(reflect.TypeOf(arg).String())
p.buf.WriteByte('=')
p.buf.writeString(reflect.TypeOf(arg).String())
p.buf.writeByte('=')
p.printArg(arg, 'v')
}
}
p.buf.WriteByte(')')
p.buf.writeByte(')')
}
}
......@@ -1135,7 +1135,7 @@ func (p *pp) doPrint(a []interface{}) {
isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
// Add a space between two non-string arguments.
if argNum > 0 && !isString && !prevString {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
p.printArg(arg, 'v')
prevString = isString
......@@ -1147,9 +1147,9 @@ func (p *pp) doPrint(a []interface{}) {
func (p *pp) doPrintln(a []interface{}) {
for argNum, arg := range a {
if argNum > 0 {
p.buf.WriteByte(' ')
p.buf.writeByte(' ')
}
p.printArg(arg, 'v')
}
p.buf.WriteByte('\n')
p.buf.writeByte('\n')
}
......@@ -457,7 +457,7 @@ func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
s.UnreadRune()
break
}
s.buf.WriteRune(r)
s.buf.writeRune(r)
}
return s.buf
}
......@@ -483,7 +483,7 @@ func (s *ss) consume(ok string, accept bool) bool {
}
if indexRune(ok, r) >= 0 {
if accept {
s.buf.WriteRune(r)
s.buf.writeRune(r)
}
return true
}
......@@ -850,20 +850,20 @@ func (s *ss) quotedString() string {
if r == quote {
break
}
s.buf.WriteRune(r)
s.buf.writeRune(r)
}
return string(s.buf)
case '"':
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
s.buf.WriteByte('"')
s.buf.writeByte('"')
for {
r := s.mustReadRune()
s.buf.WriteRune(r)
s.buf.writeRune(r)
if r == '\\' {
// In a legal backslash escape, no matter how long, only the character
// immediately after the escape can itself be a backslash or quote.
// Thus we only need to protect the first character after the backslash.
s.buf.WriteRune(s.mustReadRune())
s.buf.writeRune(s.mustReadRune())
} else if r == '"' {
break
}
......@@ -922,7 +922,7 @@ func (s *ss) hexString() string {
if !ok {
break
}
s.buf.WriteByte(b)
s.buf.writeByte(b)
}
if len(s.buf) == 0 {
s.errorString("no hex data for %x string")
......
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