summaryrefslogtreecommitdiff
path: root/libgo/go/fmt/scan.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/fmt/scan.go')
-rw-r--r--libgo/go/fmt/scan.go67
1 files changed, 32 insertions, 35 deletions
diff --git a/libgo/go/fmt/scan.go b/libgo/go/fmt/scan.go
index bf888c4d88c..5b1be5891b8 100644
--- a/libgo/go/fmt/scan.go
+++ b/libgo/go/fmt/scan.go
@@ -168,12 +168,12 @@ type ss struct {
// ssave holds the parts of ss that need to be
// saved and restored on recursive scans.
type ssave struct {
- validSave bool // is or was a part of an actual ss.
- nlIsEnd bool // whether newline terminates scan
- nlIsSpace bool // whether newline counts as white space
- fieldLimit int // max value of ss.count for this field; fieldLimit <= limit
- limit int // max value of ss.count.
- maxWid int // width of this field.
+ validSave bool // is or was a part of an actual ss.
+ nlIsEnd bool // whether newline terminates scan
+ nlIsSpace bool // whether newline counts as white space
+ argLimit int // max value of ss.count for this arg; argLimit <= limit
+ limit int // max value of ss.count.
+ maxWid int // width of this arg.
}
// The Read method is only in ScanState so that ScanState
@@ -192,7 +192,7 @@ func (s *ss) ReadRune() (r rune, size int, err error) {
s.peekRune = -1
return
}
- if s.atEOF || s.nlIsEnd && s.prevRune == '\n' || s.count >= s.fieldLimit {
+ if s.atEOF || s.nlIsEnd && s.prevRune == '\n' || s.count >= s.argLimit {
err = io.EOF
return
}
@@ -389,7 +389,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
s, ok := r.(*ss)
if ok {
old = s.ssave
- s.limit = s.fieldLimit
+ s.limit = s.argLimit
s.nlIsEnd = nlIsEnd || s.nlIsEnd
s.nlIsSpace = nlIsSpace
return
@@ -407,7 +407,7 @@ func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
s.peekRune = -1
s.atEOF = false
s.limit = hugeWid
- s.fieldLimit = hugeWid
+ s.argLimit = hugeWid
s.maxWid = hugeWid
s.validSave = true
s.count = 0
@@ -437,6 +437,9 @@ func (s *ss) skipSpace(stopAtNewline bool) {
if r == eof {
return
}
+ if r == '\r' && s.peek("\n") {
+ continue
+ }
if r == '\n' {
if stopAtNewline {
break
@@ -476,11 +479,6 @@ func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
return s.buf
}
-// typeError indicates that the type of the operand did not match the format
-func (s *ss) typeError(field interface{}, expected string) {
- s.errorString("expected field of type pointer to " + expected + "; found " + reflect.TypeOf(field).String())
-}
-
var complexError = errors.New("syntax error scanning complex number")
var boolError = errors.New("syntax error scanning boolean")
@@ -781,7 +779,7 @@ func (s *ss) convertFloat(str string, n int) float64 {
}
s.error(err)
}
- n, err := strconv.Atoi(str[p+1:])
+ m, err := strconv.Atoi(str[p+1:])
if err != nil {
// Put full string into error.
if e, ok := err.(*strconv.NumError); ok {
@@ -789,7 +787,7 @@ func (s *ss) convertFloat(str string, n int) float64 {
}
s.error(err)
}
- return math.Ldexp(f, n)
+ return math.Ldexp(f, m)
}
f, err := strconv.ParseFloat(str, n)
if err != nil {
@@ -858,8 +856,7 @@ func (s *ss) quotedString() string {
// 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.
- r := s.mustReadRune()
- s.buf.WriteRune(r)
+ s.buf.WriteRune(s.mustReadRune())
} else if r == '"' {
break
}
@@ -886,7 +883,7 @@ func (s *ss) hexDigit(d rune) int {
case 'A', 'B', 'C', 'D', 'E', 'F':
return 10 + digit - 'A'
}
- s.errorString("Scan: illegal hex digit")
+ s.errorString("illegal hex digit")
return 0
}
@@ -916,7 +913,7 @@ func (s *ss) hexString() string {
s.buf.WriteByte(b)
}
if len(s.buf) == 0 {
- s.errorString("Scan: no hex data for %x string")
+ s.errorString("no hex data for %x string")
return ""
}
return string(s.buf)
@@ -927,11 +924,11 @@ const floatVerbs = "beEfFgGv"
const hugeWid = 1 << 30
// scanOne scans a single value, deriving the scanner from the type of the argument.
-func (s *ss) scanOne(verb rune, field interface{}) {
+func (s *ss) scanOne(verb rune, arg interface{}) {
s.buf = s.buf[:0]
var err error
// If the parameter has its own Scan method, use that.
- if v, ok := field.(Scanner); ok {
+ if v, ok := arg.(Scanner); ok {
err = v.Scan(s, verb)
if err != nil {
if err == io.EOF {
@@ -942,7 +939,7 @@ func (s *ss) scanOne(verb rune, field interface{}) {
return
}
- switch v := field.(type) {
+ switch v := arg.(type) {
case *bool:
*v = s.scanBool(verb)
case *complex64:
@@ -995,7 +992,7 @@ func (s *ss) scanOne(verb rune, field interface{}) {
val := reflect.ValueOf(v)
ptr := val
if ptr.Kind() != reflect.Ptr {
- s.errorString("Scan: type not a pointer: " + val.Type().String())
+ s.errorString("type not a pointer: " + val.Type().String())
return
}
switch v := ptr.Elem(); v.Kind() {
@@ -1011,7 +1008,7 @@ func (s *ss) scanOne(verb rune, field interface{}) {
// For now, can only handle (renamed) []byte.
typ := v.Type()
if typ.Elem().Kind() != reflect.Uint8 {
- s.errorString("Scan: can't handle type: " + val.Type().String())
+ s.errorString("can't scan type: " + val.Type().String())
}
str := s.convertString(verb)
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
@@ -1025,7 +1022,7 @@ func (s *ss) scanOne(verb rune, field interface{}) {
case reflect.Complex64, reflect.Complex128:
v.SetComplex(s.scanComplex(verb, v.Type().Bits()))
default:
- s.errorString("Scan: can't handle type: " + val.Type().String())
+ s.errorString("can't scan type: " + val.Type().String())
}
}
}
@@ -1046,8 +1043,8 @@ func errorHandler(errp *error) {
// doScan does the real work for scanning without a format string.
func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
defer errorHandler(&err)
- for _, field := range a {
- s.scanOne('v', field)
+ for _, arg := range a {
+ s.scanOne('v', arg)
numProcessed++
}
// Check for newline if required.
@@ -1058,7 +1055,7 @@ func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
break
}
if !isSpace(r) {
- s.errorString("Scan: expected newline")
+ s.errorString("expected newline")
break
}
}
@@ -1144,9 +1141,9 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro
if !widPresent {
s.maxWid = hugeWid
}
- s.fieldLimit = s.limit
- if f := s.count + s.maxWid; f < s.fieldLimit {
- s.fieldLimit = f
+ s.argLimit = s.limit
+ if f := s.count + s.maxWid; f < s.argLimit {
+ s.argLimit = f
}
c, w := utf8.DecodeRuneInString(format[i:])
@@ -1156,11 +1153,11 @@ func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err erro
s.errorString("too few operands for format %" + format[i-w:])
break
}
- field := a[numProcessed]
+ arg := a[numProcessed]
- s.scanOne(c, field)
+ s.scanOne(c, arg)
numProcessed++
- s.fieldLimit = s.limit
+ s.argLimit = s.limit
}
if numProcessed < len(a) {
s.errorString("too many operands")