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.
18 // runeUnreader is the interface to something that can unread runes.
19 // If the object provided to Scan does not satisfy this interface,
20 // a local buffer will be used to back up the input, but its contents
21 // will be lost when Scan returns.
22 type runeUnreader
interface {
26 // ScanState represents the scanner state passed to custom scanners.
27 // Scanners may do rune-at-a-time scanning or ask the ScanState
28 // to discover the next space-delimited token.
29 type ScanState
interface {
30 // ReadRune reads the next rune (Unicode code point) from the input.
31 // If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
32 // return EOF after returning the first '\n' or when reading beyond
33 // the specified width.
34 ReadRune() (r rune
, size
int, err error
)
35 // UnreadRune causes the next call to ReadRune to return the same rune.
37 // SkipSpace skips space in the input. Newlines are treated appropriately
38 // for the operation being performed; see the package documentation
39 // for more information.
41 // Token skips space in the input if skipSpace is true, then returns the
42 // run of Unicode code points c satisfying f(c). If f is nil,
43 // !unicode.IsSpace(c) is used; that is, the token will hold non-space
44 // characters. Newlines are treated appropriately for the operation being
45 // performed; see the package documentation for more information.
46 // The returned slice points to shared data that may be overwritten
47 // by the next call to Token, a call to a Scan function using the ScanState
48 // as input, or when the calling Scan method returns.
49 Token(skipSpace
bool, f
func(rune
) bool) (token
[]byte, err error
)
50 // Width returns the value of the width option and whether it has been set.
51 // The unit is Unicode code points.
52 Width() (wid
int, ok
bool)
53 // Because ReadRune is implemented by the interface, Read should never be
54 // called by the scanning routines and a valid implementation of
55 // ScanState may choose always to return an error from Read.
56 Read(buf
[]byte) (n
int, err error
)
59 // Scanner is implemented by any value that has a Scan method, which scans
60 // the input for the representation of a value and stores the result in the
61 // receiver, which must be a pointer to be useful. The Scan method is called
62 // for any argument to Scan, Scanf, or Scanln that implements it.
63 type Scanner
interface {
64 Scan(state ScanState
, verb rune
) error
67 // Scan scans text read from standard input, storing successive
68 // space-separated values into successive arguments. Newlines count
69 // as space. It returns the number of items successfully scanned.
70 // If that is less than the number of arguments, err will report why.
71 func Scan(a
...interface{}) (n
int, err error
) {
72 return Fscan(os
.Stdin
, a
...)
75 // Scanln is similar to Scan, but stops scanning at a newline and
76 // after the final item there must be a newline or EOF.
77 func Scanln(a
...interface{}) (n
int, err error
) {
78 return Fscanln(os
.Stdin
, a
...)
81 // Scanf scans text read from standard input, storing successive
82 // space-separated values into successive arguments as determined by
83 // the format. It returns the number of items successfully scanned.
84 // If that is less than the number of arguments, err will report why.
85 // Newlines in the input must match newlines in the format.
86 // The one exception: the verb %c always scans the next rune in the
87 // input, even if it is a space (or tab etc.) or newline.
88 func Scanf(format
string, a
...interface{}) (n
int, err error
) {
89 return Fscanf(os
.Stdin
, format
, a
...)
92 type stringReader
string
94 func (r
*stringReader
) Read(b
[]byte) (n
int, err error
) {
103 // Sscan scans the argument string, storing successive space-separated
104 // values into successive arguments. Newlines count as space. It
105 // returns the number of items successfully scanned. If that is less
106 // than the number of arguments, err will report why.
107 func Sscan(str
string, a
...interface{}) (n
int, err error
) {
108 return Fscan((*stringReader
)(&str
), a
...)
111 // Sscanln is similar to Sscan, but stops scanning at a newline and
112 // after the final item there must be a newline or EOF.
113 func Sscanln(str
string, a
...interface{}) (n
int, err error
) {
114 return Fscanln((*stringReader
)(&str
), a
...)
117 // Sscanf scans the argument string, storing successive space-separated
118 // values into successive arguments as determined by the format. It
119 // returns the number of items successfully parsed.
120 // Newlines in the input must match newlines in the format.
121 func Sscanf(str
string, format
string, a
...interface{}) (n
int, err error
) {
122 return Fscanf((*stringReader
)(&str
), format
, a
...)
125 // Fscan scans text read from r, storing successive space-separated
126 // values into successive arguments. Newlines count as space. It
127 // returns the number of items successfully scanned. If that is less
128 // than the number of arguments, err will report why.
129 func Fscan(r io
.Reader
, a
...interface{}) (n
int, err error
) {
130 s
, old
:= newScanState(r
, true, false)
136 // Fscanln is similar to Fscan, but stops scanning at a newline and
137 // after the final item there must be a newline or EOF.
138 func Fscanln(r io
.Reader
, a
...interface{}) (n
int, err error
) {
139 s
, old
:= newScanState(r
, false, true)
145 // Fscanf scans text read from r, storing successive space-separated
146 // values into successive arguments as determined by the format. It
147 // returns the number of items successfully parsed.
148 // Newlines in the input must match newlines in the format.
149 func Fscanf(r io
.Reader
, format
string, a
...interface{}) (n
int, err error
) {
150 s
, old
:= newScanState(r
, false, false)
151 n
, err
= s
.doScanf(format
, a
)
156 // scanError represents an error generated by the scanning software.
157 // It's used as a unique signature to identify such errors when recovering.
158 type scanError
struct {
164 // ss is the internal implementation of ScanState.
166 rr io
.RuneReader
// where to read input
167 buf buffer
// token accumulator
168 peekRune rune
// one-rune lookahead
169 prevRune rune
// last rune returned by ReadRune
170 count
int // runes consumed so far.
171 atEOF
bool // already read EOF
175 // ssave holds the parts of ss that need to be
176 // saved and restored on recursive scans.
178 validSave
bool // is or was a part of an actual ss.
179 nlIsEnd
bool // whether newline terminates scan
180 nlIsSpace
bool // whether newline counts as white space
181 argLimit
int // max value of ss.count for this arg; argLimit <= limit
182 limit
int // max value of ss.count.
183 maxWid
int // width of this arg.
186 // The Read method is only in ScanState so that ScanState
187 // satisfies io.Reader. It will never be called when used as
188 // intended, so there is no need to make it actually work.
189 func (s
*ss
) Read(buf
[]byte) (n
int, err error
) {
190 return 0, errors
.New("ScanState's Read should not be called. Use ReadRune")
193 func (s
*ss
) ReadRune() (r rune
, size
int, err error
) {
197 size
= utf8
.RuneLen(r
)
202 if s
.atEOF || s
.nlIsEnd
&& s
.prevRune
== '\n' || s
.count
>= s
.argLimit
{
207 r
, size
, err
= s
.rr
.ReadRune()
211 } else if err
== io
.EOF
{
217 func (s
*ss
) Width() (wid
int, ok
bool) {
218 if s
.maxWid
== hugeWid
{
221 return s
.maxWid
, true
224 // The public method returns an error; this private one panics.
225 // If getRune reaches EOF, the return value is EOF (-1).
226 func (s
*ss
) getRune() (r rune
) {
227 r
, _
, err
:= s
.ReadRune()
237 // mustReadRune turns io.EOF into a panic(io.ErrUnexpectedEOF).
238 // It is called in cases such as string scanning where an EOF is a
240 func (s
*ss
) mustReadRune() (r rune
) {
243 s
.error(io
.ErrUnexpectedEOF
)
248 func (s
*ss
) UnreadRune() error
{
249 if u
, ok
:= s
.rr
.(runeUnreader
); ok
{
252 s
.peekRune
= s
.prevRune
259 func (s
*ss
) error(err error
) {
260 panic(scanError
{err
})
263 func (s
*ss
) errorString(err
string) {
264 panic(scanError
{errors
.New(err
)})
267 func (s
*ss
) Token(skipSpace
bool, f
func(rune
) bool) (tok
[]byte, err error
) {
269 if e
:= recover(); e
!= nil {
270 if se
, ok
:= e
.(scanError
); ok
{
281 tok
= s
.token(skipSpace
, f
)
285 // space is a copy of the unicode.White_Space ranges,
286 // to avoid depending on package unicode.
287 var space
= [][2]uint16{
300 func isSpace(r rune
) bool {
305 for _
, rng
:= range space
{
316 // notSpace is the default scanning function used in Token.
317 func notSpace(r rune
) bool {
321 // SkipSpace provides Scan methods the ability to skip space and newline
322 // characters in keeping with the current scanning mode set by format strings
324 func (s
*ss
) SkipSpace() {
328 // readRune is a structure to enable reading UTF-8 encoded code points
329 // from an io.Reader. It is used if the Reader given to the scanner does
330 // not already implement io.RuneReader.
331 type readRune
struct {
333 buf
[utf8
.UTFMax
]byte // used only inside ReadRune
334 pending
int // number of bytes in pendBuf; only >0 for bad UTF-8
335 pendBuf
[utf8
.UTFMax
]byte // bytes left over
338 // readByte returns the next byte from the input, which may be
339 // left over from a previous read if the UTF-8 was ill-formed.
340 func (r
*readRune
) readByte() (b
byte, err error
) {
343 copy(r
.pendBuf
[0:], r
.pendBuf
[1:])
347 n
, err
:= io
.ReadFull(r
.reader
, r
.pendBuf
[0:1])
351 return r
.pendBuf
[0], err
354 // unread saves the bytes for the next read.
355 func (r
*readRune
) unread(buf
[]byte) {
356 copy(r
.pendBuf
[r
.pending
:], buf
)
357 r
.pending
+= len(buf
)
360 // ReadRune returns the next UTF-8 encoded code point from the
361 // io.Reader inside r.
362 func (r
*readRune
) ReadRune() (rr rune
, size
int, err error
) {
363 r
.buf
[0], err
= r
.readByte()
367 if r
.buf
[0] < utf8
.RuneSelf
{ // fast check for common ASCII case
369 size
= 1 // Known to be 1.
373 for n
= 1; !utf8
.FullRune(r
.buf
[0:n
]); n
++ {
374 r
.buf
[n
], err
= r
.readByte()
383 rr
, size
= utf8
.DecodeRune(r
.buf
[0:n
])
384 if size
< n
{ // an error
385 r
.unread(r
.buf
[size
:n
])
390 var ssFree
= sync
.Pool
{
391 New
: func() interface{} { return new(ss
) },
394 // newScanState allocates a new ss struct or grab a cached one.
395 func newScanState(r io
.Reader
, nlIsSpace
, nlIsEnd
bool) (s
*ss
, old ssave
) {
396 s
= ssFree
.Get().(*ss
)
397 if rr
, ok
:= r
.(io
.RuneReader
); ok
{
400 s
.rr
= &readRune
{reader
: r
}
402 s
.nlIsSpace
= nlIsSpace
415 // free saves used ss structs in ssFree; avoid an allocation per invocation.
416 func (s
*ss
) free(old ssave
) {
417 // If it was used recursively, just restore the old state.
422 // Don't hold on to ss structs with large buffers.
423 if cap(s
.buf
) > 1024 {
431 // skipSpace skips spaces and maybe newlines.
432 func (s
*ss
) skipSpace(stopAtNewline
bool) {
438 if r
== '\r' && s
.peek("\n") {
448 s
.errorString("unexpected newline")
458 // token returns the next space-delimited string from the input. It
459 // skips white space. For Scanln, it stops at newlines. For Scan,
460 // newlines are treated as spaces.
461 func (s
*ss
) token(skipSpace
bool, f
func(rune
) bool) []byte {
465 // read until white space or newline
480 var complexError
= errors
.New("syntax error scanning complex number")
481 var boolError
= errors
.New("syntax error scanning boolean")
483 func indexRune(s
string, r rune
) int {
484 for i
, c
:= range s
{
492 // consume reads the next rune in the input and reports whether it is in the ok string.
493 // If accept is true, it puts the character into the input token.
494 func (s
*ss
) consume(ok
string, accept
bool) bool {
499 if indexRune(ok
, r
) >= 0 {
505 if r
!= eof
&& accept
{
511 // peek reports whether the next character is in the ok string, without consuming it.
512 func (s
*ss
) peek(ok
string) bool {
517 return indexRune(ok
, r
) >= 0
520 func (s
*ss
) notEOF() {
521 // Guarantee there is data to be read.
522 if r
:= s
.getRune(); r
== eof
{
528 // accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the
529 // buffer and returns true. Otherwise it return false.
530 func (s
*ss
) accept(ok
string) bool {
531 return s
.consume(ok
, true)
534 // okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
535 func (s
*ss
) okVerb(verb rune
, okVerbs
, typ
string) bool {
536 for _
, v
:= range okVerbs
{
541 s
.errorString("bad verb %" + string(verb
) + " for " + typ
)
545 // scanBool returns the value of the boolean represented by the next token.
546 func (s
*ss
) scanBool(verb rune
) bool {
549 if !s
.okVerb(verb
, "tv", "boolean") {
552 // Syntax-checking a boolean is annoying. We're not fastidious about case.
559 if s
.accept("rR") && (!s
.accept("uU") ||
!s
.accept("eE")) {
564 if s
.accept("aA") && (!s
.accept("lL") ||
!s
.accept("sS") ||
!s
.accept("eE")) {
572 // Numerical elements
575 octalDigits
= "01234567"
576 decimalDigits
= "0123456789"
577 hexadecimalDigits
= "0123456789aAbBcCdDeEfF"
583 // getBase returns the numeric base represented by the verb and its digit string.
584 func (s
*ss
) getBase(verb rune
) (base
int, digits
string) {
585 s
.okVerb(verb
, "bdoUxXv", "integer") // sets s.err
587 digits
= decimalDigits
591 digits
= binaryDigits
597 digits
= hexadecimalDigits
602 // scanNumber returns the numerical string with specified digits starting here.
603 func (s
*ss
) scanNumber(digits
string, haveDigits
bool) string {
606 if !s
.accept(digits
) {
607 s
.errorString("expected integer")
610 for s
.accept(digits
) {
615 // scanRune returns the next rune value in the input.
616 func (s
*ss
) scanRune(bitSize
int) int64 {
618 r
:= int64(s
.getRune())
620 x
:= (r
<< (64 - n
)) >> (64 - n
)
622 s
.errorString("overflow on character value " + string(r
))
627 // scanBasePrefix reports whether the integer begins with a 0 or 0x,
628 // and returns the base, digit string, and whether a zero was found.
629 // It is called only if the verb is %v.
630 func (s
*ss
) scanBasePrefix() (base
int, digits
string, found
bool) {
632 return 10, decimalDigits
, false
635 found
= true // We've put a digit into the token buffer.
636 // Special cases for '0' && '0x'
637 base
, digits
= 8, octalDigits
639 s
.consume("xX", false)
640 base
, digits
= 16, hexadecimalDigits
645 // scanInt returns the value of the integer represented by the next
646 // token, checking for overflow. Any error is stored in s.err.
647 func (s
*ss
) scanInt(verb rune
, bitSize
int) int64 {
649 return s
.scanRune(bitSize
)
653 base
, digits
:= s
.getBase(verb
)
656 if !s
.consume("U", false) ||
!s
.consume("+", false) {
657 s
.errorString("bad unicode format ")
660 s
.accept(sign
) // If there's a sign, it will be left in the token buffer.
662 base
, digits
, haveDigits
= s
.scanBasePrefix()
665 tok
:= s
.scanNumber(digits
, haveDigits
)
666 i
, err
:= strconv
.ParseInt(tok
, base
, 64)
671 x
:= (i
<< (64 - n
)) >> (64 - n
)
673 s
.errorString("integer overflow on token " + tok
)
678 // scanUint returns the value of the unsigned integer represented
679 // by the next token, checking for overflow. Any error is stored in s.err.
680 func (s
*ss
) scanUint(verb rune
, bitSize
int) uint64 {
682 return uint64(s
.scanRune(bitSize
))
686 base
, digits
:= s
.getBase(verb
)
689 if !s
.consume("U", false) ||
!s
.consume("+", false) {
690 s
.errorString("bad unicode format ")
692 } else if verb
== 'v' {
693 base
, digits
, haveDigits
= s
.scanBasePrefix()
695 tok
:= s
.scanNumber(digits
, haveDigits
)
696 i
, err
:= strconv
.ParseUint(tok
, base
, 64)
701 x
:= (i
<< (64 - n
)) >> (64 - n
)
703 s
.errorString("unsigned integer overflow on token " + tok
)
708 // floatToken returns the floating-point number starting here, no longer than swid
709 // if the width is specified. It's not rigorous about syntax because it doesn't check that
710 // we have at least some digits, but Atof will do that.
711 func (s
*ss
) floatToken() string {
714 if s
.accept("nN") && s
.accept("aA") && s
.accept("nN") {
720 if s
.accept("iI") && s
.accept("nN") && s
.accept("fF") {
724 for s
.accept(decimalDigits
) {
727 if s
.accept(period
) {
729 for s
.accept(decimalDigits
) {
733 if s
.accept(exponent
) {
737 for s
.accept(decimalDigits
) {
743 // complexTokens returns the real and imaginary parts of the complex number starting here.
744 // The number might be parenthesized and has the format (N+Ni) where N is a floating-point
745 // number and there are no spaces within.
746 func (s
*ss
) complexTokens() (real
, imag
string) {
747 // TODO: accept N and Ni independently?
748 parens
:= s
.accept("(")
749 real
= s
.floatToken()
751 // Must now have a sign.
753 s
.error(complexError
)
755 // Sign is now in buffer
756 imagSign
:= string(s
.buf
)
757 imag
= s
.floatToken()
759 s
.error(complexError
)
761 if parens
&& !s
.accept(")") {
762 s
.error(complexError
)
764 return real
, imagSign
+ imag
767 // convertFloat converts the string to a float64value.
768 func (s
*ss
) convertFloat(str
string, n
int) float64 {
769 if p
:= indexRune(str
, 'p'); p
>= 0 {
770 // Atof doesn't handle power-of-2 exponents,
771 // but they're easy to evaluate.
772 f
, err
:= strconv
.ParseFloat(str
[:p
], n
)
774 // Put full string into error.
775 if e
, ok
:= err
.(*strconv
.NumError
); ok
{
780 m
, err
:= strconv
.Atoi(str
[p
+1:])
782 // Put full string into error.
783 if e
, ok
:= err
.(*strconv
.NumError
); ok
{
788 return math
.Ldexp(f
, m
)
790 f
, err
:= strconv
.ParseFloat(str
, n
)
797 // convertComplex converts the next token to a complex128 value.
798 // The atof argument is a type-specific reader for the underlying type.
799 // If we're reading complex64, atof will parse float32s and convert them
800 // to float64's to avoid reproducing this code for each complex type.
801 func (s
*ss
) scanComplex(verb rune
, n
int) complex128
{
802 if !s
.okVerb(verb
, floatVerbs
, "complex") {
807 sreal
, simag
:= s
.complexTokens()
808 real
:= s
.convertFloat(sreal
, n
/2)
809 imag
:= s
.convertFloat(simag
, n
/2)
810 return complex(real
, imag
)
813 // convertString returns the string represented by the next input characters.
814 // The format of the input is determined by the verb.
815 func (s
*ss
) convertString(verb rune
) (str
string) {
816 if !s
.okVerb(verb
, "svqx", "string") {
823 str
= s
.quotedString()
827 str
= string(s
.token(true, notSpace
)) // %s and %v just return the next word
832 // quotedString returns the double- or back-quoted string represented by the next input characters.
833 func (s
*ss
) quotedString() string {
838 // Back-quoted: Anything goes until EOF or back quote.
840 r
:= s
.mustReadRune()
848 // Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
849 s
.buf
.WriteRune(quote
)
851 r
:= s
.mustReadRune()
854 // In a legal backslash escape, no matter how long, only the character
855 // immediately after the escape can itself be a backslash or quote.
856 // Thus we only need to protect the first character after the backslash.
857 s
.buf
.WriteRune(s
.mustReadRune())
862 result
, err
:= strconv
.Unquote(string(s
.buf
))
868 s
.errorString("expected quoted string")
873 // hexDigit returns the value of the hexadecimal digit.
874 func hexDigit(d rune
) (int, bool) {
877 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
878 return digit
- '0', true
879 case 'a', 'b', 'c', 'd', 'e', 'f':
880 return 10 + digit
- 'a', true
881 case 'A', 'B', 'C', 'D', 'E', 'F':
882 return 10 + digit
- 'A', true
887 // hexByte returns the next hex-encoded (two-character) byte from the input.
888 // It returns ok==false if the next bytes in the input do not encode a hex byte.
889 // If the first byte is hex and the second is not, processing stops.
890 func (s
*ss
) hexByte() (b
byte, ok
bool) {
895 value1
, ok
:= hexDigit(rune1
)
900 value2
, ok
:= hexDigit(s
.mustReadRune())
902 s
.errorString("illegal hex digit")
905 return byte(value1
<<4 | value2
), true
908 // hexString returns the space-delimited hexpair-encoded string.
909 func (s
*ss
) hexString() string {
919 s
.errorString("no hex data for %x string")
925 const floatVerbs
= "beEfFgGv"
927 const hugeWid
= 1 << 30
929 // scanOne scans a single value, deriving the scanner from the type of the argument.
930 func (s
*ss
) scanOne(verb rune
, arg
interface{}) {
933 // If the parameter has its own Scan method, use that.
934 if v
, ok
:= arg
.(Scanner
); ok
{
935 err
= v
.Scan(s
, verb
)
938 err
= io
.ErrUnexpectedEOF
945 switch v
:= arg
.(type) {
947 *v
= s
.scanBool(verb
)
949 *v
= complex64(s
.scanComplex(verb
, 64))
951 *v
= s
.scanComplex(verb
, 128)
953 *v
= int(s
.scanInt(verb
, intBits
))
955 *v
= int8(s
.scanInt(verb
, 8))
957 *v
= int16(s
.scanInt(verb
, 16))
959 *v
= int32(s
.scanInt(verb
, 32))
961 *v
= s
.scanInt(verb
, 64)
963 *v
= uint(s
.scanUint(verb
, intBits
))
965 *v
= uint8(s
.scanUint(verb
, 8))
967 *v
= uint16(s
.scanUint(verb
, 16))
969 *v
= uint32(s
.scanUint(verb
, 32))
971 *v
= s
.scanUint(verb
, 64)
973 *v
= uintptr(s
.scanUint(verb
, uintptrBits
))
974 // Floats are tricky because you want to scan in the precision of the result, not
975 // scan in high precision and convert, in order to preserve the correct error condition.
977 if s
.okVerb(verb
, floatVerbs
, "float32") {
980 *v
= float32(s
.convertFloat(s
.floatToken(), 32))
983 if s
.okVerb(verb
, floatVerbs
, "float64") {
986 *v
= s
.convertFloat(s
.floatToken(), 64)
989 *v
= s
.convertString(verb
)
991 // We scan to string and convert so we get a copy of the data.
992 // If we scanned to bytes, the slice would point at the buffer.
993 *v
= []byte(s
.convertString(verb
))
995 val
:= reflect
.ValueOf(v
)
997 if ptr
.Kind() != reflect
.Ptr
{
998 s
.errorString("type not a pointer: " + val
.Type().String())
1001 switch v
:= ptr
.Elem(); v
.Kind() {
1003 v
.SetBool(s
.scanBool(verb
))
1004 case reflect
.Int
, reflect
.Int8
, reflect
.Int16
, reflect
.Int32
, reflect
.Int64
:
1005 v
.SetInt(s
.scanInt(verb
, v
.Type().Bits()))
1006 case reflect
.Uint
, reflect
.Uint8
, reflect
.Uint16
, reflect
.Uint32
, reflect
.Uint64
, reflect
.Uintptr
:
1007 v
.SetUint(s
.scanUint(verb
, v
.Type().Bits()))
1008 case reflect
.String
:
1009 v
.SetString(s
.convertString(verb
))
1011 // For now, can only handle (renamed) []byte.
1013 if typ
.Elem().Kind() != reflect
.Uint8
{
1014 s
.errorString("can't scan type: " + val
.Type().String())
1016 str
:= s
.convertString(verb
)
1017 v
.Set(reflect
.MakeSlice(typ
, len(str
), len(str
)))
1018 for i
:= 0; i
< len(str
); i
++ {
1019 v
.Index(i
).SetUint(uint64(str
[i
]))
1021 case reflect
.Float32
, reflect
.Float64
:
1024 v
.SetFloat(s
.convertFloat(s
.floatToken(), v
.Type().Bits()))
1025 case reflect
.Complex64
, reflect
.Complex128
:
1026 v
.SetComplex(s
.scanComplex(verb
, v
.Type().Bits()))
1028 s
.errorString("can't scan type: " + val
.Type().String())
1033 // errorHandler turns local panics into error returns.
1034 func errorHandler(errp
*error
) {
1035 if e
:= recover(); e
!= nil {
1036 if se
, ok
:= e
.(scanError
); ok
{ // catch local error
1038 } else if eof
, ok
:= e
.(error
); ok
&& eof
== io
.EOF
{ // out of input
1046 // doScan does the real work for scanning without a format string.
1047 func (s
*ss
) doScan(a
[]interface{}) (numProcessed
int, err error
) {
1048 defer errorHandler(&err
)
1049 for _
, arg
:= range a
{
1053 // Check for newline (or EOF) if required (Scanln etc.).
1057 if r
== '\n' || r
== eof
{
1061 s
.errorString("expected newline")
1069 // advance determines whether the next characters in the input match
1070 // those of the format. It returns the number of bytes (sic) consumed
1071 // in the format. All runs of space characters in either input or
1072 // format behave as a single space. Newlines are special, though:
1073 // newlines in the format must match those in the input and vice versa.
1074 // This routine also handles the %% case. If the return value is zero,
1075 // either format starts with a % (with no following %) or the input
1076 // is empty. If it is negative, the input did not match the string.
1077 func (s
*ss
) advance(format
string) (i
int) {
1078 for i
< len(format
) {
1079 fmtc
, w
:= utf8
.DecodeRuneInString(format
[i
:])
1081 // %% acts like a real percent
1082 nextc
, _
:= utf8
.DecodeRuneInString(format
[i
+w
:]) // will not match % if string is empty
1086 i
+= w
// skip the first %
1090 // Skip spaces in format but absorb at most one newline.
1091 for isSpace(fmtc
) && i
< len(format
) {
1093 if wasNewline
{ // Already saw one; stop here.
1100 fmtc
, w
= utf8
.DecodeRuneInString(format
[i
:])
1103 // There was space in the format, so there should be space
1105 inputc
:= s
.getRune()
1109 if !isSpace(inputc
) {
1110 // Space in format but not in input.
1111 s
.errorString("expected space in input to match format")
1113 // Skip spaces but stop at newline.
1114 for inputc
!= '\n' && isSpace(inputc
) {
1115 inputc
= s
.getRune()
1119 s
.errorString("newline in input does not match format")
1121 // We've reached a newline, stop now; don't read further.
1126 s
.errorString("newline in format does not match input")
1130 inputc
:= s
.mustReadRune()
1140 // doScanf does the real work when scanning with a format string.
1141 // At the moment, it handles only pointers to basic types.
1142 func (s
*ss
) doScanf(format
string, a
[]interface{}) (numProcessed
int, err error
) {
1143 defer errorHandler(&err
)
1144 end
:= len(format
) - 1
1145 // We process one item per non-trivial format
1146 for i
:= 0; i
<= end
; {
1147 w
:= s
.advance(format
[i
:])
1152 // Either we failed to advance, we have a percent character, or we ran out of input.
1153 if format
[i
] != '%' {
1154 // Can't advance format. Why not?
1156 s
.errorString("input does not match format")
1158 // Otherwise at EOF; "too many operands" error handled below
1161 i
++ // % is one byte
1163 // do we have 20 (width)?
1165 s
.maxWid
, widPresent
, i
= parsenum(format
, i
, end
)
1170 c
, w
:= utf8
.DecodeRuneInString(format
[i
:])
1176 s
.argLimit
= s
.limit
1177 if f
:= s
.count
+ s
.maxWid
; f
< s
.argLimit
{
1181 if numProcessed
>= len(a
) { // out of operands
1182 s
.errorString("too few operands for format %" + format
[i
-w
:])
1185 arg
:= a
[numProcessed
]
1189 s
.argLimit
= s
.limit
1191 if numProcessed
< len(a
) {
1192 s
.errorString("too many operands")