2010-05-25 22:02:35 -06:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package fmt
|
|
|
|
|
|
|
|
import (
|
2011-11-01 20:04:37 -06:00
|
|
|
"errors"
|
2010-05-25 22:02:35 -06:00
|
|
|
"io"
|
2011-02-02 13:38:48 -07:00
|
|
|
"math"
|
2010-05-25 22:02:35 -06:00
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2013-12-18 12:09:07 -07:00
|
|
|
"sync"
|
2011-11-08 16:40:58 -07:00
|
|
|
"unicode/utf8"
|
2010-05-25 22:02:35 -06:00
|
|
|
)
|
|
|
|
|
2010-05-26 19:15:09 -06:00
|
|
|
// ScanState represents the scanner state passed to custom scanners.
|
|
|
|
// Scanners may do rune-at-a-time scanning or ask the ScanState
|
|
|
|
// to discover the next space-delimited token.
|
|
|
|
type ScanState interface {
|
2011-03-02 11:54:23 -07:00
|
|
|
// ReadRune reads the next rune (Unicode code point) from the input.
|
|
|
|
// If invoked during Scanln, Fscanln, or Sscanln, ReadRune() will
|
|
|
|
// return EOF after returning the first '\n' or when reading beyond
|
|
|
|
// the specified width.
|
2011-11-01 20:04:37 -06:00
|
|
|
ReadRune() (r rune, size int, err error)
|
2011-03-02 11:54:23 -07:00
|
|
|
// UnreadRune causes the next call to ReadRune to return the same rune.
|
2011-11-01 20:04:37 -06:00
|
|
|
UnreadRune() error
|
2015-06-05 15:23:54 -06:00
|
|
|
// SkipSpace skips space in the input. Newlines are treated appropriately
|
|
|
|
// for the operation being performed; see the package documentation
|
|
|
|
// for more information.
|
2011-06-24 18:26:45 -06:00
|
|
|
SkipSpace()
|
2011-03-09 11:01:47 -07:00
|
|
|
// Token skips space in the input if skipSpace is true, then returns the
|
|
|
|
// run of Unicode code points c satisfying f(c). If f is nil,
|
|
|
|
// !unicode.IsSpace(c) is used; that is, the token will hold non-space
|
2016-03-01 16:21:55 -07:00
|
|
|
// characters. Newlines are treated appropriately for the operation being
|
2015-06-05 15:23:54 -06:00
|
|
|
// performed; see the package documentation for more information.
|
|
|
|
// The returned slice points to shared data that may be overwritten
|
2011-03-09 11:01:47 -07:00
|
|
|
// by the next call to Token, a call to a Scan function using the ScanState
|
|
|
|
// as input, or when the calling Scan method returns.
|
2011-11-01 20:04:37 -06:00
|
|
|
Token(skipSpace bool, f func(rune) bool) (token []byte, err error)
|
2011-03-02 11:54:23 -07:00
|
|
|
// Width returns the value of the width option and whether it has been set.
|
|
|
|
// The unit is Unicode code points.
|
|
|
|
Width() (wid int, ok bool)
|
|
|
|
// Because ReadRune is implemented by the interface, Read should never be
|
|
|
|
// called by the scanning routines and a valid implementation of
|
|
|
|
// ScanState may choose always to return an error from Read.
|
2011-11-01 20:04:37 -06:00
|
|
|
Read(buf []byte) (n int, err error)
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scanner is implemented by any value that has a Scan method, which scans
|
|
|
|
// the input for the representation of a value and stores the result in the
|
2016-03-01 16:21:55 -07:00
|
|
|
// receiver, which must be a pointer to be useful. The Scan method is called
|
2011-02-24 11:14:19 -07:00
|
|
|
// for any argument to Scan, Scanf, or Scanln that implements it.
|
2010-05-26 19:15:09 -06:00
|
|
|
type Scanner interface {
|
2011-11-01 20:04:37 -06:00
|
|
|
Scan(state ScanState, verb rune) error
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scan scans text read from standard input, storing successive
|
2016-03-01 16:21:55 -07:00
|
|
|
// space-separated values into successive arguments. Newlines count
|
|
|
|
// as space. It returns the number of items successfully scanned.
|
2010-06-02 15:58:31 -06:00
|
|
|
// If that is less than the number of arguments, err will report why.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Scan(a ...interface{}) (n int, err error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscan(os.Stdin, a...)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scanln is similar to Scan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Scanln(a ...interface{}) (n int, err error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanln(os.Stdin, a...)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Scanf scans text read from standard input, storing successive
|
|
|
|
// space-separated values into successive arguments as determined by
|
2016-03-01 16:21:55 -07:00
|
|
|
// the format. It returns the number of items successfully scanned.
|
2015-05-05 12:05:35 -06:00
|
|
|
// If that is less than the number of arguments, err will report why.
|
2015-06-05 15:23:54 -06:00
|
|
|
// Newlines in the input must match newlines in the format.
|
2015-08-23 01:45:58 -06:00
|
|
|
// The one exception: the verb %c always scans the next rune in the
|
|
|
|
// input, even if it is a space (or tab etc.) or newline.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Scanf(format string, a ...interface{}) (n int, err error) {
|
2010-09-24 09:55:48 -06:00
|
|
|
return Fscanf(os.Stdin, format, a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
2012-03-06 21:27:11 -07:00
|
|
|
type stringReader string
|
|
|
|
|
|
|
|
func (r *stringReader) Read(b []byte) (n int, err error) {
|
|
|
|
n = copy(b, *r)
|
|
|
|
*r = (*r)[n:]
|
|
|
|
if n == 0 {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Sscan scans the argument string, storing successive space-separated
|
2016-03-01 16:21:55 -07:00
|
|
|
// values into successive arguments. Newlines count as space. It
|
|
|
|
// returns the number of items successfully scanned. If that is less
|
2010-06-02 15:58:31 -06:00
|
|
|
// than the number of arguments, err will report why.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Sscan(str string, a ...interface{}) (n int, err error) {
|
2012-03-06 21:27:11 -07:00
|
|
|
return Fscan((*stringReader)(&str), a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sscanln is similar to Sscan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Sscanln(str string, a ...interface{}) (n int, err error) {
|
2012-03-06 21:27:11 -07:00
|
|
|
return Fscanln((*stringReader)(&str), a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sscanf scans the argument string, storing successive space-separated
|
2016-03-01 16:21:55 -07:00
|
|
|
// values into successive arguments as determined by the format. It
|
2010-06-02 15:58:31 -06:00
|
|
|
// returns the number of items successfully parsed.
|
2015-06-05 15:23:54 -06:00
|
|
|
// Newlines in the input must match newlines in the format.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Sscanf(str string, format string, a ...interface{}) (n int, err error) {
|
2012-03-06 21:27:11 -07:00
|
|
|
return Fscanf((*stringReader)(&str), format, a...)
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fscan scans text read from r, storing successive space-separated
|
2016-03-01 16:21:55 -07:00
|
|
|
// values into successive arguments. Newlines count as space. It
|
|
|
|
// returns the number of items successfully scanned. If that is less
|
2010-06-02 15:58:31 -06:00
|
|
|
// than the number of arguments, err will report why.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Fscan(r io.Reader, a ...interface{}) (n int, err error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, true, false)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScan(a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Fscanln is similar to Fscan, but stops scanning at a newline and
|
|
|
|
// after the final item there must be a newline or EOF.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Fscanln(r io.Reader, a ...interface{}) (n int, err error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, false, true)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScan(a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-02 15:58:31 -06:00
|
|
|
// Fscanf scans text read from r, storing successive space-separated
|
2016-03-01 16:21:55 -07:00
|
|
|
// values into successive arguments as determined by the format. It
|
2010-06-02 15:58:31 -06:00
|
|
|
// returns the number of items successfully parsed.
|
2015-06-05 15:23:54 -06:00
|
|
|
// Newlines in the input must match newlines in the format.
|
2011-11-01 20:04:37 -06:00
|
|
|
func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error) {
|
2011-03-03 11:43:29 -07:00
|
|
|
s, old := newScanState(r, false, false)
|
2010-06-01 16:21:21 -06:00
|
|
|
n, err = s.doScanf(format, a)
|
2011-03-03 11:43:29 -07:00
|
|
|
s.free(old)
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanError represents an error generated by the scanning software.
|
|
|
|
// It's used as a unique signature to identify such errors when recovering.
|
|
|
|
type scanError struct {
|
2011-11-01 20:04:37 -06:00
|
|
|
err error
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2011-03-09 17:53:32 -07:00
|
|
|
const eof = -1
|
2010-06-24 16:24:25 -06:00
|
|
|
|
2010-05-26 19:15:09 -06:00
|
|
|
// ss is the internal implementation of ScanState.
|
2010-05-25 22:02:35 -06:00
|
|
|
type ss struct {
|
2016-02-24 15:22:40 -07:00
|
|
|
rs io.RuneScanner // where to read input
|
|
|
|
buf buffer // token accumulator
|
|
|
|
count int // runes consumed so far.
|
|
|
|
atEOF bool // already read EOF
|
2011-03-03 11:43:29 -07:00
|
|
|
ssave
|
|
|
|
}
|
|
|
|
|
|
|
|
// ssave holds the parts of ss that need to be
|
|
|
|
// saved and restored on recursive scans.
|
|
|
|
type ssave struct {
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
validSave bool // is or was a part of an actual ss.
|
|
|
|
nlIsEnd bool // whether newline terminates scan
|
|
|
|
nlIsSpace bool // whether newline counts as white space
|
|
|
|
argLimit int // max value of ss.count for this arg; argLimit <= limit
|
|
|
|
limit int // max value of ss.count.
|
|
|
|
maxWid int // width of this arg.
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2011-03-02 11:54:23 -07:00
|
|
|
// The Read method is only in ScanState so that ScanState
|
|
|
|
// satisfies io.Reader. It will never be called when used as
|
|
|
|
// intended, so there is no need to make it actually work.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) Read(buf []byte) (n int, err error) {
|
|
|
|
return 0, errors.New("ScanState's Read should not be called. Use ReadRune")
|
2011-03-02 11:54:23 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) ReadRune() (r rune, size int, err error) {
|
2016-02-24 15:22:40 -07:00
|
|
|
if s.atEOF || s.count >= s.argLimit {
|
2011-11-01 20:04:37 -06:00
|
|
|
err = io.EOF
|
2011-02-24 11:14:19 -07:00
|
|
|
return
|
|
|
|
}
|
2011-03-02 11:54:23 -07:00
|
|
|
|
2016-02-24 15:22:40 -07:00
|
|
|
r, size, err = s.rs.ReadRune()
|
2010-09-13 19:06:17 -06:00
|
|
|
if err == nil {
|
2011-03-03 11:43:29 -07:00
|
|
|
s.count++
|
2016-02-24 15:22:40 -07:00
|
|
|
if s.nlIsEnd && r == '\n' {
|
|
|
|
s.atEOF = true
|
|
|
|
}
|
2011-11-01 20:04:37 -06:00
|
|
|
} else if err == io.EOF {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.atEOF = true
|
2010-09-13 19:06:17 -06:00
|
|
|
}
|
2010-05-26 19:15:09 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
func (s *ss) Width() (wid int, ok bool) {
|
2011-03-02 11:54:23 -07:00
|
|
|
if s.maxWid == hugeWid {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return s.maxWid, true
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// The public method returns an error; this private one panics.
|
|
|
|
// If getRune reaches EOF, the return value is EOF (-1).
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) getRune() (r rune) {
|
|
|
|
r, _, err := s.ReadRune()
|
2011-03-02 11:54:23 -07:00
|
|
|
if err != nil {
|
2011-11-01 20:04:37 -06:00
|
|
|
if err == io.EOF {
|
2011-03-09 17:53:32 -07:00
|
|
|
return eof
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-03 15:01:30 -06:00
|
|
|
// mustReadRune turns io.EOF into a panic(io.ErrUnexpectedEOF).
|
2010-06-01 16:21:21 -06:00
|
|
|
// It is called in cases such as string scanning where an EOF is a
|
|
|
|
// syntax error.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) mustReadRune() (r rune) {
|
|
|
|
r = s.getRune()
|
|
|
|
if r == eof {
|
2010-06-24 16:24:25 -06:00
|
|
|
s.error(io.ErrUnexpectedEOF)
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) UnreadRune() error {
|
2016-02-24 15:22:40 -07:00
|
|
|
s.rs.UnreadRune()
|
|
|
|
s.atEOF = false
|
2011-03-03 11:43:29 -07:00
|
|
|
s.count--
|
2011-03-02 11:54:23 -07:00
|
|
|
return nil
|
2010-05-26 19:15:09 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) error(err error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
panic(scanError{err})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ss) errorString(err string) {
|
2011-11-01 20:04:37 -06:00
|
|
|
panic(scanError{errors.New(err)})
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) Token(skipSpace bool, f func(rune) bool) (tok []byte, err error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
if se, ok := e.(scanError); ok {
|
|
|
|
err = se.err
|
|
|
|
} else {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2011-03-09 11:01:47 -07:00
|
|
|
if f == nil {
|
|
|
|
f = notSpace
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
s.buf = s.buf[:0]
|
2011-03-09 11:01:47 -07:00
|
|
|
tok = s.token(skipSpace, f)
|
2010-05-26 19:15:09 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-03-06 21:27:11 -07:00
|
|
|
// space is a copy of the unicode.White_Space ranges,
|
|
|
|
// to avoid depending on package unicode.
|
|
|
|
var space = [][2]uint16{
|
|
|
|
{0x0009, 0x000d},
|
|
|
|
{0x0020, 0x0020},
|
|
|
|
{0x0085, 0x0085},
|
|
|
|
{0x00a0, 0x00a0},
|
|
|
|
{0x1680, 0x1680},
|
|
|
|
{0x2000, 0x200a},
|
|
|
|
{0x2028, 0x2029},
|
|
|
|
{0x202f, 0x202f},
|
|
|
|
{0x205f, 0x205f},
|
|
|
|
{0x3000, 0x3000},
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSpace(r rune) bool {
|
|
|
|
if r >= 1<<16 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rx := uint16(r)
|
|
|
|
for _, rng := range space {
|
|
|
|
if rx < rng[0] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if rx <= rng[1] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2011-03-09 11:01:47 -07:00
|
|
|
// notSpace is the default scanning function used in Token.
|
2011-10-25 23:21:33 -06:00
|
|
|
func notSpace(r rune) bool {
|
2012-03-06 21:27:11 -07:00
|
|
|
return !isSpace(r)
|
2011-03-09 11:01:47 -07:00
|
|
|
}
|
|
|
|
|
2010-05-25 22:02:35 -06:00
|
|
|
// readRune is a structure to enable reading UTF-8 encoded code points
|
2016-02-24 15:22:40 -07:00
|
|
|
// from an io.Reader. It is used if the Reader given to the scanner does
|
|
|
|
// not already implement io.RuneScanner.
|
2010-05-25 22:02:35 -06:00
|
|
|
type readRune struct {
|
2016-02-24 15:22:40 -07:00
|
|
|
reader io.Reader
|
|
|
|
buf [utf8.UTFMax]byte // used only inside ReadRune
|
|
|
|
pending int // number of bytes in pendBuf; only >0 for bad UTF-8
|
|
|
|
pendBuf [utf8.UTFMax]byte // bytes left over
|
|
|
|
peekRune rune // if >=0 next rune; when <0 is ^(previous Rune)
|
2010-06-15 18:41:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// readByte returns the next byte from the input, which may be
|
|
|
|
// left over from a previous read if the UTF-8 was ill-formed.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (r *readRune) readByte() (b byte, err error) {
|
2010-06-15 18:41:11 -06:00
|
|
|
if r.pending > 0 {
|
|
|
|
b = r.pendBuf[0]
|
|
|
|
copy(r.pendBuf[0:], r.pendBuf[1:])
|
|
|
|
r.pending--
|
|
|
|
return
|
|
|
|
}
|
2016-07-16 03:42:52 -06:00
|
|
|
n, err := io.ReadFull(r.reader, r.pendBuf[:1])
|
|
|
|
if n != 1 {
|
|
|
|
return 0, err
|
2012-12-17 10:26:48 -07:00
|
|
|
}
|
2010-06-15 18:41:11 -06:00
|
|
|
return r.pendBuf[0], err
|
|
|
|
}
|
|
|
|
|
2010-05-25 22:02:35 -06:00
|
|
|
// ReadRune returns the next UTF-8 encoded code point from the
|
|
|
|
// io.Reader inside r.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (r *readRune) ReadRune() (rr rune, size int, err error) {
|
2016-02-24 15:22:40 -07:00
|
|
|
if r.peekRune >= 0 {
|
|
|
|
rr = r.peekRune
|
|
|
|
r.peekRune = ^r.peekRune
|
|
|
|
size = utf8.RuneLen(rr)
|
|
|
|
return
|
|
|
|
}
|
2010-06-15 18:41:11 -06:00
|
|
|
r.buf[0], err = r.readByte()
|
2010-05-25 22:02:35 -06:00
|
|
|
if err != nil {
|
2016-02-24 15:22:40 -07:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
if r.buf[0] < utf8.RuneSelf { // fast check for common ASCII case
|
2011-10-25 23:21:33 -06:00
|
|
|
rr = rune(r.buf[0])
|
2014-08-15 12:41:12 -06:00
|
|
|
size = 1 // Known to be 1.
|
2016-02-24 15:22:40 -07:00
|
|
|
// Flip the bits of the rune so it's available to UnreadRune.
|
|
|
|
r.peekRune = ^rr
|
2010-05-25 22:02:35 -06:00
|
|
|
return
|
|
|
|
}
|
2010-06-15 18:41:11 -06:00
|
|
|
var n int
|
2016-02-24 15:22:40 -07:00
|
|
|
for n = 1; !utf8.FullRune(r.buf[:n]); n++ {
|
2010-06-15 18:41:11 -06:00
|
|
|
r.buf[n], err = r.readByte()
|
2010-05-25 22:02:35 -06:00
|
|
|
if err != nil {
|
2011-11-01 20:04:37 -06:00
|
|
|
if err == io.EOF {
|
2010-06-15 18:41:11 -06:00
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-02-24 15:22:40 -07:00
|
|
|
rr, size = utf8.DecodeRune(r.buf[:n])
|
|
|
|
if size < n { // an error, save the bytes for the next read
|
|
|
|
copy(r.pendBuf[r.pending:], r.buf[size:n])
|
|
|
|
r.pending += n - size
|
2010-06-15 18:41:11 -06:00
|
|
|
}
|
2016-02-24 15:22:40 -07:00
|
|
|
// Flip the bits of the rune so it's available to UnreadRune.
|
|
|
|
r.peekRune = ^rr
|
2010-06-15 18:41:11 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2016-02-24 15:22:40 -07:00
|
|
|
func (r *readRune) UnreadRune() error {
|
|
|
|
if r.peekRune >= 0 {
|
|
|
|
return errors.New("fmt: scanning called UnreadRune with no rune available")
|
|
|
|
}
|
|
|
|
// Reverse bit flip of previously read rune to obtain valid >=0 state.
|
|
|
|
r.peekRune = ^r.peekRune
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-18 12:09:07 -07:00
|
|
|
var ssFree = sync.Pool{
|
|
|
|
New: func() interface{} { return new(ss) },
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
|
2013-01-22 15:12:45 -07:00
|
|
|
// newScanState allocates a new ss struct or grab a cached one.
|
2011-03-03 11:43:29 -07:00
|
|
|
func newScanState(r io.Reader, nlIsSpace, nlIsEnd bool) (s *ss, old ssave) {
|
2013-12-18 12:09:07 -07:00
|
|
|
s = ssFree.Get().(*ss)
|
2016-02-24 15:22:40 -07:00
|
|
|
if rs, ok := r.(io.RuneScanner); ok {
|
|
|
|
s.rs = rs
|
2010-05-25 22:02:35 -06:00
|
|
|
} else {
|
2016-02-24 15:22:40 -07:00
|
|
|
s.rs = &readRune{reader: r, peekRune: -1}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
s.nlIsSpace = nlIsSpace
|
2011-02-24 11:14:19 -07:00
|
|
|
s.nlIsEnd = nlIsEnd
|
2010-06-24 16:24:25 -06:00
|
|
|
s.atEOF = false
|
2011-03-03 11:43:29 -07:00
|
|
|
s.limit = hugeWid
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
s.argLimit = hugeWid
|
2011-03-02 11:54:23 -07:00
|
|
|
s.maxWid = hugeWid
|
2011-03-03 11:43:29 -07:00
|
|
|
s.validSave = true
|
2012-01-31 16:38:33 -07:00
|
|
|
s.count = 0
|
2011-03-03 11:43:29 -07:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2013-01-22 15:12:45 -07:00
|
|
|
// free saves used ss structs in ssFree; avoid an allocation per invocation.
|
2011-03-03 11:43:29 -07:00
|
|
|
func (s *ss) free(old ssave) {
|
|
|
|
// If it was used recursively, just restore the old state.
|
|
|
|
if old.validSave {
|
|
|
|
s.ssave = old
|
|
|
|
return
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// Don't hold on to ss structs with large buffers.
|
2012-03-06 21:27:11 -07:00
|
|
|
if cap(s.buf) > 1024 {
|
2010-05-25 22:02:35 -06:00
|
|
|
return
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
s.buf = s.buf[:0]
|
2016-02-24 15:22:40 -07:00
|
|
|
s.rs = nil
|
2013-12-18 12:09:07 -07:00
|
|
|
ssFree.Put(s)
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2017-08-02 16:54:14 -06:00
|
|
|
// SkipSpace provides Scan methods the ability to skip space and newline
|
|
|
|
// characters in keeping with the current scanning mode set by format strings
|
|
|
|
// and Scan/Scanln.
|
|
|
|
func (s *ss) SkipSpace() {
|
2010-05-25 22:02:35 -06:00
|
|
|
for {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.getRune()
|
|
|
|
if r == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2013-07-30 23:00:08 -06:00
|
|
|
if r == '\r' && s.peek("\n") {
|
|
|
|
continue
|
|
|
|
}
|
2011-10-25 23:21:33 -06:00
|
|
|
if r == '\n' {
|
2010-05-25 22:02:35 -06:00
|
|
|
if s.nlIsSpace {
|
|
|
|
continue
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("unexpected newline")
|
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
if !isSpace(r) {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2016-03-01 16:21:55 -07:00
|
|
|
// token returns the next space-delimited string from the input. It
|
|
|
|
// skips white space. For Scanln, it stops at newlines. For Scan,
|
2010-06-03 17:06:41 -06:00
|
|
|
// newlines are treated as spaces.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) token(skipSpace bool, f func(rune) bool) []byte {
|
2011-03-09 11:01:47 -07:00
|
|
|
if skipSpace {
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-03-09 11:01:47 -07:00
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// read until white space or newline
|
2011-03-02 11:54:23 -07:00
|
|
|
for {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.getRune()
|
|
|
|
if r == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
break
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2011-10-25 23:21:33 -06:00
|
|
|
if !f(r) {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeRune(r)
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return s.buf
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
var complexError = errors.New("syntax error scanning complex number")
|
|
|
|
var boolError = errors.New("syntax error scanning boolean")
|
2010-06-03 17:06:41 -06:00
|
|
|
|
2012-03-06 21:27:11 -07:00
|
|
|
func indexRune(s string, r rune) int {
|
|
|
|
for i, c := range s {
|
|
|
|
if c == r {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2010-12-06 12:23:37 -07:00
|
|
|
// consume reads the next rune in the input and reports whether it is in the ok string.
|
|
|
|
// If accept is true, it puts the character into the input token.
|
|
|
|
func (s *ss) consume(ok string, accept bool) bool {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.getRune()
|
|
|
|
if r == eof {
|
2010-06-03 17:06:41 -06:00
|
|
|
return false
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
if indexRune(ok, r) >= 0 {
|
2011-02-04 14:14:38 -07:00
|
|
|
if accept {
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeRune(r)
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
return true
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
2011-10-25 23:21:33 -06:00
|
|
|
if r != eof && accept {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
|
2011-02-04 14:14:38 -07:00
|
|
|
// peek reports whether the next character is in the ok string, without consuming it.
|
|
|
|
func (s *ss) peek(ok string) bool {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.getRune()
|
|
|
|
if r != eof {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2011-02-04 14:14:38 -07:00
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return indexRune(ok, r) >= 0
|
2011-02-04 14:14:38 -07:00
|
|
|
}
|
|
|
|
|
2011-06-01 18:51:31 -06:00
|
|
|
func (s *ss) notEOF() {
|
|
|
|
// Guarantee there is data to be read.
|
2011-10-25 23:21:33 -06:00
|
|
|
if r := s.getRune(); r == eof {
|
2011-11-01 20:04:37 -06:00
|
|
|
panic(io.EOF)
|
2011-06-01 18:51:31 -06:00
|
|
|
}
|
|
|
|
s.UnreadRune()
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:21:55 -07:00
|
|
|
// accept checks the next rune in the input. If it's a byte (sic) in the string, it puts it in the
|
2010-12-06 12:23:37 -07:00
|
|
|
// buffer and returns true. Otherwise it return false.
|
|
|
|
func (s *ss) accept(ok string) bool {
|
|
|
|
return s.consume(ok, true)
|
|
|
|
}
|
|
|
|
|
2010-05-31 15:53:15 -06:00
|
|
|
// okVerb verifies that the verb is present in the list, setting s.err appropriately if not.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) okVerb(verb rune, okVerbs, typ string) bool {
|
2010-05-31 15:53:15 -06:00
|
|
|
for _, v := range okVerbs {
|
|
|
|
if v == verb {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 14:17:32 -07:00
|
|
|
s.errorString("bad verb '%" + string(verb) + "' for " + typ)
|
2010-05-31 15:53:15 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanBool returns the value of the boolean represented by the next token.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) scanBool(verb rune) bool {
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-05-31 15:53:15 -06:00
|
|
|
if !s.okVerb(verb, "tv", "boolean") {
|
2010-05-25 22:02:35 -06:00
|
|
|
return false
|
|
|
|
}
|
2016-03-01 16:21:55 -07:00
|
|
|
// Syntax-checking a boolean is annoying. We're not fastidious about case.
|
2011-06-01 18:51:31 -06:00
|
|
|
switch s.getRune() {
|
2010-06-03 17:06:41 -06:00
|
|
|
case '0':
|
|
|
|
return false
|
|
|
|
case '1':
|
|
|
|
return true
|
|
|
|
case 't', 'T':
|
|
|
|
if s.accept("rR") && (!s.accept("uU") || !s.accept("eE")) {
|
|
|
|
s.error(boolError)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case 'f', 'F':
|
2012-02-08 20:12:55 -07:00
|
|
|
if s.accept("aA") && (!s.accept("lL") || !s.accept("sS") || !s.accept("eE")) {
|
2010-06-03 17:06:41 -06:00
|
|
|
s.error(boolError)
|
|
|
|
}
|
|
|
|
return false
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
return false
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// Numerical elements
|
|
|
|
const (
|
|
|
|
binaryDigits = "01"
|
|
|
|
octalDigits = "01234567"
|
|
|
|
decimalDigits = "0123456789"
|
|
|
|
hexadecimalDigits = "0123456789aAbBcCdDeEfF"
|
|
|
|
sign = "+-"
|
|
|
|
period = "."
|
2019-01-29 21:06:06 -07:00
|
|
|
exponent = "eEpP"
|
2010-06-03 17:06:41 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// getBase returns the numeric base represented by the verb and its digit string.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) getBase(verb rune) (base int, digits string) {
|
2010-12-06 12:23:37 -07:00
|
|
|
s.okVerb(verb, "bdoUxXv", "integer") // sets s.err
|
2010-06-03 17:06:41 -06:00
|
|
|
base = 10
|
|
|
|
digits = decimalDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
switch verb {
|
|
|
|
case 'b':
|
|
|
|
base = 2
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = binaryDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
case 'o':
|
|
|
|
base = 8
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = octalDigits
|
2010-12-06 12:23:37 -07:00
|
|
|
case 'x', 'X', 'U':
|
2010-05-31 15:53:15 -06:00
|
|
|
base = 16
|
2010-06-03 17:06:41 -06:00
|
|
|
digits = hexadecimalDigits
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// scanNumber returns the numerical string with specified digits starting here.
|
2011-02-04 14:14:38 -07:00
|
|
|
func (s *ss) scanNumber(digits string, haveDigits bool) string {
|
2011-07-17 18:05:35 -06:00
|
|
|
if !haveDigits {
|
|
|
|
s.notEOF()
|
|
|
|
if !s.accept(digits) {
|
|
|
|
s.errorString("expected integer")
|
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
for s.accept(digits) {
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 18:03:22 -06:00
|
|
|
// scanRune returns the next rune value in the input.
|
2010-06-21 14:19:29 -06:00
|
|
|
func (s *ss) scanRune(bitSize int) int64 {
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2020-02-24 19:35:17 -07:00
|
|
|
r := s.getRune()
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
2020-02-24 19:35:17 -07:00
|
|
|
x := (int64(r) << (64 - n)) >> (64 - n)
|
|
|
|
if x != int64(r) {
|
2011-10-25 23:21:33 -06:00
|
|
|
s.errorString("overflow on character value " + string(r))
|
2010-06-03 18:03:22 -06:00
|
|
|
}
|
2020-02-24 19:35:17 -07:00
|
|
|
return int64(r)
|
2010-06-03 18:03:22 -06:00
|
|
|
}
|
|
|
|
|
2019-08-23 10:05:56 -06:00
|
|
|
// scanBasePrefix reports whether the integer begins with a base prefix
|
2011-02-04 14:14:38 -07:00
|
|
|
// and returns the base, digit string, and whether a zero was found.
|
|
|
|
// It is called only if the verb is %v.
|
2019-03-06 20:24:54 -07:00
|
|
|
func (s *ss) scanBasePrefix() (base int, digits string, zeroFound bool) {
|
2011-02-04 14:14:38 -07:00
|
|
|
if !s.peek("0") {
|
2019-01-29 21:06:06 -07:00
|
|
|
return 0, decimalDigits + "_", false
|
2011-02-04 14:14:38 -07:00
|
|
|
}
|
|
|
|
s.accept("0")
|
2019-01-29 21:06:06 -07:00
|
|
|
// Special cases for 0, 0b, 0o, 0x.
|
2019-03-06 04:16:14 -07:00
|
|
|
switch {
|
|
|
|
case s.peek("bB"):
|
2019-01-29 21:06:06 -07:00
|
|
|
s.consume("bB", true)
|
2019-03-06 04:16:14 -07:00
|
|
|
return 0, binaryDigits + "_", true
|
|
|
|
case s.peek("oO"):
|
2019-01-29 21:06:06 -07:00
|
|
|
s.consume("oO", true)
|
2019-03-06 04:16:14 -07:00
|
|
|
return 0, octalDigits + "_", true
|
|
|
|
case s.peek("xX"):
|
2019-01-29 21:06:06 -07:00
|
|
|
s.consume("xX", true)
|
2019-03-06 04:16:14 -07:00
|
|
|
return 0, hexadecimalDigits + "_", true
|
|
|
|
default:
|
|
|
|
return 0, octalDigits + "_", true
|
2011-02-04 14:14:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanInt returns the value of the integer represented by the next
|
2016-03-01 16:21:55 -07:00
|
|
|
// token, checking for overflow. Any error is stored in s.err.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) scanInt(verb rune, bitSize int) int64 {
|
2010-06-03 18:03:22 -06:00
|
|
|
if verb == 'c' {
|
|
|
|
return s.scanRune(bitSize)
|
|
|
|
}
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2011-02-04 14:14:38 -07:00
|
|
|
base, digits := s.getBase(verb)
|
|
|
|
haveDigits := false
|
2010-12-06 12:23:37 -07:00
|
|
|
if verb == 'U' {
|
|
|
|
if !s.consume("U", false) || !s.consume("+", false) {
|
|
|
|
s.errorString("bad unicode format ")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.accept(sign) // If there's a sign, it will be left in the token buffer.
|
2011-02-04 14:14:38 -07:00
|
|
|
if verb == 'v' {
|
|
|
|
base, digits, haveDigits = s.scanBasePrefix()
|
|
|
|
}
|
2010-12-06 12:23:37 -07:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
tok := s.scanNumber(digits, haveDigits)
|
2011-12-05 13:48:46 -07:00
|
|
|
i, err := strconv.ParseInt(tok, base, 64)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
|
|
|
x := (i << (64 - n)) >> (64 - n)
|
2010-05-31 11:56:58 -06:00
|
|
|
if x != i {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("integer overflow on token " + tok)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// scanUint returns the value of the unsigned integer represented
|
2016-03-01 16:21:55 -07:00
|
|
|
// by the next token, checking for overflow. Any error is stored in s.err.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) scanUint(verb rune, bitSize int) uint64 {
|
2010-06-03 18:03:22 -06:00
|
|
|
if verb == 'c' {
|
|
|
|
return uint64(s.scanRune(bitSize))
|
|
|
|
}
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2011-02-04 14:14:38 -07:00
|
|
|
base, digits := s.getBase(verb)
|
|
|
|
haveDigits := false
|
2010-12-06 12:23:37 -07:00
|
|
|
if verb == 'U' {
|
|
|
|
if !s.consume("U", false) || !s.consume("+", false) {
|
|
|
|
s.errorString("bad unicode format ")
|
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
} else if verb == 'v' {
|
|
|
|
base, digits, haveDigits = s.scanBasePrefix()
|
2010-12-06 12:23:37 -07:00
|
|
|
}
|
2011-02-04 14:14:38 -07:00
|
|
|
tok := s.scanNumber(digits, haveDigits)
|
2011-12-05 13:48:46 -07:00
|
|
|
i, err := strconv.ParseUint(tok, base, 64)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
2010-05-31 15:53:15 -06:00
|
|
|
}
|
2010-06-21 14:19:29 -06:00
|
|
|
n := uint(bitSize)
|
|
|
|
x := (i << (64 - n)) >> (64 - n)
|
2010-05-31 11:56:58 -06:00
|
|
|
if x != i {
|
2010-06-01 16:21:21 -06:00
|
|
|
s.errorString("unsigned integer overflow on token " + tok)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// floatToken returns the floating-point number starting here, no longer than swid
|
|
|
|
// if the width is specified. It's not rigorous about syntax because it doesn't check that
|
|
|
|
// we have at least some digits, but Atof will do that.
|
|
|
|
func (s *ss) floatToken() string {
|
2012-03-06 21:27:11 -07:00
|
|
|
s.buf = s.buf[:0]
|
2010-11-30 13:59:52 -07:00
|
|
|
// NaN?
|
|
|
|
if s.accept("nN") && s.accept("aA") && s.accept("nN") {
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-11-30 13:59:52 -07:00
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// leading sign?
|
2010-06-03 17:06:41 -06:00
|
|
|
s.accept(sign)
|
2010-11-30 13:59:52 -07:00
|
|
|
// Inf?
|
|
|
|
if s.accept("iI") && s.accept("nN") && s.accept("fF") {
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-11-30 13:59:52 -07:00
|
|
|
}
|
2019-01-29 21:06:06 -07:00
|
|
|
digits := decimalDigits + "_"
|
|
|
|
exp := exponent
|
|
|
|
if s.accept("0") && s.accept("xX") {
|
|
|
|
digits = hexadecimalDigits + "_"
|
|
|
|
exp = "pP"
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
// digits?
|
2019-01-29 21:06:06 -07:00
|
|
|
for s.accept(digits) {
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
// decimal point?
|
|
|
|
if s.accept(period) {
|
|
|
|
// fraction?
|
2019-01-29 21:06:06 -07:00
|
|
|
for s.accept(digits) {
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
// exponent?
|
2019-01-29 21:06:06 -07:00
|
|
|
if s.accept(exp) {
|
2010-05-25 22:02:35 -06:00
|
|
|
// leading sign?
|
2010-06-03 17:06:41 -06:00
|
|
|
s.accept(sign)
|
2010-05-25 22:02:35 -06:00
|
|
|
// digits?
|
2019-01-29 21:06:06 -07:00
|
|
|
for s.accept(decimalDigits + "_") {
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-06-03 17:06:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// complexTokens returns the real and imaginary parts of the complex number starting here.
|
|
|
|
// The number might be parenthesized and has the format (N+Ni) where N is a floating-point
|
|
|
|
// number and there are no spaces within.
|
|
|
|
func (s *ss) complexTokens() (real, imag string) {
|
|
|
|
// TODO: accept N and Ni independently?
|
|
|
|
parens := s.accept("(")
|
|
|
|
real = s.floatToken()
|
2012-03-06 21:27:11 -07:00
|
|
|
s.buf = s.buf[:0]
|
2010-06-03 17:06:41 -06:00
|
|
|
// Must now have a sign.
|
|
|
|
if !s.accept("+-") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
// Sign is now in buffer
|
2012-03-06 21:27:11 -07:00
|
|
|
imagSign := string(s.buf)
|
2010-06-03 17:06:41 -06:00
|
|
|
imag = s.floatToken()
|
|
|
|
if !s.accept("i") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
if parens && !s.accept(")") {
|
|
|
|
s.error(complexError)
|
|
|
|
}
|
|
|
|
return real, imagSign + imag
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2019-01-29 21:06:06 -07:00
|
|
|
func hasX(s string) bool {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] == 'x' || s[i] == 'X' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2010-06-20 13:16:25 -06:00
|
|
|
// convertFloat converts the string to a float64value.
|
|
|
|
func (s *ss) convertFloat(str string, n int) float64 {
|
2019-01-29 21:06:06 -07:00
|
|
|
// strconv.ParseFloat will handle "+0x1.fp+2",
|
|
|
|
// but we have to implement our non-standard
|
|
|
|
// decimal+binary exponent mix (1.2p4) ourselves.
|
|
|
|
if p := indexRune(str, 'p'); p >= 0 && !hasX(str) {
|
2011-02-02 13:38:48 -07:00
|
|
|
// Atof doesn't handle power-of-2 exponents,
|
|
|
|
// but they're easy to evaluate.
|
2011-12-05 13:48:46 -07:00
|
|
|
f, err := strconv.ParseFloat(str[:p], n)
|
2011-02-02 13:38:48 -07:00
|
|
|
if err != nil {
|
|
|
|
// Put full string into error.
|
|
|
|
if e, ok := err.(*strconv.NumError); ok {
|
|
|
|
e.Num = str
|
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
2013-06-20 17:14:40 -06:00
|
|
|
m, err := strconv.Atoi(str[p+1:])
|
2011-02-02 13:38:48 -07:00
|
|
|
if err != nil {
|
|
|
|
// Put full string into error.
|
|
|
|
if e, ok := err.(*strconv.NumError); ok {
|
|
|
|
e.Num = str
|
|
|
|
}
|
|
|
|
s.error(err)
|
|
|
|
}
|
2013-06-20 17:14:40 -06:00
|
|
|
return math.Ldexp(f, m)
|
2011-02-02 13:38:48 -07:00
|
|
|
}
|
2011-12-05 13:48:46 -07:00
|
|
|
f, err := strconv.ParseFloat(str, n)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
// convertComplex converts the next token to a complex128 value.
|
2010-05-25 22:02:35 -06:00
|
|
|
// The atof argument is a type-specific reader for the underlying type.
|
|
|
|
// If we're reading complex64, atof will parse float32s and convert them
|
|
|
|
// to float64's to avoid reproducing this code for each complex type.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) scanComplex(verb rune, n int) complex128 {
|
2010-06-01 16:21:21 -06:00
|
|
|
if !s.okVerb(verb, floatVerbs, "complex") {
|
2010-05-25 22:02:35 -06:00
|
|
|
return 0
|
|
|
|
}
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-06-03 17:06:41 -06:00
|
|
|
sreal, simag := s.complexTokens()
|
2010-06-20 13:16:25 -06:00
|
|
|
real := s.convertFloat(sreal, n/2)
|
|
|
|
imag := s.convertFloat(simag, n/2)
|
2011-01-19 21:09:00 -07:00
|
|
|
return complex(real, imag)
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// convertString returns the string represented by the next input characters.
|
|
|
|
// The format of the input is determined by the verb.
|
2011-10-25 23:21:33 -06:00
|
|
|
func (s *ss) convertString(verb rune) (str string) {
|
2015-10-14 13:18:59 -06:00
|
|
|
if !s.okVerb(verb, "svqxX", "string") {
|
2010-06-01 16:21:21 -06:00
|
|
|
return ""
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-06-01 16:21:21 -06:00
|
|
|
switch verb {
|
|
|
|
case 'q':
|
2010-08-06 18:02:04 -06:00
|
|
|
str = s.quotedString()
|
2015-10-14 13:18:59 -06:00
|
|
|
case 'x', 'X':
|
2010-08-06 18:02:04 -06:00
|
|
|
str = s.hexString()
|
|
|
|
default:
|
2011-03-09 11:01:47 -07:00
|
|
|
str = string(s.token(true, notSpace)) // %s and %v just return the next word
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-08-06 18:02:04 -06:00
|
|
|
return
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// quotedString returns the double- or back-quoted string represented by the next input characters.
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) quotedString() string {
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
|
|
|
quote := s.getRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
switch quote {
|
|
|
|
case '`':
|
|
|
|
// Back-quoted: Anything goes until EOF or back quote.
|
|
|
|
for {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.mustReadRune()
|
|
|
|
if r == quote {
|
2010-06-01 16:21:21 -06:00
|
|
|
break
|
|
|
|
}
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeRune(r)
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-06-01 16:21:21 -06:00
|
|
|
case '"':
|
|
|
|
// Double-quoted: Include the quotes and let strconv.Unquote do the backslash escapes.
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeByte('"')
|
2010-06-01 16:21:21 -06:00
|
|
|
for {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.mustReadRune()
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeRune(r)
|
2011-10-25 23:21:33 -06:00
|
|
|
if r == '\\' {
|
2010-06-01 16:21:21 -06:00
|
|
|
// In a legal backslash escape, no matter how long, only the character
|
|
|
|
// immediately after the escape can itself be a backslash or quote.
|
|
|
|
// Thus we only need to protect the first character after the backslash.
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeRune(s.mustReadRune())
|
2011-10-25 23:21:33 -06:00
|
|
|
} else if r == '"' {
|
2010-06-01 16:21:21 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
result, err := strconv.Unquote(string(s.buf))
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
|
|
|
s.error(err)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
default:
|
|
|
|
s.errorString("expected quoted string")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-12-14 17:52:06 -07:00
|
|
|
// hexDigit returns the value of the hexadecimal digit.
|
|
|
|
func hexDigit(d rune) (int, bool) {
|
2011-10-25 23:21:33 -06:00
|
|
|
digit := int(d)
|
2010-06-01 16:21:21 -06:00
|
|
|
switch digit {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
2014-12-14 17:52:06 -07:00
|
|
|
return digit - '0', true
|
2010-06-01 16:21:21 -06:00
|
|
|
case 'a', 'b', 'c', 'd', 'e', 'f':
|
2014-12-14 17:52:06 -07:00
|
|
|
return 10 + digit - 'a', true
|
2010-06-01 16:21:21 -06:00
|
|
|
case 'A', 'B', 'C', 'D', 'E', 'F':
|
2014-12-14 17:52:06 -07:00
|
|
|
return 10 + digit - 'A', true
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
2014-12-14 17:52:06 -07:00
|
|
|
return -1, false
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// hexByte returns the next hex-encoded (two-character) byte from the input.
|
2014-12-14 17:52:06 -07:00
|
|
|
// It returns ok==false if the next bytes in the input do not encode a hex byte.
|
|
|
|
// If the first byte is hex and the second is not, processing stops.
|
2010-06-01 16:21:21 -06:00
|
|
|
func (s *ss) hexByte() (b byte, ok bool) {
|
|
|
|
rune1 := s.getRune()
|
2011-03-09 17:53:32 -07:00
|
|
|
if rune1 == eof {
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
2014-12-14 17:52:06 -07:00
|
|
|
value1, ok := hexDigit(rune1)
|
|
|
|
if !ok {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
|
|
|
}
|
2014-12-14 17:52:06 -07:00
|
|
|
value2, ok := hexDigit(s.mustReadRune())
|
|
|
|
if !ok {
|
|
|
|
s.errorString("illegal hex digit")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return byte(value1<<4 | value2), true
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// hexString returns the space-delimited hexpair-encoded string.
|
|
|
|
func (s *ss) hexString() string {
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-06-01 16:21:21 -06:00
|
|
|
for {
|
|
|
|
b, ok := s.hexByte()
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
2019-05-08 16:47:32 -06:00
|
|
|
s.buf.writeByte(b)
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
if len(s.buf) == 0 {
|
2013-07-30 22:09:19 -06:00
|
|
|
s.errorString("no hex data for %x string")
|
2010-06-01 16:21:21 -06:00
|
|
|
return ""
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
return string(s.buf)
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
|
2016-03-10 08:11:35 -07:00
|
|
|
const (
|
|
|
|
floatVerbs = "beEfFgGv"
|
|
|
|
|
|
|
|
hugeWid = 1 << 30
|
2010-05-31 15:53:15 -06:00
|
|
|
|
2016-03-10 08:11:35 -07:00
|
|
|
intBits = 32 << (^uint(0) >> 63)
|
|
|
|
uintptrBits = 32 << (^uintptr(0) >> 63)
|
|
|
|
)
|
2011-03-02 11:54:23 -07:00
|
|
|
|
2019-10-24 01:22:43 -06:00
|
|
|
// scanPercent scans a literal percent character.
|
|
|
|
func (s *ss) scanPercent() {
|
|
|
|
s.SkipSpace()
|
|
|
|
s.notEOF()
|
|
|
|
if !s.accept("%") {
|
|
|
|
s.errorString("missing literal %")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// scanOne scans a single value, deriving the scanner from the type of the argument.
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
func (s *ss) scanOne(verb rune, arg interface{}) {
|
2012-03-06 21:27:11 -07:00
|
|
|
s.buf = s.buf[:0]
|
2011-11-01 20:04:37 -06:00
|
|
|
var err error
|
2010-05-31 15:53:15 -06:00
|
|
|
// If the parameter has its own Scan method, use that.
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
if v, ok := arg.(Scanner); ok {
|
2010-06-03 17:06:41 -06:00
|
|
|
err = v.Scan(s, verb)
|
2010-06-01 16:21:21 -06:00
|
|
|
if err != nil {
|
2011-11-01 20:04:37 -06:00
|
|
|
if err == io.EOF {
|
2011-02-24 11:14:19 -07:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
s.error(err)
|
|
|
|
}
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2011-06-01 18:51:31 -06:00
|
|
|
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
switch v := arg.(type) {
|
2010-05-31 11:56:58 -06:00
|
|
|
case *bool:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.scanBool(verb)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *complex64:
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = complex64(s.scanComplex(verb, 64))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *complex128:
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = s.scanComplex(verb, 128)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int(s.scanInt(verb, intBits))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int8:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int8(s.scanInt(verb, 8))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int16:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int16(s.scanInt(verb, 16))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int32:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = int32(s.scanInt(verb, 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *int64:
|
2010-09-05 16:04:53 -06:00
|
|
|
*v = s.scanInt(verb, 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint(s.scanUint(verb, intBits))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint8:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint8(s.scanUint(verb, 8))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint16:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint16(s.scanUint(verb, 16))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint32:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uint32(s.scanUint(verb, 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uint64:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.scanUint(verb, 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
case *uintptr:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = uintptr(s.scanUint(verb, uintptrBits))
|
|
|
|
// Floats are tricky because you want to scan in the precision of the result, not
|
|
|
|
// scan in high precision and convert, in order to preserve the correct error condition.
|
2010-05-31 11:56:58 -06:00
|
|
|
case *float32:
|
2010-05-31 15:53:15 -06:00
|
|
|
if s.okVerb(verb, floatVerbs, "float32") {
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = float32(s.convertFloat(s.floatToken(), 32))
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
case *float64:
|
2010-05-31 15:53:15 -06:00
|
|
|
if s.okVerb(verb, floatVerbs, "float64") {
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2010-06-20 13:16:25 -06:00
|
|
|
*v = s.convertFloat(s.floatToken(), 64)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
|
|
|
case *string:
|
2010-06-01 16:21:21 -06:00
|
|
|
*v = s.convertString(verb)
|
2010-06-02 17:28:01 -06:00
|
|
|
case *[]byte:
|
|
|
|
// We scan to string and convert so we get a copy of the data.
|
|
|
|
// If we scanned to bytes, the slice would point at the buffer.
|
|
|
|
*v = []byte(s.convertString(verb))
|
2010-05-31 11:56:58 -06:00
|
|
|
default:
|
2011-04-25 11:39:36 -06:00
|
|
|
val := reflect.ValueOf(v)
|
2011-04-08 10:27:58 -06:00
|
|
|
ptr := val
|
|
|
|
if ptr.Kind() != reflect.Ptr {
|
2013-07-30 22:09:19 -06:00
|
|
|
s.errorString("type not a pointer: " + val.Type().String())
|
2010-05-31 11:56:58 -06:00
|
|
|
return
|
|
|
|
}
|
2011-04-08 10:27:58 -06:00
|
|
|
switch v := ptr.Elem(); v.Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
v.SetBool(s.scanBool(verb))
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
v.SetInt(s.scanInt(verb, v.Type().Bits()))
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
|
|
|
v.SetUint(s.scanUint(verb, v.Type().Bits()))
|
|
|
|
case reflect.String:
|
|
|
|
v.SetString(s.convertString(verb))
|
|
|
|
case reflect.Slice:
|
2010-06-02 17:28:01 -06:00
|
|
|
// For now, can only handle (renamed) []byte.
|
2011-04-08 10:27:58 -06:00
|
|
|
typ := v.Type()
|
2010-06-20 13:16:25 -06:00
|
|
|
if typ.Elem().Kind() != reflect.Uint8 {
|
2013-07-30 22:09:19 -06:00
|
|
|
s.errorString("can't scan type: " + val.Type().String())
|
2010-06-02 17:28:01 -06:00
|
|
|
}
|
|
|
|
str := s.convertString(verb)
|
|
|
|
v.Set(reflect.MakeSlice(typ, len(str), len(str)))
|
|
|
|
for i := 0; i < len(str); i++ {
|
2011-04-08 10:27:58 -06:00
|
|
|
v.Index(i).SetUint(uint64(str[i]))
|
2010-06-02 17:28:01 -06:00
|
|
|
}
|
2011-04-08 10:27:58 -06:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2017-08-02 16:54:14 -06:00
|
|
|
s.SkipSpace()
|
2011-06-01 18:51:31 -06:00
|
|
|
s.notEOF()
|
2011-04-08 10:27:58 -06:00
|
|
|
v.SetFloat(s.convertFloat(s.floatToken(), v.Type().Bits()))
|
|
|
|
case reflect.Complex64, reflect.Complex128:
|
|
|
|
v.SetComplex(s.scanComplex(verb, v.Type().Bits()))
|
2010-05-31 11:56:58 -06:00
|
|
|
default:
|
2013-07-30 22:09:19 -06:00
|
|
|
s.errorString("can't scan type: " + val.Type().String())
|
2010-06-01 16:21:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 18:51:31 -06:00
|
|
|
// errorHandler turns local panics into error returns.
|
2011-11-01 20:04:37 -06:00
|
|
|
func errorHandler(errp *error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
if e := recover(); e != nil {
|
|
|
|
if se, ok := e.(scanError); ok { // catch local error
|
2011-06-01 18:51:31 -06:00
|
|
|
*errp = se.err
|
2011-11-01 20:04:37 -06:00
|
|
|
} else if eof, ok := e.(error); ok && eof == io.EOF { // out of input
|
2011-06-01 18:51:31 -06:00
|
|
|
*errp = eof
|
2010-06-01 16:21:21 -06:00
|
|
|
} else {
|
|
|
|
panic(e)
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// doScan does the real work for scanning without a format string.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) doScan(a []interface{}) (numProcessed int, err error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
defer errorHandler(&err)
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
for _, arg := range a {
|
|
|
|
s.scanOne('v', arg)
|
2010-06-01 16:21:21 -06:00
|
|
|
numProcessed++
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2015-06-05 15:23:54 -06:00
|
|
|
// Check for newline (or EOF) if required (Scanln etc.).
|
|
|
|
if s.nlIsEnd {
|
2010-05-25 22:02:35 -06:00
|
|
|
for {
|
2011-10-25 23:21:33 -06:00
|
|
|
r := s.getRune()
|
|
|
|
if r == '\n' || r == eof {
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
2012-03-06 21:27:11 -07:00
|
|
|
if !isSpace(r) {
|
2013-07-30 22:09:19 -06:00
|
|
|
s.errorString("expected newline")
|
2010-05-25 22:02:35 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-25 22:02:35 -06:00
|
|
|
}
|
2010-05-31 11:56:58 -06:00
|
|
|
|
2010-06-02 17:28:01 -06:00
|
|
|
// advance determines whether the next characters in the input match
|
2015-06-05 15:23:54 -06:00
|
|
|
// those of the format. It returns the number of bytes (sic) consumed
|
|
|
|
// in the format. All runs of space characters in either input or
|
|
|
|
// format behave as a single space. Newlines are special, though:
|
|
|
|
// newlines in the format must match those in the input and vice versa.
|
|
|
|
// This routine also handles the %% case. If the return value is zero,
|
|
|
|
// either format starts with a % (with no following %) or the input
|
|
|
|
// is empty. If it is negative, the input did not match the string.
|
2010-06-02 15:58:31 -06:00
|
|
|
func (s *ss) advance(format string) (i int) {
|
|
|
|
for i < len(format) {
|
|
|
|
fmtc, w := utf8.DecodeRuneInString(format[i:])
|
2016-10-06 15:08:44 -06:00
|
|
|
|
|
|
|
// Space processing.
|
|
|
|
// In the rest of this comment "space" means spaces other than newline.
|
|
|
|
// Newline in the format matches input of zero or more spaces and then newline or end-of-input.
|
|
|
|
// Spaces in the format before the newline are collapsed into the newline.
|
|
|
|
// Spaces in the format after the newline match zero or more spaces after the corresponding input newline.
|
|
|
|
// Other spaces in the format match input of one or more spaces or end-of-input.
|
|
|
|
if isSpace(fmtc) {
|
|
|
|
newlines := 0
|
|
|
|
trailingSpace := false
|
|
|
|
for isSpace(fmtc) && i < len(format) {
|
|
|
|
if fmtc == '\n' {
|
|
|
|
newlines++
|
|
|
|
trailingSpace = false
|
|
|
|
} else {
|
|
|
|
trailingSpace = true
|
|
|
|
}
|
|
|
|
i += w
|
|
|
|
fmtc, w = utf8.DecodeRuneInString(format[i:])
|
|
|
|
}
|
|
|
|
for j := 0; j < newlines; j++ {
|
|
|
|
inputc := s.getRune()
|
|
|
|
for isSpace(inputc) && inputc != '\n' {
|
|
|
|
inputc = s.getRune()
|
|
|
|
}
|
|
|
|
if inputc != '\n' && inputc != eof {
|
|
|
|
s.errorString("newline in format does not match input")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if trailingSpace {
|
|
|
|
inputc := s.getRune()
|
|
|
|
if newlines == 0 {
|
|
|
|
// If the trailing space stood alone (did not follow a newline),
|
|
|
|
// it must find at least one space to consume.
|
|
|
|
if !isSpace(inputc) && inputc != eof {
|
|
|
|
s.errorString("expected space in input to match format")
|
|
|
|
}
|
|
|
|
if inputc == '\n' {
|
|
|
|
s.errorString("newline in input does not match format")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for isSpace(inputc) && inputc != '\n' {
|
|
|
|
inputc = s.getRune()
|
|
|
|
}
|
|
|
|
if inputc != eof {
|
|
|
|
s.UnreadRune()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verbs.
|
2010-06-02 15:58:31 -06:00
|
|
|
if fmtc == '%' {
|
2015-11-20 14:17:32 -07:00
|
|
|
// % at end of string is an error.
|
|
|
|
if i+w == len(format) {
|
|
|
|
s.errorString("missing verb: % at end of format string")
|
|
|
|
}
|
2010-06-02 15:58:31 -06:00
|
|
|
// %% acts like a real percent
|
|
|
|
nextc, _ := utf8.DecodeRuneInString(format[i+w:]) // will not match % if string is empty
|
|
|
|
if nextc != '%' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i += w // skip the first %
|
|
|
|
}
|
2016-10-06 15:08:44 -06:00
|
|
|
|
|
|
|
// Literals.
|
2011-03-02 11:54:23 -07:00
|
|
|
inputc := s.mustReadRune()
|
2010-06-02 15:58:31 -06:00
|
|
|
if fmtc != inputc {
|
2011-03-02 11:54:23 -07:00
|
|
|
s.UnreadRune()
|
2010-06-18 21:37:03 -06:00
|
|
|
return -1
|
2010-06-02 15:58:31 -06:00
|
|
|
}
|
|
|
|
i += w
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-05-31 11:56:58 -06:00
|
|
|
// doScanf does the real work when scanning with a format string.
|
2016-02-24 15:22:40 -07:00
|
|
|
// At the moment, it handles only pointers to basic types.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (s *ss) doScanf(format string, a []interface{}) (numProcessed int, err error) {
|
2010-06-01 16:21:21 -06:00
|
|
|
defer errorHandler(&err)
|
2010-05-31 11:56:58 -06:00
|
|
|
end := len(format) - 1
|
2010-06-01 16:21:21 -06:00
|
|
|
// We process one item per non-trivial format
|
2010-05-31 11:56:58 -06:00
|
|
|
for i := 0; i <= end; {
|
2015-12-10 20:28:15 -07:00
|
|
|
w := s.advance(format[i:])
|
|
|
|
if w > 0 {
|
2010-05-31 11:56:58 -06:00
|
|
|
i += w
|
|
|
|
continue
|
|
|
|
}
|
2015-12-10 20:28:15 -07:00
|
|
|
// Either we failed to advance, we have a percent character, or we ran out of input.
|
2010-06-02 15:58:31 -06:00
|
|
|
if format[i] != '%' {
|
2016-03-01 16:21:55 -07:00
|
|
|
// Can't advance format. Why not?
|
2015-12-10 20:28:15 -07:00
|
|
|
if w < 0 {
|
|
|
|
s.errorString("input does not match format")
|
|
|
|
}
|
2010-06-18 21:37:03 -06:00
|
|
|
// Otherwise at EOF; "too many operands" error handled below
|
2010-06-02 15:58:31 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
i++ // % is one byte
|
|
|
|
|
2010-06-03 17:06:41 -06:00
|
|
|
// do we have 20 (width)?
|
2011-03-02 11:54:23 -07:00
|
|
|
var widPresent bool
|
|
|
|
s.maxWid, widPresent, i = parsenum(format, i, end)
|
|
|
|
if !widPresent {
|
|
|
|
s.maxWid = hugeWid
|
|
|
|
}
|
2015-08-23 01:45:58 -06:00
|
|
|
|
|
|
|
c, w := utf8.DecodeRuneInString(format[i:])
|
|
|
|
i += w
|
|
|
|
|
|
|
|
if c != 'c' {
|
|
|
|
s.SkipSpace()
|
|
|
|
}
|
2019-10-24 01:22:43 -06:00
|
|
|
if c == '%' {
|
|
|
|
s.scanPercent()
|
|
|
|
continue // Do not consume an argument.
|
|
|
|
}
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
s.argLimit = s.limit
|
|
|
|
if f := s.count + s.maxWid; f < s.argLimit {
|
|
|
|
s.argLimit = f
|
2011-03-03 11:43:29 -07:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
|
2010-06-01 16:21:21 -06:00
|
|
|
if numProcessed >= len(a) { // out of operands
|
2015-11-20 14:17:32 -07:00
|
|
|
s.errorString("too few operands for format '%" + format[i-w:] + "'")
|
2010-05-31 15:53:15 -06:00
|
|
|
break
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
arg := a[numProcessed]
|
2010-05-31 11:56:58 -06:00
|
|
|
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
s.scanOne(c, arg)
|
2010-06-01 16:21:21 -06:00
|
|
|
numProcessed++
|
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
R=rsc, bradfitz, rogpeppe, minux.ma, peter.armitage
CC=golang-dev
https://golang.org/cl/9680043
2013-05-24 16:49:26 -06:00
|
|
|
s.argLimit = s.limit
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|
2010-06-03 17:06:41 -06:00
|
|
|
if numProcessed < len(a) {
|
|
|
|
s.errorString("too many operands")
|
|
|
|
}
|
2010-06-01 16:21:21 -06:00
|
|
|
return
|
2010-05-31 11:56:58 -06:00
|
|
|
}
|