mirror of
https://github.com/golang/go
synced 2024-11-25 09:17:57 -07:00
fmt: use rune
Lots of internal edits. Formatter and Scanner interfaces change (clients to be checked by govet). R=r CC=golang-dev https://golang.org/cl/5305045
This commit is contained in:
parent
28c06182c0
commit
4e4eca2618
@ -73,7 +73,7 @@ type C struct {
|
|||||||
|
|
||||||
type F int
|
type F int
|
||||||
|
|
||||||
func (f F) Format(s State, c int) {
|
func (f F) Format(s State, c rune) {
|
||||||
Fprintf(s, "<%c=F(%d)>", c, int(f))
|
Fprintf(s, "<%c=F(%d)>", c, int(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -546,7 +546,7 @@ func TestCountMallocs(t *testing.T) {
|
|||||||
|
|
||||||
type flagPrinter struct{}
|
type flagPrinter struct{}
|
||||||
|
|
||||||
func (*flagPrinter) Format(f State, c int) {
|
func (*flagPrinter) Format(f State, c rune) {
|
||||||
s := "%"
|
s := "%"
|
||||||
for i := 0; i < 128; i++ {
|
for i := 0; i < 128; i++ {
|
||||||
if f.Flag(i) {
|
if f.Flag(i) {
|
||||||
@ -746,7 +746,7 @@ type PanicF struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Value receiver.
|
// Value receiver.
|
||||||
func (p PanicF) Format(f State, c int) {
|
func (p PanicF) Format(f State, c rune) {
|
||||||
panic(p.message)
|
panic(p.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,8 +242,8 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If we want a quoted char for %#U, move the data up to make room.
|
// If we want a quoted char for %#U, move the data up to make room.
|
||||||
if f.unicode && f.uniQuote && a >= 0 && a <= unicode.MaxRune && unicode.IsPrint(int(a)) {
|
if f.unicode && f.uniQuote && a >= 0 && a <= unicode.MaxRune && unicode.IsPrint(rune(a)) {
|
||||||
runeWidth := utf8.RuneLen(int(a))
|
runeWidth := utf8.RuneLen(rune(a))
|
||||||
width := 1 + 1 + runeWidth + 1 // space, quote, rune, quote
|
width := 1 + 1 + runeWidth + 1 // space, quote, rune, quote
|
||||||
copy(buf[i-width:], buf[i:]) // guaranteed to have enough room.
|
copy(buf[i-width:], buf[i:]) // guaranteed to have enough room.
|
||||||
i -= width
|
i -= width
|
||||||
@ -253,7 +253,7 @@ func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
|
|||||||
j++
|
j++
|
||||||
buf[j] = '\''
|
buf[j] = '\''
|
||||||
j++
|
j++
|
||||||
utf8.EncodeRune(buf[j:], int(a))
|
utf8.EncodeRune(buf[j:], rune(a))
|
||||||
j += runeWidth
|
j += runeWidth
|
||||||
buf[j] = '\''
|
buf[j] = '\''
|
||||||
}
|
}
|
||||||
@ -400,7 +400,7 @@ func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f,
|
|||||||
func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
|
func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
|
||||||
|
|
||||||
// fmt_c64 formats a complex64 according to the verb.
|
// fmt_c64 formats a complex64 according to the verb.
|
||||||
func (f *fmt) fmt_c64(v complex64, verb int) {
|
func (f *fmt) fmt_c64(v complex64, verb rune) {
|
||||||
f.buf.WriteByte('(')
|
f.buf.WriteByte('(')
|
||||||
r := real(v)
|
r := real(v)
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
@ -426,7 +426,7 @@ func (f *fmt) fmt_c64(v complex64, verb int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fmt_c128 formats a complex128 according to the verb.
|
// fmt_c128 formats a complex128 according to the verb.
|
||||||
func (f *fmt) fmt_c128(v complex128, verb int) {
|
func (f *fmt) fmt_c128(v complex128, verb rune) {
|
||||||
f.buf.WriteByte('(')
|
f.buf.WriteByte('(')
|
||||||
r := real(v)
|
r := real(v)
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
|
@ -51,7 +51,7 @@ type State interface {
|
|||||||
// The implementation of Format may call Sprintf or Fprintf(f) etc.
|
// The implementation of Format may call Sprintf or Fprintf(f) etc.
|
||||||
// to generate its output.
|
// to generate its output.
|
||||||
type Formatter interface {
|
type Formatter interface {
|
||||||
Format(f State, c int)
|
Format(f State, c rune)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stringer is implemented by any value that has a String method,
|
// Stringer is implemented by any value that has a String method,
|
||||||
@ -159,7 +159,7 @@ func (p *pp) Flag(b int) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) add(c int) {
|
func (p *pp) add(c rune) {
|
||||||
p.buf.WriteRune(c)
|
p.buf.WriteRune(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,7 +297,7 @@ func (p *pp) unknownType(v interface{}) {
|
|||||||
p.buf.WriteByte('?')
|
p.buf.WriteByte('?')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) badVerb(verb int) {
|
func (p *pp) badVerb(verb rune) {
|
||||||
p.add('%')
|
p.add('%')
|
||||||
p.add('!')
|
p.add('!')
|
||||||
p.add(verb)
|
p.add(verb)
|
||||||
@ -317,7 +317,7 @@ func (p *pp) badVerb(verb int) {
|
|||||||
p.add(')')
|
p.add(')')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtBool(v bool, verb int) {
|
func (p *pp) fmtBool(v bool, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 't', 'v':
|
case 't', 'v':
|
||||||
p.fmt.fmt_boolean(v)
|
p.fmt.fmt_boolean(v)
|
||||||
@ -328,15 +328,15 @@ func (p *pp) fmtBool(v bool, verb int) {
|
|||||||
|
|
||||||
// fmtC formats a rune for the 'c' format.
|
// fmtC formats a rune for the 'c' format.
|
||||||
func (p *pp) fmtC(c int64) {
|
func (p *pp) fmtC(c int64) {
|
||||||
rune := int(c) // Check for overflow.
|
r := rune(c) // Check for overflow.
|
||||||
if int64(rune) != c {
|
if int64(r) != c {
|
||||||
rune = utf8.RuneError
|
r = utf8.RuneError
|
||||||
}
|
}
|
||||||
w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], rune)
|
w := utf8.EncodeRune(p.runeBuf[0:utf8.UTFMax], r)
|
||||||
p.fmt.pad(p.runeBuf[0:w])
|
p.fmt.pad(p.runeBuf[0:w])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtInt64(v int64, verb int) {
|
func (p *pp) fmtInt64(v int64, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'b':
|
case 'b':
|
||||||
p.fmt.integer(v, 2, signed, ldigits)
|
p.fmt.integer(v, 2, signed, ldigits)
|
||||||
@ -394,7 +394,7 @@ func (p *pp) fmtUnicode(v int64) {
|
|||||||
p.fmt.sharp = sharp
|
p.fmt.sharp = sharp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool) {
|
func (p *pp) fmtUint64(v uint64, verb rune, goSyntax bool) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'b':
|
case 'b':
|
||||||
p.fmt.integer(int64(v), 2, unsigned, ldigits)
|
p.fmt.integer(int64(v), 2, unsigned, ldigits)
|
||||||
@ -427,7 +427,7 @@ func (p *pp) fmtUint64(v uint64, verb int, goSyntax bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtFloat32(v float32, verb int) {
|
func (p *pp) fmtFloat32(v float32, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'b':
|
case 'b':
|
||||||
p.fmt.fmt_fb32(v)
|
p.fmt.fmt_fb32(v)
|
||||||
@ -446,7 +446,7 @@ func (p *pp) fmtFloat32(v float32, verb int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtFloat64(v float64, verb int) {
|
func (p *pp) fmtFloat64(v float64, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'b':
|
case 'b':
|
||||||
p.fmt.fmt_fb64(v)
|
p.fmt.fmt_fb64(v)
|
||||||
@ -465,7 +465,7 @@ func (p *pp) fmtFloat64(v float64, verb int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtComplex64(v complex64, verb int) {
|
func (p *pp) fmtComplex64(v complex64, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'e', 'E', 'f', 'F', 'g', 'G':
|
case 'e', 'E', 'f', 'F', 'g', 'G':
|
||||||
p.fmt.fmt_c64(v, verb)
|
p.fmt.fmt_c64(v, verb)
|
||||||
@ -476,7 +476,7 @@ func (p *pp) fmtComplex64(v complex64, verb int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtComplex128(v complex128, verb int) {
|
func (p *pp) fmtComplex128(v complex128, verb rune) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'e', 'E', 'f', 'F', 'g', 'G':
|
case 'e', 'E', 'f', 'F', 'g', 'G':
|
||||||
p.fmt.fmt_c128(v, verb)
|
p.fmt.fmt_c128(v, verb)
|
||||||
@ -487,7 +487,7 @@ func (p *pp) fmtComplex128(v complex128, verb int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtString(v string, verb int, goSyntax bool) {
|
func (p *pp) fmtString(v string, verb rune, goSyntax bool) {
|
||||||
switch verb {
|
switch verb {
|
||||||
case 'v':
|
case 'v':
|
||||||
if goSyntax {
|
if goSyntax {
|
||||||
@ -508,7 +508,7 @@ func (p *pp) fmtString(v string, verb int, goSyntax bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int) {
|
func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, depth int) {
|
||||||
if verb == 'v' || verb == 'd' {
|
if verb == 'v' || verb == 'd' {
|
||||||
if goSyntax {
|
if goSyntax {
|
||||||
p.buf.Write(bytesBytes)
|
p.buf.Write(bytesBytes)
|
||||||
@ -547,7 +547,7 @@ func (p *pp) fmtBytes(v []byte, verb int, goSyntax bool, depth int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) fmtPointer(value reflect.Value, verb int, goSyntax bool) {
|
func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
|
||||||
var u uintptr
|
var u uintptr
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
|
||||||
@ -579,7 +579,7 @@ var (
|
|||||||
uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
|
uintptrBits = reflect.TypeOf(uintptr(0)).Bits()
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *pp) catchPanic(field interface{}, verb int) {
|
func (p *pp) catchPanic(field interface{}, verb rune) {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
// If it's a nil pointer, just say "<nil>". The likeliest causes are a
|
// If it's a nil pointer, just say "<nil>". The likeliest causes are a
|
||||||
// Stringer that fails to guard against nil or a nil pointer for a
|
// Stringer that fails to guard against nil or a nil pointer for a
|
||||||
@ -604,7 +604,7 @@ func (p *pp) catchPanic(field interface{}, verb int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) handleMethods(verb int, plus, goSyntax bool, depth int) (wasString, handled bool) {
|
func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString, handled bool) {
|
||||||
// Is it a Formatter?
|
// Is it a Formatter?
|
||||||
if formatter, ok := p.field.(Formatter); ok {
|
if formatter, ok := p.field.(Formatter); ok {
|
||||||
handled = true
|
handled = true
|
||||||
@ -643,7 +643,7 @@ func (p *pp) handleMethods(verb int, plus, goSyntax bool, depth int) (wasString,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth int) (wasString bool) {
|
func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
||||||
if field == nil {
|
if field == nil {
|
||||||
if verb == 'T' || verb == 'v' {
|
if verb == 'T' || verb == 'v' {
|
||||||
p.buf.Write(nilAngleBytes)
|
p.buf.Write(nilAngleBytes)
|
||||||
@ -719,7 +719,7 @@ func (p *pp) printField(field interface{}, verb int, plus, goSyntax bool, depth
|
|||||||
}
|
}
|
||||||
|
|
||||||
// printValue is like printField but starts with a reflect value, not an interface{} value.
|
// printValue is like printField but starts with a reflect value, not an interface{} value.
|
||||||
func (p *pp) printValue(value reflect.Value, verb int, plus, goSyntax bool, depth int) (wasString bool) {
|
func (p *pp) printValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
||||||
if !value.IsValid() {
|
if !value.IsValid() {
|
||||||
if verb == 'T' || verb == 'v' {
|
if verb == 'T' || verb == 'v' {
|
||||||
p.buf.Write(nilAngleBytes)
|
p.buf.Write(nilAngleBytes)
|
||||||
@ -755,7 +755,7 @@ func (p *pp) printValue(value reflect.Value, verb int, plus, goSyntax bool, dept
|
|||||||
|
|
||||||
// printReflectValue is the fallback for both printField and printValue.
|
// printReflectValue is the fallback for both printField and printValue.
|
||||||
// It uses reflect to print the value.
|
// It uses reflect to print the value.
|
||||||
func (p *pp) printReflectValue(value reflect.Value, verb int, plus, goSyntax bool, depth int) (wasString bool) {
|
func (p *pp) printReflectValue(value reflect.Value, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
|
||||||
oldValue := p.value
|
oldValue := p.value
|
||||||
p.value = value
|
p.value = value
|
||||||
BigSwitch:
|
BigSwitch:
|
||||||
|
@ -32,7 +32,7 @@ type ScanState interface {
|
|||||||
// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
|
// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
|
||||||
// return EOF after returning the first '\n' or when reading beyond
|
// return EOF after returning the first '\n' or when reading beyond
|
||||||
// the specified width.
|
// the specified width.
|
||||||
ReadRune() (rune int, size int, err os.Error)
|
ReadRune() (r rune, size int, err os.Error)
|
||||||
// UnreadRune causes the next call to ReadRune to return the same rune.
|
// UnreadRune causes the next call to ReadRune to return the same rune.
|
||||||
UnreadRune() os.Error
|
UnreadRune() os.Error
|
||||||
// SkipSpace skips space in the input. Newlines are treated as space
|
// SkipSpace skips space in the input. Newlines are treated as space
|
||||||
@ -47,7 +47,7 @@ type ScanState interface {
|
|||||||
// EOF. The returned slice points to shared data that may be overwritten
|
// EOF. The returned slice points to shared data that may be overwritten
|
||||||
// by the next call to Token, a call to a Scan function using the ScanState
|
// by the next call to Token, a call to a Scan function using the ScanState
|
||||||
// as input, or when the calling Scan method returns.
|
// as input, or when the calling Scan method returns.
|
||||||
Token(skipSpace bool, f func(int) bool) (token []byte, err os.Error)
|
Token(skipSpace bool, f func(rune) bool) (token []byte, err os.Error)
|
||||||
// Width returns the value of the width option and whether it has been set.
|
// Width returns the value of the width option and whether it has been set.
|
||||||
// The unit is Unicode code points.
|
// The unit is Unicode code points.
|
||||||
Width() (wid int, ok bool)
|
Width() (wid int, ok bool)
|
||||||
@ -62,7 +62,7 @@ type ScanState interface {
|
|||||||
// receiver, which must be a pointer to be useful. The Scan method is called
|
// receiver, which must be a pointer to be useful. The Scan method is called
|
||||||
// for any argument to Scan, Scanf, or Scanln that implements it.
|
// for any argument to Scan, Scanf, or Scanln that implements it.
|
||||||
type Scanner interface {
|
type Scanner interface {
|
||||||
Scan(state ScanState, verb int) os.Error
|
Scan(state ScanState, verb rune) os.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan scans text read from standard input, storing successive
|
// Scan scans text read from standard input, storing successive
|
||||||
@ -149,8 +149,8 @@ const eof = -1
|
|||||||
type ss struct {
|
type ss struct {
|
||||||
rr io.RuneReader // where to read input
|
rr io.RuneReader // where to read input
|
||||||
buf bytes.Buffer // token accumulator
|
buf bytes.Buffer // token accumulator
|
||||||
peekRune int // one-rune lookahead
|
peekRune rune // one-rune lookahead
|
||||||
prevRune int // last rune returned by ReadRune
|
prevRune rune // last rune returned by ReadRune
|
||||||
count int // runes consumed so far.
|
count int // runes consumed so far.
|
||||||
atEOF bool // already read EOF
|
atEOF bool // already read EOF
|
||||||
ssave
|
ssave
|
||||||
@ -174,12 +174,12 @@ func (s *ss) Read(buf []byte) (n int, err os.Error) {
|
|||||||
return 0, os.NewError("ScanState's Read should not be called. Use ReadRune")
|
return 0, os.NewError("ScanState's Read should not be called. Use ReadRune")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ss) ReadRune() (rune int, size int, err os.Error) {
|
func (s *ss) ReadRune() (r rune, size int, err os.Error) {
|
||||||
if s.peekRune >= 0 {
|
if s.peekRune >= 0 {
|
||||||
s.count++
|
s.count++
|
||||||
rune = s.peekRune
|
r = s.peekRune
|
||||||
size = utf8.RuneLen(rune)
|
size = utf8.RuneLen(r)
|
||||||
s.prevRune = rune
|
s.prevRune = r
|
||||||
s.peekRune = -1
|
s.peekRune = -1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -188,10 +188,10 @@ func (s *ss) ReadRune() (rune int, size int, err os.Error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rune, size, err = s.rr.ReadRune()
|
r, size, err = s.rr.ReadRune()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.count++
|
s.count++
|
||||||
s.prevRune = rune
|
s.prevRune = r
|
||||||
} else if err == os.EOF {
|
} else if err == os.EOF {
|
||||||
s.atEOF = true
|
s.atEOF = true
|
||||||
}
|
}
|
||||||
@ -207,8 +207,8 @@ func (s *ss) Width() (wid int, ok bool) {
|
|||||||
|
|
||||||
// The public method returns an error; this private one panics.
|
// The public method returns an error; this private one panics.
|
||||||
// If getRune reaches EOF, the return value is EOF (-1).
|
// If getRune reaches EOF, the return value is EOF (-1).
|
||||||
func (s *ss) getRune() (rune int) {
|
func (s *ss) getRune() (r rune) {
|
||||||
rune, _, err := s.ReadRune()
|
r, _, err := s.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == os.EOF {
|
if err == os.EOF {
|
||||||
return eof
|
return eof
|
||||||
@ -221,9 +221,9 @@ func (s *ss) getRune() (rune int) {
|
|||||||
// mustReadRune turns os.EOF into a panic(io.ErrUnexpectedEOF).
|
// mustReadRune turns os.EOF into a panic(io.ErrUnexpectedEOF).
|
||||||
// It is called in cases such as string scanning where an EOF is a
|
// It is called in cases such as string scanning where an EOF is a
|
||||||
// syntax error.
|
// syntax error.
|
||||||
func (s *ss) mustReadRune() (rune int) {
|
func (s *ss) mustReadRune() (r rune) {
|
||||||
rune = s.getRune()
|
r = s.getRune()
|
||||||
if rune == eof {
|
if r == eof {
|
||||||
s.error(io.ErrUnexpectedEOF)
|
s.error(io.ErrUnexpectedEOF)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -248,7 +248,7 @@ func (s *ss) errorString(err string) {
|
|||||||
panic(scanError{os.NewError(err)})
|
panic(scanError{os.NewError(err)})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ss) Token(skipSpace bool, f func(int) bool) (tok []byte, err os.Error) {
|
func (s *ss) Token(skipSpace bool, f func(rune) bool) (tok []byte, err os.Error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
if se, ok := e.(scanError); ok {
|
if se, ok := e.(scanError); ok {
|
||||||
@ -267,7 +267,7 @@ func (s *ss) Token(skipSpace bool, f func(int) bool) (tok []byte, err os.Error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// notSpace is the default scanning function used in Token.
|
// notSpace is the default scanning function used in Token.
|
||||||
func notSpace(r int) bool {
|
func notSpace(r rune) bool {
|
||||||
return !unicode.IsSpace(r)
|
return !unicode.IsSpace(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,13 +308,13 @@ func (r *readRune) unread(buf []byte) {
|
|||||||
|
|
||||||
// ReadRune returns the next UTF-8 encoded code point from the
|
// ReadRune returns the next UTF-8 encoded code point from the
|
||||||
// io.Reader inside r.
|
// io.Reader inside r.
|
||||||
func (r *readRune) ReadRune() (rune int, size int, err os.Error) {
|
func (r *readRune) ReadRune() (rr rune, size int, err os.Error) {
|
||||||
r.buf[0], err = r.readByte()
|
r.buf[0], err = r.readByte()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
|
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
|
||||||
rune = int(r.buf[0])
|
rr = rune(r.buf[0])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var n int
|
var n int
|
||||||
@ -328,7 +328,7 @@ func (r *readRune) ReadRune() (rune int, size int, err os.Error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rune, size = utf8.DecodeRune(r.buf[0:n])
|
rr, size = utf8.DecodeRune(r.buf[0:n])
|
||||||
if size < n { // an error
|
if size < n { // an error
|
||||||
r.unread(r.buf[size:n])
|
r.unread(r.buf[size:n])
|
||||||
}
|
}
|
||||||
@ -387,11 +387,11 @@ func (s *ss) free(old ssave) {
|
|||||||
// skipSpace skips spaces and maybe newlines.
|
// skipSpace skips spaces and maybe newlines.
|
||||||
func (s *ss) skipSpace(stopAtNewline bool) {
|
func (s *ss) skipSpace(stopAtNewline bool) {
|
||||||
for {
|
for {
|
||||||
rune := s.getRune()
|
r := s.getRune()
|
||||||
if rune == eof {
|
if r == eof {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if rune == '\n' {
|
if r == '\n' {
|
||||||
if stopAtNewline {
|
if stopAtNewline {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -401,7 +401,7 @@ func (s *ss) skipSpace(stopAtNewline bool) {
|
|||||||
s.errorString("unexpected newline")
|
s.errorString("unexpected newline")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !unicode.IsSpace(rune) {
|
if !unicode.IsSpace(r) {
|
||||||
s.UnreadRune()
|
s.UnreadRune()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -411,21 +411,21 @@ func (s *ss) skipSpace(stopAtNewline bool) {
|
|||||||
// token returns the next space-delimited string from the input. It
|
// token returns the next space-delimited string from the input. It
|
||||||
// skips white space. For Scanln, it stops at newlines. For Scan,
|
// skips white space. For Scanln, it stops at newlines. For Scan,
|
||||||
// newlines are treated as spaces.
|
// newlines are treated as spaces.
|
||||||
func (s *ss) token(skipSpace bool, f func(int) bool) []byte {
|
func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
|
||||||
if skipSpace {
|
if skipSpace {
|
||||||
s.skipSpace(false)
|
s.skipSpace(false)
|
||||||
}
|
}
|
||||||
// read until white space or newline
|
// read until white space or newline
|
||||||
for {
|
for {
|
||||||
rune := s.getRune()
|
r := s.getRune()
|
||||||
if rune == eof {
|
if r == eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if !f(rune) {
|
if !f(r) {
|
||||||
s.UnreadRune()
|
s.UnreadRune()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
s.buf.WriteRune(rune)
|
s.buf.WriteRune(r)
|
||||||
}
|
}
|
||||||
return s.buf.Bytes()
|
return s.buf.Bytes()
|
||||||
}
|
}
|
||||||
@ -441,17 +441,17 @@ var boolError = os.NewError("syntax error scanning boolean")
|
|||||||
// consume reads the next rune in the input and reports whether it is in the ok string.
|
// consume reads the next rune in the input and reports whether it is in the ok string.
|
||||||
// If accept is true, it puts the character into the input token.
|
// If accept is true, it puts the character into the input token.
|
||||||
func (s *ss) consume(ok string, accept bool) bool {
|
func (s *ss) consume(ok string, accept bool) bool {
|
||||||
rune := s.getRune()
|
r := s.getRune()
|
||||||
if rune == eof {
|
if r == eof {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if strings.IndexRune(ok, rune) >= 0 {
|
if strings.IndexRune(ok, r) >= 0 {
|
||||||
if accept {
|
if accept {
|
||||||
s.buf.WriteRune(rune)
|
s.buf.WriteRune(r)
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if rune != eof && accept {
|
if r != eof && accept {
|
||||||
s.UnreadRune()
|
s.UnreadRune()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -459,16 +459,16 @@ func (s *ss) consume(ok string, accept bool) bool {
|
|||||||
|
|
||||||
// peek reports whether the next character is in the ok string, without consuming it.
|
// peek reports whether the next character is in the ok string, without consuming it.
|
||||||
func (s *ss) peek(ok string) bool {
|
func (s *ss) peek(ok string) bool {
|
||||||
rune := s.getRune()
|
r := s.getRune()
|
||||||
if rune != eof {
|
if r != eof {
|
||||||
s.UnreadRune()
|
s.UnreadRune()
|
||||||
}
|
}
|
||||||
return strings.IndexRune(ok, rune) >= 0
|
return strings.IndexRune(ok, r) >= 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ss) notEOF() {
|
func (s *ss) notEOF() {
|
||||||
// Guarantee there is data to be read.
|
// Guarantee there is data to be read.
|
||||||
if rune := s.getRune(); rune == eof {
|
if r := s.getRune(); r == eof {
|
||||||
panic(os.EOF)
|
panic(os.EOF)
|
||||||
}
|
}
|
||||||
s.UnreadRune()
|
s.UnreadRune()
|
||||||
@ -481,7 +481,7 @@ func (s *ss) accept(ok string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
|
// okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
|
||||||
func (s *ss) okVerb(verb int, okVerbs, typ string) bool {
|
func (s *ss) okVerb(verb rune, okVerbs, typ string) bool {
|
||||||
for _, v := range okVerbs {
|
for _, v := range okVerbs {
|
||||||
if v == verb {
|
if v == verb {
|
||||||
return true
|
return true
|
||||||
@ -492,7 +492,7 @@ func (s *ss) okVerb(verb int, okVerbs, typ string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// scanBool returns the value of the boolean represented by the next token.
|
// scanBool returns the value of the boolean represented by the next token.
|
||||||
func (s *ss) scanBool(verb int) bool {
|
func (s *ss) scanBool(verb rune) bool {
|
||||||
s.skipSpace(false)
|
s.skipSpace(false)
|
||||||
s.notEOF()
|
s.notEOF()
|
||||||
if !s.okVerb(verb, "tv", "boolean") {
|
if !s.okVerb(verb, "tv", "boolean") {
|
||||||
@ -530,7 +530,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// getBase returns the numeric base represented by the verb and its digit string.
|
// getBase returns the numeric base represented by the verb and its digit string.
|
||||||
func (s *ss) getBase(verb int) (base int, digits string) {
|
func (s *ss) getBase(verb rune) (base int, digits string) {
|
||||||
s.okVerb(verb, "bdoUxXv", "integer") // sets s.err
|
s.okVerb(verb, "bdoUxXv", "integer") // sets s.err
|
||||||
base = 10
|
base = 10
|
||||||
digits = decimalDigits
|
digits = decimalDigits
|
||||||
@ -564,13 +564,13 @@ func (s *ss) scanNumber(digits string, haveDigits bool) string {
|
|||||||
// scanRune returns the next rune value in the input.
|
// scanRune returns the next rune value in the input.
|
||||||
func (s *ss) scanRune(bitSize int) int64 {
|
func (s *ss) scanRune(bitSize int) int64 {
|
||||||
s.notEOF()
|
s.notEOF()
|
||||||
rune := int64(s.getRune())
|
r := int64(s.getRune())
|
||||||
n := uint(bitSize)
|
n := uint(bitSize)
|
||||||
x := (rune << (64 - n)) >> (64 - n)
|
x := (r << (64 - n)) >> (64 - n)
|
||||||
if x != rune {
|
if x != r {
|
||||||
s.errorString("overflow on character value " + string(rune))
|
s.errorString("overflow on character value " + string(r))
|
||||||
}
|
}
|
||||||
return rune
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// scanBasePrefix reports whether the integer begins with a 0 or 0x,
|
// scanBasePrefix reports whether the integer begins with a 0 or 0x,
|
||||||
@ -593,7 +593,7 @@ func (s *ss) scanBasePrefix() (base int, digits string, found bool) {
|
|||||||
|
|
||||||
// scanInt returns the value of the integer represented by the next
|
// scanInt returns the value of the integer represented by the next
|
||||||
// token, checking for overflow. Any error is stored in s.err.
|
// token, checking for overflow. Any error is stored in s.err.
|
||||||
func (s *ss) scanInt(verb int, bitSize int) int64 {
|
func (s *ss) scanInt(verb rune, bitSize int) int64 {
|
||||||
if verb == 'c' {
|
if verb == 'c' {
|
||||||
return s.scanRune(bitSize)
|
return s.scanRune(bitSize)
|
||||||
}
|
}
|
||||||
@ -626,7 +626,7 @@ func (s *ss) scanInt(verb int, bitSize int) int64 {
|
|||||||
|
|
||||||
// scanUint returns the value of the unsigned integer represented
|
// scanUint returns the value of the unsigned integer represented
|
||||||
// by the next token, checking for overflow. Any error is stored in s.err.
|
// by the next token, checking for overflow. Any error is stored in s.err.
|
||||||
func (s *ss) scanUint(verb int, bitSize int) uint64 {
|
func (s *ss) scanUint(verb rune, bitSize int) uint64 {
|
||||||
if verb == 'c' {
|
if verb == 'c' {
|
||||||
return uint64(s.scanRune(bitSize))
|
return uint64(s.scanRune(bitSize))
|
||||||
}
|
}
|
||||||
@ -747,7 +747,7 @@ func (s *ss) convertFloat(str string, n int) float64 {
|
|||||||
// The atof argument is a type-specific reader for the underlying type.
|
// The atof argument is a type-specific reader for the underlying type.
|
||||||
// If we're reading complex64, atof will parse float32s and convert them
|
// If we're reading complex64, atof will parse float32s and convert them
|
||||||
// to float64's to avoid reproducing this code for each complex type.
|
// to float64's to avoid reproducing this code for each complex type.
|
||||||
func (s *ss) scanComplex(verb int, n int) complex128 {
|
func (s *ss) scanComplex(verb rune, n int) complex128 {
|
||||||
if !s.okVerb(verb, floatVerbs, "complex") {
|
if !s.okVerb(verb, floatVerbs, "complex") {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -761,7 +761,7 @@ func (s *ss) scanComplex(verb int, n int) complex128 {
|
|||||||
|
|
||||||
// convertString returns the string represented by the next input characters.
|
// convertString returns the string represented by the next input characters.
|
||||||
// The format of the input is determined by the verb.
|
// The format of the input is determined by the verb.
|
||||||
func (s *ss) convertString(verb int) (str string) {
|
func (s *ss) convertString(verb rune) (str string) {
|
||||||
if !s.okVerb(verb, "svqx", "string") {
|
if !s.okVerb(verb, "svqx", "string") {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -786,26 +786,26 @@ func (s *ss) quotedString() string {
|
|||||||
case '`':
|
case '`':
|
||||||
// Back-quoted: Anything goes until EOF or back quote.
|
// Back-quoted: Anything goes until EOF or back quote.
|
||||||
for {
|
for {
|
||||||
rune := s.mustReadRune()
|
r := s.mustReadRune()
|
||||||
if rune == quote {
|
if r == quote {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
s.buf.WriteRune(rune)
|
s.buf.WriteRune(r)
|
||||||
}
|
}
|
||||||
return s.buf.String()
|
return s.buf.String()
|
||||||
case '"':
|
case '"':
|
||||||
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
|
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
|
||||||
s.buf.WriteRune(quote)
|
s.buf.WriteRune(quote)
|
||||||
for {
|
for {
|
||||||
rune := s.mustReadRune()
|
r := s.mustReadRune()
|
||||||
s.buf.WriteRune(rune)
|
s.buf.WriteRune(r)
|
||||||
if rune == '\\' {
|
if r == '\\' {
|
||||||
// In a legal backslash escape, no matter how long, only the character
|
// In a legal backslash escape, no matter how long, only the character
|
||||||
// immediately after the escape can itself be a backslash or quote.
|
// immediately after the escape can itself be a backslash or quote.
|
||||||
// Thus we only need to protect the first character after the backslash.
|
// Thus we only need to protect the first character after the backslash.
|
||||||
rune := s.mustReadRune()
|
r := s.mustReadRune()
|
||||||
s.buf.WriteRune(rune)
|
s.buf.WriteRune(r)
|
||||||
} else if rune == '"' {
|
} else if r == '"' {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -821,7 +821,8 @@ func (s *ss) quotedString() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// hexDigit returns the value of the hexadecimal digit
|
// hexDigit returns the value of the hexadecimal digit
|
||||||
func (s *ss) hexDigit(digit int) int {
|
func (s *ss) hexDigit(d rune) int {
|
||||||
|
digit := int(d)
|
||||||
switch digit {
|
switch digit {
|
||||||
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
return digit - '0'
|
return digit - '0'
|
||||||
@ -871,7 +872,7 @@ const floatVerbs = "beEfFgGv"
|
|||||||
const hugeWid = 1 << 30
|
const hugeWid = 1 << 30
|
||||||
|
|
||||||
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
||||||
func (s *ss) scanOne(verb int, field interface{}) {
|
func (s *ss) scanOne(verb rune, field interface{}) {
|
||||||
s.buf.Reset()
|
s.buf.Reset()
|
||||||
var err os.Error
|
var err os.Error
|
||||||
// If the parameter has its own Scan method, use that.
|
// If the parameter has its own Scan method, use that.
|
||||||
@ -997,11 +998,11 @@ func (s *ss) doScan(a []interface{}) (numProcessed int, err os.Error) {
|
|||||||
// Check for newline if required.
|
// Check for newline if required.
|
||||||
if !s.nlIsSpace {
|
if !s.nlIsSpace {
|
||||||
for {
|
for {
|
||||||
rune := s.getRune()
|
r := s.getRune()
|
||||||
if rune == '\n' || rune == eof {
|
if r == '\n' || r == eof {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if !unicode.IsSpace(rune) {
|
if !unicode.IsSpace(r) {
|
||||||
s.errorString("Scan: expected newline")
|
s.errorString("Scan: expected newline")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -87,8 +87,8 @@ type FloatTest struct {
|
|||||||
// Xs accepts any non-empty run of the verb character
|
// Xs accepts any non-empty run of the verb character
|
||||||
type Xs string
|
type Xs string
|
||||||
|
|
||||||
func (x *Xs) Scan(state ScanState, verb int) os.Error {
|
func (x *Xs) Scan(state ScanState, verb rune) os.Error {
|
||||||
tok, err := state.Token(true, func(r int) bool { return r == verb })
|
tok, err := state.Token(true, func(r rune) bool { return r == verb })
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -109,7 +109,7 @@ type IntString struct {
|
|||||||
s string
|
s string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *IntString) Scan(state ScanState, verb int) os.Error {
|
func (s *IntString) Scan(state ScanState, verb rune) os.Error {
|
||||||
if _, err := Fscan(state, &s.i); err != nil {
|
if _, err := Fscan(state, &s.i); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -749,8 +749,8 @@ type TwoLines string
|
|||||||
|
|
||||||
// Attempt to read two lines into the object. Scanln should prevent this
|
// Attempt to read two lines into the object. Scanln should prevent this
|
||||||
// because it stops at newline; Scan and Scanf should be fine.
|
// because it stops at newline; Scan and Scanf should be fine.
|
||||||
func (t *TwoLines) Scan(state ScanState, verb int) os.Error {
|
func (t *TwoLines) Scan(state ScanState, verb rune) os.Error {
|
||||||
chars := make([]int, 0, 100)
|
chars := make([]rune, 0, 100)
|
||||||
for nlCount := 0; nlCount < 2; {
|
for nlCount := 0; nlCount < 2; {
|
||||||
c, _, err := state.ReadRune()
|
c, _, err := state.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -812,7 +812,7 @@ type RecursiveInt struct {
|
|||||||
next *RecursiveInt
|
next *RecursiveInt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RecursiveInt) Scan(state ScanState, verb int) (err os.Error) {
|
func (r *RecursiveInt) Scan(state ScanState, verb rune) (err os.Error) {
|
||||||
_, err = Fscan(state, &r.i)
|
_, err = Fscan(state, &r.i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -838,8 +838,7 @@ func scanInts(r *RecursiveInt, b *bytes.Buffer) (err os.Error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var c int
|
c, _, err := b.ReadRune()
|
||||||
c, _, err = b.ReadRune()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == os.EOF {
|
if err == os.EOF {
|
||||||
err = nil
|
err = nil
|
||||||
|
Loading…
Reference in New Issue
Block a user