Run Format

Source file src/pkg/fmt/scan.go

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

View as plain text