2010-05-25 22:02:35 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2011-02-02 13:38:48 -07:00
|
|
|
"math"
|
2010-05-25 22:02:35 -06:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2010-06-02 15:58:31 -06:00
|
|
|
"strings"
|
2010-05-25 22:02:35 -06:00
|
|
|
"unicode"
|
|
|
|
"utf8"
|
|
|
|
)
|
|
|
|
|
2011-02-02 16:00:14 -07:00
|
|
|
// runeUnreader is the interface to something that can unread runes.
|
2010-09-13 19:06:17 -06:00
|
|
|
// If the object provided to Scan does not satisfy this interface,
|
|
|
|
// a local buffer will be used to back up the input, but its contents
|
|
|
|
// will be lost when Scan returns.
|
2011-02-02 16:00:14 -07:00
|
|
|
type runeUnreader interface {
|
2010-09-13 19:06:17 -06:00
|
|
|
UnreadRune() os.Error
|
|
|
|
}
|
|
|
|
|
2010-05-26 19:15:09 -06:00
|
|
|
// ScanState represents the scanner state passed to custom scanners.
|
|
|
|
// Scanners may do rune-at-a-time scanning or ask the ScanState
|
|
|
|
// to discover the next space-delimited token.
|
|
|
|
type ScanState interface {
|
2011-03-02 11:54:23 -07:00
|
|
|
// ReadRune reads the next rune (Unicode code point) from the input.
|
|
|
|
// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
|
|
|
|
// return EOF after returning the first '\n' or when reading beyond
|
|
|
|
// the specified width.
|
|
|
|
ReadRune() (rune int, size int, err os.Error)
|
|
|
|
// UnreadRune causes the next call to ReadRune to return the same rune.
|
|
|
|
UnreadRune() os.Error
|
2011-03-09 11:01:47 -07:00
|
|
|
// Token skips space in the input if skipSpace is true, then returns the
|
|
|
|
// run of Unicode code points c satisfying f(c). If f is nil,
|
|
|
|
// !unicode.IsSpace(c) is used; that is, the token will hold non-space
|
|
|
|
// characters. Newlines are treated as space unless the scan operation
|
|
|
|
// is Scanln, Fscanln or Sscanln, in which case a newline is treated as
|
|
|
|
// 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
|
|
|
|
// as input, or when the calling Scan method returns.
|
|
|
|
Token(skipSpace bool, f func(int) bool) (token []byte, err os.Error)
|
2011-03-02 11:54:23 -07:00
|
|
|
// Width returns the value of the width option and whether it has been set.
|
|
|
|
// The unit is Unicode code points.
|
|
|
|
Width() (wid int, ok bool)
|
|
|
|
// Because ReadRune is implemented by the interface, Read should never be
|
|
|
|
// called by the scanning routines and a valid implementation of
|
|
|
|
// ScanState may choose always to return an error from Read.
|
|
|
|
Read(buf []byte) (n int, err os.Error)
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scanner is implemented by any value that has a Scan method, which scans
|
|
|
|
// the input for the representation of a value and stores the result in the
|
|
|
|
// receiver, which must be a pointer to be useful. The Scan method is called
|
2011-02-24 11:14:19 -07:00
|
|
|
// for any argument to Scan, Scanf, or Scanln that implements it.
|
2010-05-26 19:15:09 -06:00
|
|
|
type Scanner interface {
|
2010-06-03 17:06:41 -06:00
|
|
|
Scan(state ScanState, verb int) os.Error
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scan scans text read from standard input, storing successive
|
|
|
|
// space-separated values into successive arguments. Newlines count
|
|
|
|
// as space. It returns the number of items successfully scanned.
|
|
|
|
// If that is less than the number of arguments, err will report why.
|
2010-05-31 11:56:58 -06:00
|
|
|
func Scan(a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscan(os.Stdin, a...)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scanln is similar to Scan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
2010-05-31 11:56:58 -06:00
|
|
|
func Scanln(a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanln(os.Stdin, a...)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scanf scans text read from standard input, storing successive
|
|
|
|
// space-separated values into successive arguments as determined by
|
|
|
|
// the format. It returns the number of items successfully scanned.
|
|
|
|
func Scanf(format string, a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanf(os.Stdin, format, a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sscan scans the argument string, storing successive space-separated
|
|
|
|
// values into successive arguments. Newlines count as space. It
|
|
|
|
// returns the number of items successfully scanned. If that is less
|
|
|
|
// than the number of arguments, err will report why.
|
|
|
|
func Sscan(str string, a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscan(strings.NewReader(str), a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sscanln is similar to Sscan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
|
|
|
func Sscanln(str string, a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanln(strings.NewReader(str), a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sscanf scans the argument string, storing successive space-separated
|
|
|
|
// values into successive arguments as determined by the format. It
|
|
|
|
// returns the number of items successfully parsed.
|
|
|
|
func Sscanf(str string, format string, a ...interface{}) (n int, err os.Error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanf(strings.NewReader(str), format, a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fscan scans text read from r, storing successive space-separated
|
|
|
|
// values into successive arguments. Newlines count as space. It
|
|
|
|
// returns the number of items successfully scanned. If that is less
|
|
|
|
// than the number of arguments, err will report why.
|
2010-05-31 11:56:58 -06:00
|
|
|
func Fscan(r io.Reader, a ...interface{}) (n int, err os.Error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, true, false)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScan(a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Fscanln is similar to Fscan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
2010-05-31 11:56:58 -06:00
|
|
|
func Fscanln(r io.Reader, a ...interface{}) (n int, err os.Error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, false, true)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScan(a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Fscanf scans text read from r, storing successive space-separated
|
|
|
|
// values into successive arguments as determined by the format. It
|
|
|
|
// returns the number of items successfully parsed.
|
|
|
|
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err os.Error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, false, false)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScanf(format, a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanError represents an error generated by the scanning software.
|
|
|
|
// It's used as a unique signature to identify such errors when recovering.
|
|
|
|
type scanError struct {
|
|
|
|
err os.Error
|
|
|
|
}
|
|
|
|
|
2011-03-09 17:53:32 -07:00
|
|
|
const eof = -1
|
2010-06-24 16:24:25 -06:00
|
|
|
|
2010-05-26 19:15:09 -06:00
|
|
|
// ss is the internal implementation of ScanState.
|
2010-05-25 22:02:35 -06:00
|
|
|
type ss struct {
|
2011-03-03 11:43:29 -07:00
|
|
|
rr io.RuneReader // where to read input
|
|
|
|
buf bytes.Buffer // token accumulator
|
|
|
|
peekRune int // one-rune lookahead
|
|
|
|
prevRune int // last rune returned by ReadRune
|
|
|
|
count int // runes consumed so far.
|
|
|
|
atEOF bool // already read EOF
|
|
|
|
ssave
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2011-03-02 11:54:23 -07:00
|
|
|
// The Read method is only in ScanState so that ScanState
|
|
|
|
// satisfies io.Reader. It will never be called when used as
|
|
|
|
// intended, so there is no need to make it actually work.
|
|
|
|
func (s *ss) Read(buf []byte) (n int, err os.Error) {
|
|
|
|
return 0, os.ErrorString("ScanState's Read should not be called. Use ReadRune")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ss) ReadRune() (rune int, size int, err os.Error) {
|
2010-05-26 19:15:09 -06:00
|
|
|
if s.peekRune >= 0 {
|
2011-03-03 11:43:29 -07:00
|
|
|
s.count++
|
2010-05-26 19:15:09 -06:00
|
|
|
rune = s.peekRune
|
2011-03-02 11:54:23 -07:00
|
|
|
size = utf8.RuneLen(rune)
|
2010-09-13 19:06:17 -06:00
|
|
|
s.prevRune = rune
|
2010-05-26 19:15:09 -06:00
|
|
|
s.peekRune = -1
|
|
|
|
return
|
|
|
|
}
|
2011-03-03 11:43:29 -07:00
|
|
|
if s.atEOF || s.nlIsEnd && s.prevRune == '\n' || s.count >= s.fieldLimit {
|
2011-02-24 11:14:19 -07:00
|
|
|
err = os.EOF
|
|
|
|
return
|
|
|
|
}
|
2011-03-02 11:54:23 -07:00
|
|
|
|
|
|
|
rune, size, err = s.rr.ReadRune()
|
2010-09-13 19:06:17 -06:00
|
|
|
if err == nil {
|
2011-03-03 11:43:29 -07:00
|
|
|
s.count++
|
2010-09-13 19:06:17 -06:00
|
|
|
s.prevRune = rune
|
2011-03-02 11:54:23 -07:00
|
|
|
} else if err == os.EOF {
|
|
|
|
s.atEOF = true
|
2010-09-13 19:06:17 -06:00
|
|
|
}
|
2010-05-26 19:15:09 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
func (s *ss) Width() (wid int, ok bool) {
|
2011-03-02 11:54:23 -07:00
|
|
|
if s.maxWid == hugeWid {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return s.maxWid, true
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// The public method returns an error; this private one panics.
|
|
|
|
// If getRune reaches EOF, the return value is EOF (-1).
|
|
|
|
func (s *ss) getRune() (rune int) {
|
2011-03-02 11:54:23 -07:00
|
|
|
rune, _, err := s.ReadRune()
|
|
|
|
if err != nil {
|
2010-06-01 16:21:21 -06:00
|
|
|
if err == os.EOF {
|
2011-03-09 17:53:32 -07:00
|
|
|
return eof
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-02 11:54:23 -07:00
|
|
|
// mustReadRune turns os.EOF into a panic(io.ErrUnexpectedEOF).
|
2010-06-01 16:21:21 -06:00
|
|
|
// It is called in cases such as string scanning where an EOF is a
|
|
|
|
// syntax error.
|
2011-03-02 11:54:23 -07:00
|
|
|
func (s *ss) mustReadRune() (rune int) {
|
|
|
|
rune = s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune == eof {
|
2010-06-24 16:24:25 -06:00
|
|
|
s.error(io.ErrUnexpectedEOF)
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-02 11:54:23 -07:00
|
|
|
func (s *ss) UnreadRune() os.Error {
|
2011-05-26 12:54:10 -06:00
|
|
|
// Don't use strings.Reader.UnreadRune for now - appears to cause a problem.
|
|
|
|
// TODO(r, gri): Fix this and remove code between --- lines!
|
|
|
|
// ---
|
|
|
|
if _, ok := s.rr.(*strings.Reader); ok {
|
|
|
|
s.peekRune = s.prevRune
|
|
|
|
s.count--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// ---
|
2011-02-02 16:00:14 -07:00
|
|
|
if u, ok := s.rr.(runeUnreader); ok {
|
2010-09-13 19:06:17 -06:00
|
|
|
u.UnreadRune()
|
|
|
|
} else {
|
|
|
|
s.peekRune = s.prevRune
|
|
|
|
}
|
2011-03-03 11:43:29 -07:00
|
|
|
s.count--
|
2011-03-02 11:54:23 -07:00
|
|
|
return nil
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) error(err os.Error) {
|
|
|
|
panic(scanError{err})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ss) errorString(err string) {
|
|
|
|
panic(scanError{os.ErrorString(err)})
|
|
|
|
}
|
|
|
|
|
2011-03-09 11:01:47 -07:00
|
|
|
func (s *ss) Token(skipSpace bool, f func(int) bool) (tok []byte, err os.Error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if se, ok := e.(scanError); ok {
|
|
|
|
err = se.err
|
|
|
|
} else {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2011-03-09 11:01:47 -07:00
|
|
|
if f == nil {
|
|
|
|
f = notSpace
|
|
|
|
}
|
|
|
|
s.buf.Reset()
|
|
|
|
tok = s.token(skipSpace, f)
|
2010-05-26 19:15:09 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-09 11:01:47 -07:00
|
|
|
// notSpace is the default scanning function used in Token.
|
|
|
|
func notSpace(r int) bool {
|
|
|
|
return !unicode.IsSpace(r)
|
|
|
|
}
|
|
|
|
|
2010-05-25 22:02:35 -06:00
|
|
|
// readRune is a structure to enable reading UTF-8 encoded code points
|
|
|
|
// from an io.Reader. It is used if the Reader given to the scanner does
|
2011-02-02 16:00:14 -07:00
|
|
|
// not already implement io.RuneReader.
|
2010-05-25 22:02:35 -06:00
|
|
|
type readRune struct {
|
2010-06-15 18:41:11 -06:00
|
|
|
reader io.Reader
|
|
|
|
buf [utf8.UTFMax]byte // used only inside ReadRune
|
|
|
|
pending int // number of bytes in pendBuf; only >0 for bad UTF-8
|
|
|
|
pendBuf [utf8.UTFMax]byte // bytes left over
|
|
|
|
}
|
|
|
|
|
|
|
|
// readByte returns the next byte from the input, which may be
|
|
|
|
// left over from a previous read if the UTF-8 was ill-formed.
|
|
|
|
func (r *readRune) readByte() (b byte, err os.Error) {
|
|
|
|
if r.pending > 0 {
|
|
|
|
b = r.pendBuf[0]
|
|
|
|
copy(r.pendBuf[0:], r.pendBuf[1:])
|
|
|
|
r.pending--
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = r.reader.Read(r.pendBuf[0:1])
|
|
|
|
return r.pendBuf[0], err
|
|
|
|
}
|
|
|
|
|
|
|
|
// unread saves the bytes for the next read.
|
|
|
|
func (r *readRune) unread(buf []byte) {
|
|
|
|
copy(r.pendBuf[r.pending:], buf)
|
|
|
|
r.pending += len(buf)
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadRune returns the next UTF-8 encoded code point from the
|
|
|
|
// io.Reader inside r.
|
2010-06-15 18:41:11 -06:00
|
|
|
func (r *readRune) ReadRune() (rune int, size int, err os.Error) {
|
|
|
|
r.buf[0], err = r.readByte()
|
2010-05-25 22:02:35 -06:00
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
|
|
|
|
rune = int(r.buf[0])
|
|
|
|
return
|
|
|
|
}
|
2010-06-15 18:41:11 -06:00
|
|
|
var n int
|
|
|
|
for n = 1; !utf8.FullRune(r.buf[0:n]); n++ {
|
|
|
|
r.buf[n], err = r.readByte()
|
2010-05-25 22:02:35 -06:00
|
|
|
if err != nil {
|
2010-06-15 18:41:11 -06:00
|
|
|
if err == os.EOF {
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 18:41:11 -06:00
|
|
|
rune, size = utf8.DecodeRune(r.buf[0:n])
|
|
|
|
if size < n { // an error
|
|
|
|
r.unread(r.buf[size:n])
|
|
|
|
}
|
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-31 16:36:28 -07:00
|
|
|
var ssFree = newCache(func() interface{} { return new(ss) })
|
2010-05-25 22:02:35 -06:00
|
|
|
|
2011-01-31 16:36:28 -07:00
|
|
|
// Allocate a new ss struct or grab a cached one.
|
2011-03-03 11:43:29 -07:00
|
|
|
func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
|
|
|
|
// If the reader is a *ss, then we've got a recursive
|
|
|
|
// call to Scan, so re-use the scan state.
|
|
|
|
s, ok := r.(*ss)
|
|
|
|
if ok {
|
|
|
|
old = s.ssave
|
|
|
|
s.limit = s.fieldLimit
|
|
|
|
s.nlIsEnd = nlIsEnd || s.nlIsEnd
|
|
|
|
s.nlIsSpace = nlIsSpace
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = ssFree.get().(*ss)
|
2011-02-02 16:00:14 -07:00
|
|
|
if rr, ok := r.(io.RuneReader); ok {
|
2010-05-25 22:02:35 -06:00
|
|
|
s.rr = rr
|
|
|
|
} else {
|
2010-06-15 18:41:11 -06:00
|
|
|
s.rr = &readRune{reader: r}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
s.nlIsSpace = nlIsSpace
|
2011-02-24 11:14:19 -07:00
|
|
|
s.nlIsEnd = nlIsEnd
|
|
|
|
s.prevRune = -1
|
2010-05-25 22:02:35 -06:00
|
|
|
s.peekRune = -1
|
2010-06-24 16:24:25 -06:00
|
|
|
s.atEOF = false
|
2011-03-03 11:43:29 -07:00
|
|
|
s.limit = hugeWid
|
|
|
|
s.fieldLimit = hugeWid
|
2011-03-02 11:54:23 -07:00
|
|
|
s.maxWid = hugeWid
|
2011-03-03 11:43:29 -07:00
|
|
|
s.validSave = true
|
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save used ss structs in ssFree; avoid an allocation per invocation.
|
2011-03-03 11:43:29 -07:00
|
|
|
func (s *ss) free(old ssave) {
|
|
|
|
// If it was used recursively, just restore the old state.
|
|
|
|
if old.validSave {
|
|
|
|
s.ssave = old
|
|
|
|
return
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// Don't hold on to ss structs with large buffers.
|
|
|
|
if cap(s.buf.Bytes()) > 1024 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.buf.Reset()
|
|
|
|
s.rr = nil
|
2011-01-31 16:36:28 -07:00
|
|
|
ssFree.put(s)
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2010-07-26 17:38:35 -06:00
|
|
|
// skipSpace skips spaces and maybe newlines.
|
|
|
|
func (s *ss) skipSpace(stopAtNewline bool) {
|
2010-05-25 22:02:35 -06:00
|
|
|
for {
|
2010-06-01 16:21:21 -06:00
|
|
|
rune := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
if rune == '\n' {
|
2010-07-26 17:38:35 -06:00
|
|
|
if stopAtNewline {
|
|
|
|
break
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
if s.nlIsSpace {
|
|
|
|
continue
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("unexpected newline")
|
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
if !unicode.IsSpace(rune) {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2011-03-09 11:01:47 -07:00
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// token returns the next space-delimited string from the input. It
|
|
|
|
// skips white space. For Scanln, it stops at newlines. For Scan,
|
|
|
|
// newlines are treated as spaces.
|
2011-03-09 11:01:47 -07:00
|
|
|
func (s *ss) token(skipSpace bool, f func(int) bool) []byte {
|
|
|
|
if skipSpace {
|
|
|
|
s.skipSpace(false)
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// read until white space or newline
|
2011-03-02 11:54:23 -07:00
|
|
|
for {
|
2010-06-01 16:21:21 -06:00
|
|
|
rune := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
break
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2011-03-09 11:01:47 -07:00
|
|
|
if !f(rune) {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
s.buf.WriteRune(rune)
|
|
|
|
}
|
2011-03-09 11:01:47 -07:00
|
|
|
return s.buf.Bytes()
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// typeError indicates that the type of the operand did not match the format
|
2010-05-31 11:56:58 -06:00
|
|
|
func (s *ss) typeError(field interface{}, expected string) {
|
2011-04-25 11:39:36 -06:00
|
|
|
s.errorString("expected field of type pointer to " + expected + "; found " + reflect.TypeOf(field).String())
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var complexError = os.ErrorString("syntax error scanning complex number")
|
2010-06-03 17:06:41 -06:00
|
|
|
var boolError = os.ErrorString("syntax error scanning boolean")
|
|
|
|
|
2010-12-06 12:23:37 -07:00
|
|
|
// 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.
|
|
|
|
func (s *ss) consume(ok string, accept bool) bool {
|
2010-06-03 17:06:41 -06:00
|
|
|
rune := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune == eof {
|
2010-06-03 17:06:41 -06:00
|
|
|
return false
|
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
if strings.IndexRune(ok, rune) >= 0 {
|
|
|
|
if accept {
|
|
|
|
s.buf.WriteRune(rune)
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
return true
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune != eof && accept {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
|
2011-02-04 14:14:38 -07:00
|
|
|
// peek reports whether the next character is in the ok string, without consuming it.
|
|
|
|
func (s *ss) peek(ok string) bool {
|
|
|
|
rune := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune != eof {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2011-02-04 14:14:38 -07:00
|
|
|
}
|
|
|
|
return strings.IndexRune(ok, rune) >= 0
|
|
|
|
}
|
|
|
|
|
2010-12-06 12:23:37 -07:00
|
|
|
// accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the
|
|
|
|
// buffer and returns true. Otherwise it return false.
|
|
|
|
func (s *ss) accept(ok string) bool {
|
|
|
|
return s.consume(ok, true)
|
|
|
|
}
|
|
|
|
|
2010-05-31 15:53:15 -06:00
|
|
|
// 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 {
|
|
|
|
for _, v := range okVerbs {
|
|
|
|
if v == verb {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("bad verb %" + string(verb) + " for " + typ)
|
2010-05-31 15:53:15 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanBool returns the value of the boolean represented by the next token.
|
|
|
|
func (s *ss) scanBool(verb int) bool {
|
2010-05-31 15:53:15 -06:00
|
|
|
if !s.okVerb(verb, "tv", "boolean") {
|
2010-05-25 22:02:35 -06:00
|
|
|
return false
|
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
// Syntax-checking a boolean is annoying. We're not fastidious about case.
|
2011-03-02 11:54:23 -07:00
|
|
|
switch s.mustReadRune() {
|
2010-06-03 17:06:41 -06:00
|
|
|
case '0':
|
|
|
|
return false
|
|
|
|
case '1':
|
|
|
|
return true
|
|
|
|
case 't', 'T':
|
|
|
|
if s.accept("rR") && (!s.accept("uU") || !s.accept("eE")) {
|
|
|
|
s.error(boolError)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case 'f', 'F':
|
|
|
|
if s.accept("aL") && (!s.accept("lL") || !s.accept("sS") || !s.accept("eE")) {
|
|
|
|
s.error(boolError)
|
|
|
|
}
|
|
|
|
return false
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
return false
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// Numerical elements
|
|
|
|
const (
|
|
|
|
binaryDigits = "01"
|
|
|
|
octalDigits = "01234567"
|
|
|
|
decimalDigits = "0123456789"
|
|
|
|
hexadecimalDigits = "0123456789aAbBcCdDeEfF"
|
|
|
|
sign = "+-"
|
|
|
|
period = "."
|
2011-02-02 13:38:48 -07:00
|
|
|
exponent = "eEp"
|
2010-06-03 17:06:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// getBase returns the numeric base represented by the verb and its digit string.
|
|
|
|
func (s *ss) getBase(verb int) (base int, digits string) {
|
2010-12-06 12:23:37 -07:00
|
|
|
s.okVerb(verb, "bdoUxXv", "integer") // sets s.err
|
2010-06-03 17:06:41 -06:00
|
|
|
base = 10
|
|
|
|
digits = decimalDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
base = 2
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = binaryDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
case 'o':
|
|
|
|
base = 8
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = octalDigits
|
2010-12-06 12:23:37 -07:00
|
|
|
case 'x', 'X', 'U':
|
2010-05-31 15:53:15 -06:00
|
|
|
base = 16
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = hexadecimalDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// scanNumber returns the numerical string with specified digits starting here.
|
2011-02-04 14:14:38 -07:00
|
|
|
func (s *ss) scanNumber(digits string, haveDigits bool) string {
|
|
|
|
if !haveDigits && !s.accept(digits) {
|
2010-06-03 17:06:41 -06:00
|
|
|
s.errorString("expected integer")
|
|
|
|
}
|
|
|
|
for s.accept(digits) {
|
|
|
|
}
|
|
|
|
return s.buf.String()
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:03:22 -06:00
|
|
|
// scanRune returns the next rune value in the input.
|
2010-06-21 14:19:29 -06:00
|
|
|
func (s *ss) scanRune(bitSize int) int64 {
|
2011-03-02 11:54:23 -07:00
|
|
|
rune := int64(s.mustReadRune())
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
|
|
|
x := (rune << (64 - n)) >> (64 - n)
|
2010-06-03 18:03:22 -06:00
|
|
|
if x != rune {
|
|
|
|
s.errorString("overflow on character value " + string(rune))
|
|
|
|
}
|
|
|
|
return rune
|
|
|
|
}
|
|
|
|
|
2011-02-04 14:14:38 -07:00
|
|
|
// scanBasePrefix reports whether the integer begins with a 0 or 0x,
|
|
|
|
// and returns the base, digit string, and whether a zero was found.
|
|
|
|
// It is called only if the verb is %v.
|
|
|
|
func (s *ss) scanBasePrefix() (base int, digits string, found bool) {
|
|
|
|
if !s.peek("0") {
|
|
|
|
return 10, decimalDigits, false
|
|
|
|
}
|
|
|
|
s.accept("0")
|
|
|
|
found = true // We've put a digit into the token buffer.
|
|
|
|
// Special cases for '0' && '0x'
|
|
|
|
base, digits = 8, octalDigits
|
|
|
|
if s.peek("xX") {
|
|
|
|
s.consume("xX", false)
|
|
|
|
base, digits = 16, hexadecimalDigits
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanInt returns the value of the integer represented by the next
|
|
|
|
// token, checking for overflow. Any error is stored in s.err.
|
2010-06-21 14:19:29 -06:00
|
|
|
func (s *ss) scanInt(verb int, bitSize int) int64 {
|
2010-06-03 18:03:22 -06:00
|
|
|
if verb == 'c' {
|
|
|
|
return s.scanRune(bitSize)
|
|
|
|
}
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2011-02-04 14:14:38 -07:00
|
|
|
base, digits := s.getBase(verb)
|
|
|
|
haveDigits := false
|
2010-12-06 12:23:37 -07:00
|
|
|
if verb == 'U' {
|
|
|
|
if !s.consume("U", false) || !s.consume("+", false) {
|
|
|
|
s.errorString("bad unicode format ")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.accept(sign) // If there's a sign, it will be left in the token buffer.
|
2011-02-04 14:14:38 -07:00
|
|
|
if verb == 'v' {
|
|
|
|
base, digits, haveDigits = s.scanBasePrefix()
|
|
|
|
}
|
2010-12-06 12:23:37 -07:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
tok := s.scanNumber(digits, haveDigits)
|
2010-06-01 16:21:21 -06:00
|
|
|
i, err := strconv.Btoi64(tok, base)
|
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
|
|
|
x := (i << (64 - n)) >> (64 - n)
|
2010-05-31 11:56:58 -06:00
|
|
|
if x != i {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("integer overflow on token " + tok)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanUint returns the value of the unsigned integer represented
|
|
|
|
// by the next token, checking for overflow. Any error is stored in s.err.
|
2010-06-21 14:19:29 -06:00
|
|
|
func (s *ss) scanUint(verb int, bitSize int) uint64 {
|
2010-06-03 18:03:22 -06:00
|
|
|
if verb == 'c' {
|
|
|
|
return uint64(s.scanRune(bitSize))
|
|
|
|
}
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2011-02-04 14:14:38 -07:00
|
|
|
base, digits := s.getBase(verb)
|
|
|
|
haveDigits := false
|
2010-12-06 12:23:37 -07:00
|
|
|
if verb == 'U' {
|
|
|
|
if !s.consume("U", false) || !s.consume("+", false) {
|
|
|
|
s.errorString("bad unicode format ")
|
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
} else if verb == 'v' {
|
|
|
|
base, digits, haveDigits = s.scanBasePrefix()
|
2010-12-06 12:23:37 -07:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
tok := s.scanNumber(digits, haveDigits)
|
2010-06-01 16:21:21 -06:00
|
|
|
i, err := strconv.Btoui64(tok, base)
|
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
|
|
|
x := (i << (64 - n)) >> (64 - n)
|
2010-05-31 11:56:58 -06:00
|
|
|
if x != i {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("unsigned integer overflow on token " + tok)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// floatToken returns the floating-point number starting here, no longer than swid
|
|
|
|
// if the width is specified. It's not rigorous about syntax because it doesn't check that
|
|
|
|
// we have at least some digits, but Atof will do that.
|
|
|
|
func (s *ss) floatToken() string {
|
|
|
|
s.buf.Reset()
|
2010-11-30 13:59:52 -07:00
|
|
|
// NaN?
|
|
|
|
if s.accept("nN") && s.accept("aA") && s.accept("nN") {
|
|
|
|
return s.buf.String()
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// leading sign?
|
2010-06-03 17:06:41 -06:00
|
|
|
s.accept(sign)
|
2010-11-30 13:59:52 -07:00
|
|
|
// Inf?
|
|
|
|
if s.accept("iI") && s.accept("nN") && s.accept("fF") {
|
|
|
|
return s.buf.String()
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// digits?
|
2010-06-03 17:06:41 -06:00
|
|
|
for s.accept(decimalDigits) {
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
// decimal point?
|
|
|
|
if s.accept(period) {
|
|
|
|
// fraction?
|
|
|
|
for s.accept(decimalDigits) {
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
// exponent?
|
2010-06-03 17:06:41 -06:00
|
|
|
if s.accept(exponent) {
|
2010-05-25 22:02:35 -06:00
|
|
|
// leading sign?
|
2010-06-03 17:06:41 -06:00
|
|
|
s.accept(sign)
|
2010-05-25 22:02:35 -06:00
|
|
|
// digits?
|
2010-06-03 17:06:41 -06:00
|
|
|
for s.accept(decimalDigits) {
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
return s.buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// complexTokens returns the real and imaginary parts of the complex number starting here.
|
|
|
|
// The number might be parenthesized and has the format (N+Ni) where N is a floating-point
|
|
|
|
// number and there are no spaces within.
|
|
|
|
func (s *ss) complexTokens() (real, imag string) {
|
|
|
|
// TODO: accept N and Ni independently?
|
|
|
|
parens := s.accept("(")
|
|
|
|
real = s.floatToken()
|
|
|
|
s.buf.Reset()
|
|
|
|
// Must now have a sign.
|
|
|
|
if !s.accept("+-") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
// Sign is now in buffer
|
|
|
|
imagSign := s.buf.String()
|
|
|
|
imag = s.floatToken()
|
|
|
|
if !s.accept("i") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
if parens && !s.accept(")") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
return real, imagSign + imag
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2010-06-20 13:16:25 -06:00
|
|
|
// convertFloat converts the string to a float64value.
|
|
|
|
func (s *ss) convertFloat(str string, n int) float64 {
|
2011-02-02 13:38:48 -07:00
|
|
|
if p := strings.Index(str, "p"); p >= 0 {
|
|
|
|
// Atof doesn't handle power-of-2 exponents,
|
|
|
|
// but they're easy to evaluate.
|
|
|
|
f, err := strconv.AtofN(str[:p], n)
|
|
|
|
if err != nil {
|
|
|
|
// Put full string into error.
|
|
|
|
if e, ok := err.(*strconv.NumError); ok {
|
|
|
|
e.Num = str
|
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
n, err := strconv.Atoi(str[p+1:])
|
|
|
|
if err != nil {
|
|
|
|
// Put full string into error.
|
|
|
|
if e, ok := err.(*strconv.NumError); ok {
|
|
|
|
e.Num = str
|
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
return math.Ldexp(f, n)
|
|
|
|
}
|
2010-06-20 13:16:25 -06:00
|
|
|
f, err := strconv.AtofN(str, n)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// convertComplex converts the next token to a complex128 value.
|
2010-05-25 22:02:35 -06:00
|
|
|
// The atof argument is a type-specific reader for the underlying type.
|
|
|
|
// If we're reading complex64, atof will parse float32s and convert them
|
|
|
|
// to float64's to avoid reproducing this code for each complex type.
|
2010-06-20 13:16:25 -06:00
|
|
|
func (s *ss) scanComplex(verb int, n int) complex128 {
|
2010-06-01 16:21:21 -06:00
|
|
|
if !s.okVerb(verb, floatVerbs, "complex") {
|
2010-05-25 22:02:35 -06:00
|
|
|
return 0
|
|
|
|
}
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2010-06-03 17:06:41 -06:00
|
|
|
sreal, simag := s.complexTokens()
|
2010-06-20 13:16:25 -06:00
|
|
|
real := s.convertFloat(sreal, n/2)
|
|
|
|
imag := s.convertFloat(simag, n/2)
|
2011-01-19 21:09:00 -07:00
|
|
|
return complex(real, imag)
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// convertString returns the string represented by the next input characters.
|
|
|
|
// The format of the input is determined by the verb.
|
2010-08-06 18:02:04 -06:00
|
|
|
func (s *ss) convertString(verb int) (str string) {
|
2010-06-01 16:21:21 -06:00
|
|
|
if !s.okVerb(verb, "svqx", "string") {
|
|
|
|
return ""
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2010-06-01 16:21:21 -06:00
|
|
|
switch verb {
|
|
|
|
case 'q':
|
2010-08-06 18:02:04 -06:00
|
|
|
str = s.quotedString()
|
2010-06-01 16:21:21 -06:00
|
|
|
case 'x':
|
2010-08-06 18:02:04 -06:00
|
|
|
str = s.hexString()
|
|
|
|
default:
|
2011-03-09 11:01:47 -07:00
|
|
|
str = string(s.token(true, notSpace)) // %s and %v just return the next word
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-08-06 18:02:04 -06:00
|
|
|
// Empty strings other than with %q are not OK.
|
|
|
|
if len(str) == 0 && verb != 'q' && s.maxWid > 0 {
|
|
|
|
s.errorString("Scan: no data for string")
|
|
|
|
}
|
|
|
|
return
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// quotedString returns the double- or back-quoted string represented by the next input characters.
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) quotedString() string {
|
2011-03-02 11:54:23 -07:00
|
|
|
quote := s.mustReadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
switch quote {
|
|
|
|
case '`':
|
|
|
|
// Back-quoted: Anything goes until EOF or back quote.
|
|
|
|
for {
|
2011-03-02 11:54:23 -07:00
|
|
|
rune := s.mustReadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
if rune == quote {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.buf.WriteRune(rune)
|
|
|
|
}
|
|
|
|
return s.buf.String()
|
|
|
|
case '"':
|
|
|
|
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
|
|
|
|
s.buf.WriteRune(quote)
|
|
|
|
for {
|
2011-03-02 11:54:23 -07:00
|
|
|
rune := s.mustReadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
s.buf.WriteRune(rune)
|
|
|
|
if rune == '\\' {
|
|
|
|
// 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.
|
2011-03-02 11:54:23 -07:00
|
|
|
rune := s.mustReadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
s.buf.WriteRune(rune)
|
|
|
|
} else if rune == '"' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result, err := strconv.Unquote(s.buf.String())
|
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
default:
|
|
|
|
s.errorString("expected quoted string")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// hexDigit returns the value of the hexadecimal digit
|
|
|
|
func (s *ss) hexDigit(digit int) int {
|
|
|
|
switch digit {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
return digit - '0'
|
|
|
|
case 'a', 'b', 'c', 'd', 'e', 'f':
|
|
|
|
return 10 + digit - 'a'
|
|
|
|
case 'A', 'B', 'C', 'D', 'E', 'F':
|
|
|
|
return 10 + digit - 'A'
|
|
|
|
}
|
|
|
|
s.errorString("Scan: illegal hex digit")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// hexByte returns the next hex-encoded (two-character) byte from the input.
|
|
|
|
// There must be either two hexadecimal digits or a space character in the input.
|
|
|
|
func (s *ss) hexByte() (b byte, ok bool) {
|
|
|
|
rune1 := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune1 == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if unicode.IsSpace(rune1) {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
2011-03-02 11:54:23 -07:00
|
|
|
rune2 := s.mustReadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
return byte(s.hexDigit(rune1)<<4 | s.hexDigit(rune2)), true
|
|
|
|
}
|
|
|
|
|
|
|
|
// hexString returns the space-delimited hexpair-encoded string.
|
|
|
|
func (s *ss) hexString() string {
|
|
|
|
for {
|
|
|
|
b, ok := s.hexByte()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s.buf.WriteByte(b)
|
|
|
|
}
|
|
|
|
if s.buf.Len() == 0 {
|
|
|
|
s.errorString("Scan: no hex data for %x string")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return s.buf.String()
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2011-02-02 13:38:48 -07:00
|
|
|
const floatVerbs = "beEfFgGv"
|
2010-05-31 15:53:15 -06:00
|
|
|
|
2011-03-02 11:54:23 -07:00
|
|
|
const hugeWid = 1 << 30
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
2010-05-31 15:53:15 -06:00
|
|
|
func (s *ss) scanOne(verb int, field interface{}) {
|
2010-06-03 17:06:41 -06:00
|
|
|
s.buf.Reset()
|
2010-06-01 16:21:21 -06:00
|
|
|
var err os.Error
|
2010-05-31 15:53:15 -06:00
|
|
|
// If the parameter has its own Scan method, use that.
|
|
|
|
if v, ok := field.(Scanner); ok {
|
2010-06-03 17:06:41 -06:00
|
|
|
err = v.Scan(s, verb)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
2011-02-24 11:14:19 -07:00
|
|
|
if err == os.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
s.error(err)
|
|
|
|
}
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-05-31 11:56:58 -06:00
|
|
|
switch v := field.(type) {
|
|
|
|
case *bool:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.scanBool(verb)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *complex64:
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = complex64(s.scanComplex(verb, 64))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *complex128:
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = s.scanComplex(verb, 128)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int(s.scanInt(verb, intBits))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int8:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int8(s.scanInt(verb, 8))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int16:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int16(s.scanInt(verb, 16))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int32:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int32(s.scanInt(verb, 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int64:
|
2010-09-05 16:04:53 -06:00
|
|
|
*v = s.scanInt(verb, 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint(s.scanUint(verb, intBits))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint8:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint8(s.scanUint(verb, 8))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint16:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint16(s.scanUint(verb, 16))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint32:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint32(s.scanUint(verb, 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint64:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.scanUint(verb, 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uintptr:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uintptr(s.scanUint(verb, uintptrBits))
|
|
|
|
// Floats are tricky because you want to scan in the precision of the result, not
|
|
|
|
// scan in high precision and convert, in order to preserve the correct error condition.
|
2010-05-31 11:56:58 -06:00
|
|
|
case *float32:
|
2010-05-31 15:53:15 -06:00
|
|
|
if s.okVerb(verb, floatVerbs, "float32") {
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = float32(s.convertFloat(s.floatToken(), 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
case *float64:
|
2010-05-31 15:53:15 -06:00
|
|
|
if s.okVerb(verb, floatVerbs, "float64") {
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = s.convertFloat(s.floatToken(), 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
case *string:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.convertString(verb)
|
2010-06-02 17:28:01 -06:00
|
|
|
case *[]byte:
|
|
|
|
// We scan to string and convert so we get a copy of the data.
|
|
|
|
// If we scanned to bytes, the slice would point at the buffer.
|
|
|
|
*v = []byte(s.convertString(verb))
|
2010-05-31 11:56:58 -06:00
|
|
|
default:
|
2011-04-25 11:39:36 -06:00
|
|
|
val := reflect.ValueOf(v)
|
2011-04-08 10:27:58 -06:00
|
|
|
ptr := val
|
|
|
|
if ptr.Kind() != reflect.Ptr {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("Scan: type not a pointer: " + val.Type().String())
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
2011-04-08 10:27:58 -06:00
|
|
|
switch v := ptr.Elem(); v.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
v.SetBool(s.scanBool(verb))
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
v.SetInt(s.scanInt(verb, v.Type().Bits()))
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
v.SetUint(s.scanUint(verb, v.Type().Bits()))
|
|
|
|
case reflect.String:
|
|
|
|
v.SetString(s.convertString(verb))
|
|
|
|
case reflect.Slice:
|
2010-06-02 17:28:01 -06:00
|
|
|
// For now, can only handle (renamed) []byte.
|
2011-04-08 10:27:58 -06:00
|
|
|
typ := v.Type()
|
2010-06-20 13:16:25 -06:00
|
|
|
if typ.Elem().Kind() != reflect.Uint8 {
|
2010-06-02 17:28:01 -06:00
|
|
|
goto CantHandle
|
|
|
|
}
|
|
|
|
str := s.convertString(verb)
|
|
|
|
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
|
|
|
|
for i := 0; i < len(str); i++ {
|
2011-04-08 10:27:58 -06:00
|
|
|
v.Index(i).SetUint(uint64(str[i]))
|
2010-06-02 17:28:01 -06:00
|
|
|
}
|
2011-04-08 10:27:58 -06:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(false)
|
2011-04-08 10:27:58 -06:00
|
|
|
v.SetFloat(s.convertFloat(s.floatToken(), v.Type().Bits()))
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
v.SetComplex(s.scanComplex(verb, v.Type().Bits()))
|
2010-05-31 11:56:58 -06:00
|
|
|
default:
|
2010-06-02 17:28:01 -06:00
|
|
|
CantHandle:
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("Scan: can't handle type: " + val.Type().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorHandler turns local panics into error returns. EOFs are benign.
|
|
|
|
func errorHandler(errp *os.Error) {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if se, ok := e.(scanError); ok { // catch local error
|
|
|
|
if se.err != os.EOF {
|
|
|
|
*errp = se.err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(e)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// doScan does the real work for scanning without a format string.
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) doScan(a []interface{}) (numProcessed int, err os.Error) {
|
|
|
|
defer errorHandler(&err)
|
|
|
|
for _, field := range a {
|
2010-05-31 15:53:15 -06:00
|
|
|
s.scanOne('v', field)
|
2010-06-01 16:21:21 -06:00
|
|
|
numProcessed++
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
// Check for newline if required.
|
|
|
|
if !s.nlIsSpace {
|
|
|
|
for {
|
2010-06-01 16:21:21 -06:00
|
|
|
rune := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune == '\n' || rune == eof {
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if !unicode.IsSpace(rune) {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("Scan: expected newline")
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-05-31 11:56:58 -06:00
|
|
|
|
2010-06-02 17:28:01 -06:00
|
|
|
// advance determines whether the next characters in the input match
|
2010-06-02 15:58:31 -06:00
|
|
|
// those of the format. It returns the number of bytes (sic) consumed
|
|
|
|
// in the format. Newlines included, all runs of space characters in
|
2010-06-02 17:28:01 -06:00
|
|
|
// either input or format behave as a single space. This routine also
|
|
|
|
// handles the %% case. If the return value is zero, either format
|
|
|
|
// starts with a % (with no following %) or the input is empty.
|
2010-06-18 21:37:03 -06:00
|
|
|
// If it is negative, the input did not match the string.
|
2010-06-02 15:58:31 -06:00
|
|
|
func (s *ss) advance(format string) (i int) {
|
|
|
|
for i < len(format) {
|
|
|
|
fmtc, w := utf8.DecodeRuneInString(format[i:])
|
|
|
|
if fmtc == '%' {
|
|
|
|
// %% acts like a real percent
|
|
|
|
nextc, _ := utf8.DecodeRuneInString(format[i+w:]) // will not match % if string is empty
|
|
|
|
if nextc != '%' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i += w // skip the first %
|
|
|
|
}
|
|
|
|
sawSpace := false
|
|
|
|
for unicode.IsSpace(fmtc) && i < len(format) {
|
|
|
|
sawSpace = true
|
|
|
|
i += w
|
|
|
|
fmtc, w = utf8.DecodeRuneInString(format[i:])
|
|
|
|
}
|
|
|
|
if sawSpace {
|
|
|
|
// There was space in the format, so there should be space (EOF)
|
|
|
|
// in the input.
|
|
|
|
inputc := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if inputc == eof {
|
2010-06-02 15:58:31 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !unicode.IsSpace(inputc) {
|
|
|
|
// Space in format but not in input: error
|
|
|
|
s.errorString("expected space in input to match format")
|
|
|
|
}
|
2010-07-26 17:38:35 -06:00
|
|
|
s.skipSpace(true)
|
2010-06-02 15:58:31 -06:00
|
|
|
continue
|
|
|
|
}
|
2011-03-02 11:54:23 -07:00
|
|
|
inputc := s.mustReadRune()
|
2010-06-02 15:58:31 -06:00
|
|
|
if fmtc != inputc {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-18 21:37:03 -06:00
|
|
|
return -1
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
i += w
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// doScanf does the real work when scanning with a format string.
|
|
|
|
// At the moment, it handles only pointers to basic types.
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err os.Error) {
|
|
|
|
defer errorHandler(&err)
|
2010-05-31 11:56:58 -06:00
|
|
|
end := len(format) - 1
|
2010-06-01 16:21:21 -06:00
|
|
|
// We process one item per non-trivial format
|
2010-05-31 11:56:58 -06:00
|
|
|
for i := 0; i <= end; {
|
2010-06-02 15:58:31 -06:00
|
|
|
w := s.advance(format[i:])
|
|
|
|
if w > 0 {
|
2010-05-31 11:56:58 -06:00
|
|
|
i += w
|
|
|
|
continue
|
|
|
|
}
|
2010-06-02 17:28:01 -06:00
|
|
|
// Either we failed to advance, we have a percent character, or we ran out of input.
|
2010-06-02 15:58:31 -06:00
|
|
|
if format[i] != '%' {
|
2010-06-18 21:37:03 -06:00
|
|
|
// Can't advance format. Why not?
|
|
|
|
if w < 0 {
|
|
|
|
s.errorString("input does not match format")
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
2010-06-18 21:37:03 -06:00
|
|
|
// Otherwise at EOF; "too many operands" error handled below
|
2010-06-02 15:58:31 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
i++ // % is one byte
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// do we have 20 (width)?
|
2011-03-02 11:54:23 -07:00
|
|
|
var widPresent bool
|
|
|
|
s.maxWid, widPresent, i = parsenum(format, i, end)
|
|
|
|
if !widPresent {
|
|
|
|
s.maxWid = hugeWid
|
|
|
|
}
|
2011-03-03 11:43:29 -07:00
|
|
|
s.fieldLimit = s.limit
|
|
|
|
if f := s.count + s.maxWid; f < s.fieldLimit {
|
|
|
|
s.fieldLimit = f
|
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
c, w := utf8.DecodeRuneInString(format[i:])
|
2010-05-31 11:56:58 -06:00
|
|
|
i += w
|
2010-05-31 15:53:15 -06:00
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
if numProcessed >= len(a) { // out of operands
|
|
|
|
s.errorString("too few operands for format %" + format[i-w:])
|
2010-05-31 15:53:15 -06:00
|
|
|
break
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
field := a[numProcessed]
|
2010-05-31 11:56:58 -06:00
|
|
|
|
2010-05-31 15:53:15 -06:00
|
|
|
s.scanOne(c, field)
|
2010-06-01 16:21:21 -06:00
|
|
|
numProcessed++
|
2011-03-03 11:43:29 -07:00
|
|
|
s.fieldLimit = s.limit
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
if numProcessed < len(a) {
|
|
|
|
s.errorString("too many operands")
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|