Source file src/fmt/scan.go

Documentation: fmt

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package fmt
     6  
     7  import (
     8  	"errors"
     9  	"io"
    10  	"math"
    11  	"os"
    12  	"reflect"
    13  	"strconv"
    14  	"sync"
    15  	"unicode/utf8"
    16  )
    17  
    18  // ScanState represents the scanner state passed to custom scanners.
    19  // Scanners may do rune-at-a-time scanning or ask the ScanState
    20  // to discover the next space-delimited token.
    21  type ScanState interface {
    22  	// ReadRune reads the next rune (Unicode code point) from the input.
    23  	// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
    24  	// return EOF after returning the first '\n' or when reading beyond
    25  	// the specified width.
    26  	ReadRune() (r rune, size int, err error)
    27  	// UnreadRune causes the next call to ReadRune to return the same rune.
    28  	UnreadRune() error
    29  	// SkipSpace skips space in the input. Newlines are treated appropriately
    30  	// for the operation being performed; see the package documentation
    31  	// for more information.
    32  	SkipSpace()
    33  	// Token skips space in the input if skipSpace is true, then returns the
    34  	// run of Unicode code points c satisfying f(c).  If f is nil,
    35  	// !unicode.IsSpace(c) is used; that is, the token will hold non-space
    36  	// characters. Newlines are treated appropriately for the operation being
    37  	// performed; see the package documentation for more information.
    38  	// The returned slice points to shared data that may be overwritten
    39  	// by the next call to Token, a call to a Scan function using the ScanState
    40  	// as input, or when the calling Scan method returns.
    41  	Token(skipSpace bool, f func(rune) bool) (token []byte, err error)
    42  	// Width returns the value of the width option and whether it has been set.
    43  	// The unit is Unicode code points.
    44  	Width() (wid int, ok bool)
    45  	// Because ReadRune is implemented by the interface, Read should never be
    46  	// called by the scanning routines and a valid implementation of
    47  	// ScanState may choose always to return an error from Read.
    48  	Read(buf []byte) (n int, err error)
    49  }
    50  
    51  // Scanner is implemented by any value that has a Scan method, which scans
    52  // the input for the representation of a value and stores the result in the
    53  // receiver, which must be a pointer to be useful. The Scan method is called
    54  // for any argument to Scan, Scanf, or Scanln that implements it.
    55  type Scanner interface {
    56  	Scan(state ScanState, verb rune) error
    57  }
    58  
    59  // Scan scans text read from standard input, storing successive
    60  // space-separated values into successive arguments. Newlines count
    61  // as space. It returns the number of items successfully scanned.
    62  // If that is less than the number of arguments, err will report why.
    63  func Scan(a ...interface{}) (n int, err error) {
    64  	return Fscan(os.Stdin, a...)
    65  }
    66  
    67  // Scanln is similar to Scan, but stops scanning at a newline and
    68  // after the final item there must be a newline or EOF.
    69  func Scanln(a ...interface{}) (n int, err error) {
    70  	return Fscanln(os.Stdin, a...)
    71  }
    72  
    73  // Scanf scans text read from standard input, storing successive
    74  // space-separated values into successive arguments as determined by
    75  // the format. It returns the number of items successfully scanned.
    76  // If that is less than the number of arguments, err will report why.
    77  // Newlines in the input must match newlines in the format.
    78  // The one exception: the verb %c always scans the next rune in the
    79  // input, even if it is a space (or tab etc.) or newline.
    80  func Scanf(format string, a ...interface{}) (n int, err error) {
    81  	return Fscanf(os.Stdin, format, a...)
    82  }
    83  
    84  type stringReader string
    85  
    86  func (r *stringReader) Read(b []byte) (n int, err error) {
    87  	n = copy(b, *r)
    88  	*r = (*r)[n:]
    89  	if n == 0 {
    90  		err = io.EOF
    91  	}
    92  	return
    93  }
    94  
    95  // Sscan scans the argument string, storing successive space-separated
    96  // values into successive arguments. Newlines count as space. It
    97  // returns the number of items successfully scanned. If that is less
    98  // than the number of arguments, err will report why.
    99  func Sscan(str string, a ...interface{}) (n int, err error) {
   100  	return Fscan((*stringReader)(&str), a...)
   101  }
   102  
   103  // Sscanln is similar to Sscan, but stops scanning at a newline and
   104  // after the final item there must be a newline or EOF.
   105  func Sscanln(str string, a ...interface{}) (n int, err error) {
   106  	return Fscanln((*stringReader)(&str), a...)
   107  }
   108  
   109  // Sscanf scans the argument string, storing successive space-separated
   110  // values into successive arguments as determined by the format. It
   111  // returns the number of items successfully parsed.
   112  // Newlines in the input must match newlines in the format.
   113  func Sscanf(str string, format string, a ...interface{}) (n int, err error) {
   114  	return Fscanf((*stringReader)(&str), format, a...)
   115  }
   116  
   117  // Fscan scans text read from r, storing successive space-separated
   118  // values into successive arguments. Newlines count as space. It
   119  // returns the number of items successfully scanned. If that is less
   120  // than the number of arguments, err will report why.
   121  func Fscan(r io.Reader, a ...interface{}) (n int, err error) {
   122  	s, old := newScanState(r, true, false)
   123  	n, err = s.doScan(a)
   124  	s.free(old)
   125  	return
   126  }
   127  
   128  // Fscanln is similar to Fscan, but stops scanning at a newline and
   129  // after the final item there must be a newline or EOF.
   130  func Fscanln(r io.Reader, a ...interface{}) (n int, err error) {
   131  	s, old := newScanState(r, false, true)
   132  	n, err = s.doScan(a)
   133  	s.free(old)
   134  	return
   135  }
   136  
   137  // Fscanf scans text read from r, storing successive space-separated
   138  // values into successive arguments as determined by the format. It
   139  // returns the number of items successfully parsed.
   140  // Newlines in the input must match newlines in the format.
   141  func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error) {
   142  	s, old := newScanState(r, false, false)
   143  	n, err = s.doScanf(format, a)
   144  	s.free(old)
   145  	return
   146  }
   147  
   148  // scanError represents an error generated by the scanning software.
   149  // It's used as a unique signature to identify such errors when recovering.
   150  type scanError struct {
   151  	err error
   152  }
   153  
   154  const eof = -1
   155  
   156  // ss is the internal implementation of ScanState.
   157  type ss struct {
   158  	rs    io.RuneScanner // where to read input
   159  	buf   buffer         // token accumulator
   160  	count int            // runes consumed so far.
   161  	atEOF bool           // already read EOF
   162  	ssave
   163  }
   164  
   165  // ssave holds the parts of ss that need to be
   166  // saved and restored on recursive scans.
   167  type ssave struct {
   168  	validSave bool // is or was a part of an actual ss.
   169  	nlIsEnd   bool // whether newline terminates scan
   170  	nlIsSpace bool // whether newline counts as white space
   171  	argLimit  int  // max value of ss.count for this arg; argLimit <= limit
   172  	limit     int  // max value of ss.count.
   173  	maxWid    int  // width of this arg.
   174  }
   175  
   176  // The Read method is only in ScanState so that ScanState
   177  // satisfies io.Reader. It will never be called when used as
   178  // intended, so there is no need to make it actually work.
   179  func (s *ss) Read(buf []byte) (n int, err error) {
   180  	return 0, errors.New("ScanState's Read should not be called. Use ReadRune")
   181  }
   182  
   183  func (s *ss) ReadRune() (r rune, size int, err error) {
   184  	if s.atEOF || s.count >= s.argLimit {
   185  		err = io.EOF
   186  		return
   187  	}
   188  
   189  	r, size, err = s.rs.ReadRune()
   190  	if err == nil {
   191  		s.count++
   192  		if s.nlIsEnd && r == '\n' {
   193  			s.atEOF = true
   194  		}
   195  	} else if err == io.EOF {
   196  		s.atEOF = true
   197  	}
   198  	return
   199  }
   200  
   201  func (s *ss) Width() (wid int, ok bool) {
   202  	if s.maxWid == hugeWid {
   203  		return 0, false
   204  	}
   205  	return s.maxWid, true
   206  }
   207  
   208  // The public method returns an error; this private one panics.
   209  // If getRune reaches EOF, the return value is EOF (-1).
   210  func (s *ss) getRune() (r rune) {
   211  	r, _, err := s.ReadRune()
   212  	if err != nil {
   213  		if err == io.EOF {
   214  			return eof
   215  		}
   216  		s.error(err)
   217  	}
   218  	return
   219  }
   220  
   221  // mustReadRune turns io.EOF into a panic(io.ErrUnexpectedEOF).
   222  // It is called in cases such as string scanning where an EOF is a
   223  // syntax error.
   224  func (s *ss) mustReadRune() (r rune) {
   225  	r = s.getRune()
   226  	if r == eof {
   227  		s.error(io.ErrUnexpectedEOF)
   228  	}
   229  	return
   230  }
   231  
   232  func (s *ss) UnreadRune() error {
   233  	s.rs.UnreadRune()
   234  	s.atEOF = false
   235  	s.count--
   236  	return nil
   237  }
   238  
   239  func (s *ss) error(err error) {
   240  	panic(scanError{err})
   241  }
   242  
   243  func (s *ss) errorString(err string) {
   244  	panic(scanError{errors.New(err)})
   245  }
   246  
   247  func (s *ss) Token(skipSpace bool, f func(rune) bool) (tok []byte, err error) {
   248  	defer func() {
   249  		if e := recover(); e != nil {
   250  			if se, ok := e.(scanError); ok {
   251  				err = se.err
   252  			} else {
   253  				panic(e)
   254  			}
   255  		}
   256  	}()
   257  	if f == nil {
   258  		f = notSpace
   259  	}
   260  	s.buf = s.buf[:0]
   261  	tok = s.token(skipSpace, f)
   262  	return
   263  }
   264  
   265  // space is a copy of the unicode.White_Space ranges,
   266  // to avoid depending on package unicode.
   267  var space = [][2]uint16{
   268  	{0x0009, 0x000d},
   269  	{0x0020, 0x0020},
   270  	{0x0085, 0x0085},
   271  	{0x00a0, 0x00a0},
   272  	{0x1680, 0x1680},
   273  	{0x2000, 0x200a},
   274  	{0x2028, 0x2029},
   275  	{0x202f, 0x202f},
   276  	{0x205f, 0x205f},
   277  	{0x3000, 0x3000},
   278  }
   279  
   280  func isSpace(r rune) bool {
   281  	if r >= 1<<16 {
   282  		return false
   283  	}
   284  	rx := uint16(r)
   285  	for _, rng := range space {
   286  		if rx < rng[0] {
   287  			return false
   288  		}
   289  		if rx <= rng[1] {
   290  			return true
   291  		}
   292  	}
   293  	return false
   294  }
   295  
   296  // notSpace is the default scanning function used in Token.
   297  func notSpace(r rune) bool {
   298  	return !isSpace(r)
   299  }
   300  
   301  // readRune is a structure to enable reading UTF-8 encoded code points
   302  // from an io.Reader. It is used if the Reader given to the scanner does
   303  // not already implement io.RuneScanner.
   304  type readRune struct {
   305  	reader   io.Reader
   306  	buf      [utf8.UTFMax]byte // used only inside ReadRune
   307  	pending  int               // number of bytes in pendBuf; only >0 for bad UTF-8
   308  	pendBuf  [utf8.UTFMax]byte // bytes left over
   309  	peekRune rune              // if >=0 next rune; when <0 is ^(previous Rune)
   310  }
   311  
   312  // readByte returns the next byte from the input, which may be
   313  // left over from a previous read if the UTF-8 was ill-formed.
   314  func (r *readRune) readByte() (b byte, err error) {
   315  	if r.pending > 0 {
   316  		b = r.pendBuf[0]
   317  		copy(r.pendBuf[0:], r.pendBuf[1:])
   318  		r.pending--
   319  		return
   320  	}
   321  	n, err := io.ReadFull(r.reader, r.pendBuf[:1])
   322  	if n != 1 {
   323  		return 0, err
   324  	}
   325  	return r.pendBuf[0], err
   326  }
   327  
   328  // ReadRune returns the next UTF-8 encoded code point from the
   329  // io.Reader inside r.
   330  func (r *readRune) ReadRune() (rr rune, size int, err error) {
   331  	if r.peekRune >= 0 {
   332  		rr = r.peekRune
   333  		r.peekRune = ^r.peekRune
   334  		size = utf8.RuneLen(rr)
   335  		return
   336  	}
   337  	r.buf[0], err = r.readByte()
   338  	if err != nil {
   339  		return
   340  	}
   341  	if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
   342  		rr = rune(r.buf[0])
   343  		size = 1 // Known to be 1.
   344  		// Flip the bits of the rune so it's available to UnreadRune.
   345  		r.peekRune = ^rr
   346  		return
   347  	}
   348  	var n int
   349  	for n = 1; !utf8.FullRune(r.buf[:n]); n++ {
   350  		r.buf[n], err = r.readByte()
   351  		if err != nil {
   352  			if err == io.EOF {
   353  				err = nil
   354  				break
   355  			}
   356  			return
   357  		}
   358  	}
   359  	rr, size = utf8.DecodeRune(r.buf[:n])
   360  	if size < n { // an error, save the bytes for the next read
   361  		copy(r.pendBuf[r.pending:], r.buf[size:n])
   362  		r.pending += n - size
   363  	}
   364  	// Flip the bits of the rune so it's available to UnreadRune.
   365  	r.peekRune = ^rr
   366  	return
   367  }
   368  
   369  func (r *readRune) UnreadRune() error {
   370  	if r.peekRune >= 0 {
   371  		return errors.New("fmt: scanning called UnreadRune with no rune available")
   372  	}
   373  	// Reverse bit flip of previously read rune to obtain valid >=0 state.
   374  	r.peekRune = ^r.peekRune
   375  	return nil
   376  }
   377  
   378  var ssFree = sync.Pool{
   379  	New: func() interface{} { return new(ss) },
   380  }
   381  
   382  // newScanState allocates a new ss struct or grab a cached one.
   383  func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
   384  	s = ssFree.Get().(*ss)
   385  	if rs, ok := r.(io.RuneScanner); ok {
   386  		s.rs = rs
   387  	} else {
   388  		s.rs = &readRune{reader: r, peekRune: -1}
   389  	}
   390  	s.nlIsSpace = nlIsSpace
   391  	s.nlIsEnd = nlIsEnd
   392  	s.atEOF = false
   393  	s.limit = hugeWid
   394  	s.argLimit = hugeWid
   395  	s.maxWid = hugeWid
   396  	s.validSave = true
   397  	s.count = 0
   398  	return
   399  }
   400  
   401  // free saves used ss structs in ssFree; avoid an allocation per invocation.
   402  func (s *ss) free(old ssave) {
   403  	// If it was used recursively, just restore the old state.
   404  	if old.validSave {
   405  		s.ssave = old
   406  		return
   407  	}
   408  	// Don't hold on to ss structs with large buffers.
   409  	if cap(s.buf) > 1024 {
   410  		return
   411  	}
   412  	s.buf = s.buf[:0]
   413  	s.rs = nil
   414  	ssFree.Put(s)
   415  }
   416  
   417  // SkipSpace provides Scan methods the ability to skip space and newline
   418  // characters in keeping with the current scanning mode set by format strings
   419  // and Scan/Scanln.
   420  func (s *ss) SkipSpace() {
   421  	for {
   422  		r := s.getRune()
   423  		if r == eof {
   424  			return
   425  		}
   426  		if r == '\r' && s.peek("\n") {
   427  			continue
   428  		}
   429  		if r == '\n' {
   430  			if s.nlIsSpace {
   431  				continue
   432  			}
   433  			s.errorString("unexpected newline")
   434  			return
   435  		}
   436  		if !isSpace(r) {
   437  			s.UnreadRune()
   438  			break
   439  		}
   440  	}
   441  }
   442  
   443  // token returns the next space-delimited string from the input. It
   444  // skips white space. For Scanln, it stops at newlines. For Scan,
   445  // newlines are treated as spaces.
   446  func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
   447  	if skipSpace {
   448  		s.SkipSpace()
   449  	}
   450  	// read until white space or newline
   451  	for {
   452  		r := s.getRune()
   453  		if r == eof {
   454  			break
   455  		}
   456  		if !f(r) {
   457  			s.UnreadRune()
   458  			break
   459  		}
   460  		s.buf.WriteRune(r)
   461  	}
   462  	return s.buf
   463  }
   464  
   465  var complexError = errors.New("syntax error scanning complex number")
   466  var boolError = errors.New("syntax error scanning boolean")
   467  
   468  func indexRune(s string, r rune) int {
   469  	for i, c := range s {
   470  		if c == r {
   471  			return i
   472  		}
   473  	}
   474  	return -1
   475  }
   476  
   477  // consume reads the next rune in the input and reports whether it is in the ok string.
   478  // If accept is true, it puts the character into the input token.
   479  func (s *ss) consume(ok string, accept bool) bool {
   480  	r := s.getRune()
   481  	if r == eof {
   482  		return false
   483  	}
   484  	if indexRune(ok, r) >= 0 {
   485  		if accept {
   486  			s.buf.WriteRune(r)
   487  		}
   488  		return true
   489  	}
   490  	if r != eof && accept {
   491  		s.UnreadRune()
   492  	}
   493  	return false
   494  }
   495  
   496  // peek reports whether the next character is in the ok string, without consuming it.
   497  func (s *ss) peek(ok string) bool {
   498  	r := s.getRune()
   499  	if r != eof {
   500  		s.UnreadRune()
   501  	}
   502  	return indexRune(ok, r) >= 0
   503  }
   504  
   505  func (s *ss) notEOF() {
   506  	// Guarantee there is data to be read.
   507  	if r := s.getRune(); r == eof {
   508  		panic(io.EOF)
   509  	}
   510  	s.UnreadRune()
   511  }
   512  
   513  // accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the
   514  // buffer and returns true. Otherwise it return false.
   515  func (s *ss) accept(ok string) bool {
   516  	return s.consume(ok, true)
   517  }
   518  
   519  // okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
   520  func (s *ss) okVerb(verb rune, okVerbs, typ string) bool {
   521  	for _, v := range okVerbs {
   522  		if v == verb {
   523  			return true
   524  		}
   525  	}
   526  	s.errorString("bad verb '%" + string(verb) + "' for " + typ)
   527  	return false
   528  }
   529  
   530  // scanBool returns the value of the boolean represented by the next token.
   531  func (s *ss) scanBool(verb rune) bool {
   532  	s.SkipSpace()
   533  	s.notEOF()
   534  	if !s.okVerb(verb, "tv", "boolean") {
   535  		return false
   536  	}
   537  	// Syntax-checking a boolean is annoying. We're not fastidious about case.
   538  	switch s.getRune() {
   539  	case '0':
   540  		return false
   541  	case '1':
   542  		return true
   543  	case 't', 'T':
   544  		if s.accept("rR") && (!s.accept("uU") || !s.accept("eE")) {
   545  			s.error(boolError)
   546  		}
   547  		return true
   548  	case 'f', 'F':
   549  		if s.accept("aA") && (!s.accept("lL") || !s.accept("sS") || !s.accept("eE")) {
   550  			s.error(boolError)
   551  		}
   552  		return false
   553  	}
   554  	return false
   555  }
   556  
   557  // Numerical elements
   558  const (
   559  	binaryDigits      = "01"
   560  	octalDigits       = "01234567"
   561  	decimalDigits     = "0123456789"
   562  	hexadecimalDigits = "0123456789aAbBcCdDeEfF"
   563  	sign              = "+-"
   564  	period            = "."
   565  	exponent          = "eEp"
   566  )
   567  
   568  // getBase returns the numeric base represented by the verb and its digit string.
   569  func (s *ss) getBase(verb rune) (base int, digits string) {
   570  	s.okVerb(verb, "bdoUxXv", "integer") // sets s.err
   571  	base = 10
   572  	digits = decimalDigits
   573  	switch verb {
   574  	case 'b':
   575  		base = 2
   576  		digits = binaryDigits
   577  	case 'o':
   578  		base = 8
   579  		digits = octalDigits
   580  	case 'x', 'X', 'U':
   581  		base = 16
   582  		digits = hexadecimalDigits
   583  	}
   584  	return
   585  }
   586  
   587  // scanNumber returns the numerical string with specified digits starting here.
   588  func (s *ss) scanNumber(digits string, haveDigits bool) string {
   589  	if !haveDigits {
   590  		s.notEOF()
   591  		if !s.accept(digits) {
   592  			s.errorString("expected integer")
   593  		}
   594  	}
   595  	for s.accept(digits) {
   596  	}
   597  	return string(s.buf)
   598  }
   599  
   600  // scanRune returns the next rune value in the input.
   601  func (s *ss) scanRune(bitSize int) int64 {
   602  	s.notEOF()
   603  	r := int64(s.getRune())
   604  	n := uint(bitSize)
   605  	x := (r << (64 - n)) >> (64 - n)
   606  	if x != r {
   607  		s.errorString("overflow on character value " + string(r))
   608  	}
   609  	return r
   610  }
   611  
   612  // scanBasePrefix reports whether the integer begins with a 0 or 0x,
   613  // and returns the base, digit string, and whether a zero was found.
   614  // It is called only if the verb is %v.
   615  func (s *ss) scanBasePrefix() (base int, digits string, found bool) {
   616  	if !s.peek("0") {
   617  		return 10, decimalDigits, false
   618  	}
   619  	s.accept("0")
   620  	found = true // We've put a digit into the token buffer.
   621  	// Special cases for '0' && '0x'
   622  	base, digits = 8, octalDigits
   623  	if s.peek("xX") {
   624  		s.consume("xX", false)
   625  		base, digits = 16, hexadecimalDigits
   626  	}
   627  	return
   628  }
   629  
   630  // scanInt returns the value of the integer represented by the next
   631  // token, checking for overflow. Any error is stored in s.err.
   632  func (s *ss) scanInt(verb rune, bitSize int) int64 {
   633  	if verb == 'c' {
   634  		return s.scanRune(bitSize)
   635  	}
   636  	s.SkipSpace()
   637  	s.notEOF()
   638  	base, digits := s.getBase(verb)
   639  	haveDigits := false
   640  	if verb == 'U' {
   641  		if !s.consume("U", false) || !s.consume("+", false) {
   642  			s.errorString("bad unicode format ")
   643  		}
   644  	} else {
   645  		s.accept(sign) // If there's a sign, it will be left in the token buffer.
   646  		if verb == 'v' {
   647  			base, digits, haveDigits = s.scanBasePrefix()
   648  		}
   649  	}
   650  	tok := s.scanNumber(digits, haveDigits)
   651  	i, err := strconv.ParseInt(tok, base, 64)
   652  	if err != nil {
   653  		s.error(err)
   654  	}
   655  	n := uint(bitSize)
   656  	x := (i << (64 - n)) >> (64 - n)
   657  	if x != i {
   658  		s.errorString("integer overflow on token " + tok)
   659  	}
   660  	return i
   661  }
   662  
   663  // scanUint returns the value of the unsigned integer represented
   664  // by the next token, checking for overflow. Any error is stored in s.err.
   665  func (s *ss) scanUint(verb rune, bitSize int) uint64 {
   666  	if verb == 'c' {
   667  		return uint64(s.scanRune(bitSize))
   668  	}
   669  	s.SkipSpace()
   670  	s.notEOF()
   671  	base, digits := s.getBase(verb)
   672  	haveDigits := false
   673  	if verb == 'U' {
   674  		if !s.consume("U", false) || !s.consume("+", false) {
   675  			s.errorString("bad unicode format ")
   676  		}
   677  	} else if verb == 'v' {
   678  		base, digits, haveDigits = s.scanBasePrefix()
   679  	}
   680  	tok := s.scanNumber(digits, haveDigits)
   681  	i, err := strconv.ParseUint(tok, base, 64)
   682  	if err != nil {
   683  		s.error(err)
   684  	}
   685  	n := uint(bitSize)
   686  	x := (i << (64 - n)) >> (64 - n)
   687  	if x != i {
   688  		s.errorString("unsigned integer overflow on token " + tok)
   689  	}
   690  	return i
   691  }
   692  
   693  // floatToken returns the floating-point number starting here, no longer than swid
   694  // if the width is specified. It's not rigorous about syntax because it doesn't check that
   695  // we have at least some digits, but Atof will do that.
   696  func (s *ss) floatToken() string {
   697  	s.buf = s.buf[:0]
   698  	// NaN?
   699  	if s.accept("nN") && s.accept("aA") && s.accept("nN") {
   700  		return string(s.buf)
   701  	}
   702  	// leading sign?
   703  	s.accept(sign)
   704  	// Inf?
   705  	if s.accept("iI") && s.accept("nN") && s.accept("fF") {
   706  		return string(s.buf)
   707  	}
   708  	// digits?
   709  	for s.accept(decimalDigits) {
   710  	}
   711  	// decimal point?
   712  	if s.accept(period) {
   713  		// fraction?
   714  		for s.accept(decimalDigits) {
   715  		}
   716  	}
   717  	// exponent?
   718  	if s.accept(exponent) {
   719  		// leading sign?
   720  		s.accept(sign)
   721  		// digits?
   722  		for s.accept(decimalDigits) {
   723  		}
   724  	}
   725  	return string(s.buf)
   726  }
   727  
   728  // complexTokens returns the real and imaginary parts of the complex number starting here.
   729  // The number might be parenthesized and has the format (N+Ni) where N is a floating-point
   730  // number and there are no spaces within.
   731  func (s *ss) complexTokens() (real, imag string) {
   732  	// TODO: accept N and Ni independently?
   733  	parens := s.accept("(")
   734  	real = s.floatToken()
   735  	s.buf = s.buf[:0]
   736  	// Must now have a sign.
   737  	if !s.accept("+-") {
   738  		s.error(complexError)
   739  	}
   740  	// Sign is now in buffer
   741  	imagSign := string(s.buf)
   742  	imag = s.floatToken()
   743  	if !s.accept("i") {
   744  		s.error(complexError)
   745  	}
   746  	if parens && !s.accept(")") {
   747  		s.error(complexError)
   748  	}
   749  	return real, imagSign + imag
   750  }
   751  
   752  // convertFloat converts the string to a float64value.
   753  func (s *ss) convertFloat(str string, n int) float64 {
   754  	if p := indexRune(str, 'p'); p >= 0 {
   755  		// Atof doesn't handle power-of-2 exponents,
   756  		// but they're easy to evaluate.
   757  		f, err := strconv.ParseFloat(str[:p], n)
   758  		if err != nil {
   759  			// Put full string into error.
   760  			if e, ok := err.(*strconv.NumError); ok {
   761  				e.Num = str
   762  			}
   763  			s.error(err)
   764  		}
   765  		m, err := strconv.Atoi(str[p+1:])
   766  		if err != nil {
   767  			// Put full string into error.
   768  			if e, ok := err.(*strconv.NumError); ok {
   769  				e.Num = str
   770  			}
   771  			s.error(err)
   772  		}
   773  		return math.Ldexp(f, m)
   774  	}
   775  	f, err := strconv.ParseFloat(str, n)
   776  	if err != nil {
   777  		s.error(err)
   778  	}
   779  	return f
   780  }
   781  
   782  // convertComplex converts the next token to a complex128 value.
   783  // The atof argument is a type-specific reader for the underlying type.
   784  // If we're reading complex64, atof will parse float32s and convert them
   785  // to float64's to avoid reproducing this code for each complex type.
   786  func (s *ss) scanComplex(verb rune, n int) complex128 {
   787  	if !s.okVerb(verb, floatVerbs, "complex") {
   788  		return 0
   789  	}
   790  	s.SkipSpace()
   791  	s.notEOF()
   792  	sreal, simag := s.complexTokens()
   793  	real := s.convertFloat(sreal, n/2)
   794  	imag := s.convertFloat(simag, n/2)
   795  	return complex(real, imag)
   796  }
   797  
   798  // convertString returns the string represented by the next input characters.
   799  // The format of the input is determined by the verb.
   800  func (s *ss) convertString(verb rune) (str string) {
   801  	if !s.okVerb(verb, "svqxX", "string") {
   802  		return ""
   803  	}
   804  	s.SkipSpace()
   805  	s.notEOF()
   806  	switch verb {
   807  	case 'q':
   808  		str = s.quotedString()
   809  	case 'x', 'X':
   810  		str = s.hexString()
   811  	default:
   812  		str = string(s.token(true, notSpace)) // %s and %v just return the next word
   813  	}
   814  	return
   815  }
   816  
   817  // quotedString returns the double- or back-quoted string represented by the next input characters.
   818  func (s *ss) quotedString() string {
   819  	s.notEOF()
   820  	quote := s.getRune()
   821  	switch quote {
   822  	case '`':
   823  		// Back-quoted: Anything goes until EOF or back quote.
   824  		for {
   825  			r := s.mustReadRune()
   826  			if r == quote {
   827  				break
   828  			}
   829  			s.buf.WriteRune(r)
   830  		}
   831  		return string(s.buf)
   832  	case '"':
   833  		// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
   834  		s.buf.WriteByte('"')
   835  		for {
   836  			r := s.mustReadRune()
   837  			s.buf.WriteRune(r)
   838  			if r == '\\' {
   839  				// In a legal backslash escape, no matter how long, only the character
   840  				// immediately after the escape can itself be a backslash or quote.
   841  				// Thus we only need to protect the first character after the backslash.
   842  				s.buf.WriteRune(s.mustReadRune())
   843  			} else if r == '"' {
   844  				break
   845  			}
   846  		}
   847  		result, err := strconv.Unquote(string(s.buf))
   848  		if err != nil {
   849  			s.error(err)
   850  		}
   851  		return result
   852  	default:
   853  		s.errorString("expected quoted string")
   854  	}
   855  	return ""
   856  }
   857  
   858  // hexDigit returns the value of the hexadecimal digit.
   859  func hexDigit(d rune) (int, bool) {
   860  	digit := int(d)
   861  	switch digit {
   862  	case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
   863  		return digit - '0', true
   864  	case 'a', 'b', 'c', 'd', 'e', 'f':
   865  		return 10 + digit - 'a', true
   866  	case 'A', 'B', 'C', 'D', 'E', 'F':
   867  		return 10 + digit - 'A', true
   868  	}
   869  	return -1, false
   870  }
   871  
   872  // hexByte returns the next hex-encoded (two-character) byte from the input.
   873  // It returns ok==false if the next bytes in the input do not encode a hex byte.
   874  // If the first byte is hex and the second is not, processing stops.
   875  func (s *ss) hexByte() (b byte, ok bool) {
   876  	rune1 := s.getRune()
   877  	if rune1 == eof {
   878  		return
   879  	}
   880  	value1, ok := hexDigit(rune1)
   881  	if !ok {
   882  		s.UnreadRune()
   883  		return
   884  	}
   885  	value2, ok := hexDigit(s.mustReadRune())
   886  	if !ok {
   887  		s.errorString("illegal hex digit")
   888  		return
   889  	}
   890  	return byte(value1<<4 | value2), true
   891  }
   892  
   893  // hexString returns the space-delimited hexpair-encoded string.
   894  func (s *ss) hexString() string {
   895  	s.notEOF()
   896  	for {
   897  		b, ok := s.hexByte()
   898  		if !ok {
   899  			break
   900  		}
   901  		s.buf.WriteByte(b)
   902  	}
   903  	if len(s.buf) == 0 {
   904  		s.errorString("no hex data for %x string")
   905  		return ""
   906  	}
   907  	return string(s.buf)
   908  }
   909  
   910  const (
   911  	floatVerbs = "beEfFgGv"
   912  
   913  	hugeWid = 1 << 30
   914  
   915  	intBits     = 32 << (^uint(0) >> 63)
   916  	uintptrBits = 32 << (^uintptr(0) >> 63)
   917  )
   918  
   919  // scanOne scans a single value, deriving the scanner from the type of the argument.
   920  func (s *ss) scanOne(verb rune, arg interface{}) {
   921  	s.buf = s.buf[:0]
   922  	var err error
   923  	// If the parameter has its own Scan method, use that.
   924  	if v, ok := arg.(Scanner); ok {
   925  		err = v.Scan(s, verb)
   926  		if err != nil {
   927  			if err == io.EOF {
   928  				err = io.ErrUnexpectedEOF
   929  			}
   930  			s.error(err)
   931  		}
   932  		return
   933  	}
   934  
   935  	switch v := arg.(type) {
   936  	case *bool:
   937  		*v = s.scanBool(verb)
   938  	case *complex64:
   939  		*v = complex64(s.scanComplex(verb, 64))
   940  	case *complex128:
   941  		*v = s.scanComplex(verb, 128)
   942  	case *int:
   943  		*v = int(s.scanInt(verb, intBits))
   944  	case *int8:
   945  		*v = int8(s.scanInt(verb, 8))
   946  	case *int16:
   947  		*v = int16(s.scanInt(verb, 16))
   948  	case *int32:
   949  		*v = int32(s.scanInt(verb, 32))
   950  	case *int64:
   951  		*v = s.scanInt(verb, 64)
   952  	case *uint:
   953  		*v = uint(s.scanUint(verb, intBits))
   954  	case *uint8:
   955  		*v = uint8(s.scanUint(verb, 8))
   956  	case *uint16:
   957  		*v = uint16(s.scanUint(verb, 16))
   958  	case *uint32:
   959  		*v = uint32(s.scanUint(verb, 32))
   960  	case *uint64:
   961  		*v = s.scanUint(verb, 64)
   962  	case *uintptr:
   963  		*v = uintptr(s.scanUint(verb, uintptrBits))
   964  	// Floats are tricky because you want to scan in the precision of the result, not
   965  	// scan in high precision and convert, in order to preserve the correct error condition.
   966  	case *float32:
   967  		if s.okVerb(verb, floatVerbs, "float32") {
   968  			s.SkipSpace()
   969  			s.notEOF()
   970  			*v = float32(s.convertFloat(s.floatToken(), 32))
   971  		}
   972  	case *float64:
   973  		if s.okVerb(verb, floatVerbs, "float64") {
   974  			s.SkipSpace()
   975  			s.notEOF()
   976  			*v = s.convertFloat(s.floatToken(), 64)
   977  		}
   978  	case *string:
   979  		*v = s.convertString(verb)
   980  	case *[]byte:
   981  		// We scan to string and convert so we get a copy of the data.
   982  		// If we scanned to bytes, the slice would point at the buffer.
   983  		*v = []byte(s.convertString(verb))
   984  	default:
   985  		val := reflect.ValueOf(v)
   986  		ptr := val
   987  		if ptr.Kind() != reflect.Ptr {
   988  			s.errorString("type not a pointer: " + val.Type().String())
   989  			return
   990  		}
   991  		switch v := ptr.Elem(); v.Kind() {
   992  		case reflect.Bool:
   993  			v.SetBool(s.scanBool(verb))
   994  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   995  			v.SetInt(s.scanInt(verb, v.Type().Bits()))
   996  		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
   997  			v.SetUint(s.scanUint(verb, v.Type().Bits()))
   998  		case reflect.String:
   999  			v.SetString(s.convertString(verb))
  1000  		case reflect.Slice:
  1001  			// For now, can only handle (renamed) []byte.
  1002  			typ := v.Type()
  1003  			if typ.Elem().Kind() != reflect.Uint8 {
  1004  				s.errorString("can't scan type: " + val.Type().String())
  1005  			}
  1006  			str := s.convertString(verb)
  1007  			v.Set(reflect.MakeSlice(typ, len(str), len(str)))
  1008  			for i := 0; i < len(str); i++ {
  1009  				v.Index(i).SetUint(uint64(str[i]))
  1010  			}
  1011  		case reflect.Float32, reflect.Float64:
  1012  			s.SkipSpace()
  1013  			s.notEOF()
  1014  			v.SetFloat(s.convertFloat(s.floatToken(), v.Type().Bits()))
  1015  		case reflect.Complex64, reflect.Complex128:
  1016  			v.SetComplex(s.scanComplex(verb, v.Type().Bits()))
  1017  		default:
  1018  			s.errorString("can't scan type: " + val.Type().String())
  1019  		}
  1020  	}
  1021  }
  1022  
  1023  // errorHandler turns local panics into error returns.
  1024  func errorHandler(errp *error) {
  1025  	if e := recover(); e != nil {
  1026  		if se, ok := e.(scanError); ok { // catch local error
  1027  			*errp = se.err
  1028  		} else if eof, ok := e.(error); ok && eof == io.EOF { // out of input
  1029  			*errp = eof
  1030  		} else {
  1031  			panic(e)
  1032  		}
  1033  	}
  1034  }
  1035  
  1036  // doScan does the real work for scanning without a format string.
  1037  func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
  1038  	defer errorHandler(&err)
  1039  	for _, arg := range a {
  1040  		s.scanOne('v', arg)
  1041  		numProcessed++
  1042  	}
  1043  	// Check for newline (or EOF) if required (Scanln etc.).
  1044  	if s.nlIsEnd {
  1045  		for {
  1046  			r := s.getRune()
  1047  			if r == '\n' || r == eof {
  1048  				break
  1049  			}
  1050  			if !isSpace(r) {
  1051  				s.errorString("expected newline")
  1052  				break
  1053  			}
  1054  		}
  1055  	}
  1056  	return
  1057  }
  1058  
  1059  // advance determines whether the next characters in the input match
  1060  // those of the format. It returns the number of bytes (sic) consumed
  1061  // in the format. All runs of space characters in either input or
  1062  // format behave as a single space. Newlines are special, though:
  1063  // newlines in the format must match those in the input and vice versa.
  1064  // This routine also handles the %% case. If the return value is zero,
  1065  // either format starts with a % (with no following %) or the input
  1066  // is empty. If it is negative, the input did not match the string.
  1067  func (s *ss) advance(format string) (i int) {
  1068  	for i < len(format) {
  1069  		fmtc, w := utf8.DecodeRuneInString(format[i:])
  1070  
  1071  		// Space processing.
  1072  		// In the rest of this comment "space" means spaces other than newline.
  1073  		// Newline in the format matches input of zero or more spaces and then newline or end-of-input.
  1074  		// Spaces in the format before the newline are collapsed into the newline.
  1075  		// Spaces in the format after the newline match zero or more spaces after the corresponding input newline.
  1076  		// Other spaces in the format match input of one or more spaces or end-of-input.
  1077  		if isSpace(fmtc) {
  1078  			newlines := 0
  1079  			trailingSpace := false
  1080  			for isSpace(fmtc) && i < len(format) {
  1081  				if fmtc == '\n' {
  1082  					newlines++
  1083  					trailingSpace = false
  1084  				} else {
  1085  					trailingSpace = true
  1086  				}
  1087  				i += w
  1088  				fmtc, w = utf8.DecodeRuneInString(format[i:])
  1089  			}
  1090  			for j := 0; j < newlines; j++ {
  1091  				inputc := s.getRune()
  1092  				for isSpace(inputc) && inputc != '\n' {
  1093  					inputc = s.getRune()
  1094  				}
  1095  				if inputc != '\n' && inputc != eof {
  1096  					s.errorString("newline in format does not match input")
  1097  				}
  1098  			}
  1099  			if trailingSpace {
  1100  				inputc := s.getRune()
  1101  				if newlines == 0 {
  1102  					// If the trailing space stood alone (did not follow a newline),
  1103  					// it must find at least one space to consume.
  1104  					if !isSpace(inputc) && inputc != eof {
  1105  						s.errorString("expected space in input to match format")
  1106  					}
  1107  					if inputc == '\n' {
  1108  						s.errorString("newline in input does not match format")
  1109  					}
  1110  				}
  1111  				for isSpace(inputc) && inputc != '\n' {
  1112  					inputc = s.getRune()
  1113  				}
  1114  				if inputc != eof {
  1115  					s.UnreadRune()
  1116  				}
  1117  			}
  1118  			continue
  1119  		}
  1120  
  1121  		// Verbs.
  1122  		if fmtc == '%' {
  1123  			// % at end of string is an error.
  1124  			if i+w == len(format) {
  1125  				s.errorString("missing verb: % at end of format string")
  1126  			}
  1127  			// %% acts like a real percent
  1128  			nextc, _ := utf8.DecodeRuneInString(format[i+w:]) // will not match % if string is empty
  1129  			if nextc != '%' {
  1130  				return
  1131  			}
  1132  			i += w // skip the first %
  1133  		}
  1134  
  1135  		// Literals.
  1136  		inputc := s.mustReadRune()
  1137  		if fmtc != inputc {
  1138  			s.UnreadRune()
  1139  			return -1
  1140  		}
  1141  		i += w
  1142  	}
  1143  	return
  1144  }
  1145  
  1146  // doScanf does the real work when scanning with a format string.
  1147  // At the moment, it handles only pointers to basic types.
  1148  func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err error) {
  1149  	defer errorHandler(&err)
  1150  	end := len(format) - 1
  1151  	// We process one item per non-trivial format
  1152  	for i := 0; i <= end; {
  1153  		w := s.advance(format[i:])
  1154  		if w > 0 {
  1155  			i += w
  1156  			continue
  1157  		}
  1158  		// Either we failed to advance, we have a percent character, or we ran out of input.
  1159  		if format[i] != '%' {
  1160  			// Can't advance format. Why not?
  1161  			if w < 0 {
  1162  				s.errorString("input does not match format")
  1163  			}
  1164  			// Otherwise at EOF; "too many operands" error handled below
  1165  			break
  1166  		}
  1167  		i++ // % is one byte
  1168  
  1169  		// do we have 20 (width)?
  1170  		var widPresent bool
  1171  		s.maxWid, widPresent, i = parsenum(format, i, end)
  1172  		if !widPresent {
  1173  			s.maxWid = hugeWid
  1174  		}
  1175  
  1176  		c, w := utf8.DecodeRuneInString(format[i:])
  1177  		i += w
  1178  
  1179  		if c != 'c' {
  1180  			s.SkipSpace()
  1181  		}
  1182  		s.argLimit = s.limit
  1183  		if f := s.count + s.maxWid; f < s.argLimit {
  1184  			s.argLimit = f
  1185  		}
  1186  
  1187  		if numProcessed >= len(a) { // out of operands
  1188  			s.errorString("too few operands for format '%" + format[i-w:] + "'")
  1189  			break
  1190  		}
  1191  		arg := a[numProcessed]
  1192  
  1193  		s.scanOne(c, arg)
  1194  		numProcessed++
  1195  		s.argLimit = s.limit
  1196  	}
  1197  	if numProcessed < len(a) {
  1198  		s.errorString("too many operands")
  1199  	}
  1200  	return
  1201  }
  1202  

View as plain text