Revert r215321.
[official-gcc.git] / libgo / go / bufio / scan.go
blob715ce071e3bb8113595620067ae11ee7a8834b99
1 // Copyright 2013 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.
5 package bufio
7 import (
8 "bytes"
9 "errors"
10 "io"
11 "unicode/utf8"
14 // Scanner provides a convenient interface for reading data such as
15 // a file of newline-delimited lines of text. Successive calls to
16 // the Scan method will step through the 'tokens' of a file, skipping
17 // the bytes between the tokens. The specification of a token is
18 // defined by a split function of type SplitFunc; the default split
19 // function breaks the input into lines with line termination stripped. Split
20 // functions are defined in this package for scanning a file into
21 // lines, bytes, UTF-8-encoded runes, and space-delimited words. The
22 // client may instead provide a custom split function.
24 // Scanning stops unrecoverably at EOF, the first I/O error, or a token too
25 // large to fit in the buffer. When a scan stops, the reader may have
26 // advanced arbitrarily far past the last token. Programs that need more
27 // control over error handling or large tokens, or must run sequential scans
28 // on a reader, should use bufio.Reader instead.
30 type Scanner struct {
31 r io.Reader // The reader provided by the client.
32 split SplitFunc // The function to split the tokens.
33 maxTokenSize int // Maximum size of a token; modified by tests.
34 token []byte // Last token returned by split.
35 buf []byte // Buffer used as argument to split.
36 start int // First non-processed byte in buf.
37 end int // End of data in buf.
38 err error // Sticky error.
41 // SplitFunc is the signature of the split function used to tokenize the
42 // input. The arguments are an initial substring of the remaining unprocessed
43 // data and a flag, atEOF, that reports whether the Reader has no more data
44 // to give. The return values are the number of bytes to advance the input
45 // and the next token to return to the user, plus an error, if any. If the
46 // data does not yet hold a complete token, for instance if it has no newline
47 // while scanning lines, SplitFunc can return (0, nil, nil) to signal the
48 // Scanner to read more data into the slice and try again with a longer slice
49 // starting at the same point in the input.
51 // If the returned error is non-nil, scanning stops and the error
52 // is returned to the client.
54 // The function is never called with an empty data slice unless atEOF
55 // is true. If atEOF is true, however, data may be non-empty and,
56 // as always, holds unprocessed text.
57 type SplitFunc func(data []byte, atEOF bool) (advance int, token []byte, err error)
59 // Errors returned by Scanner.
60 var (
61 ErrTooLong = errors.New("bufio.Scanner: token too long")
62 ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
63 ErrAdvanceTooFar = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
66 const (
67 // Maximum size used to buffer a token. The actual maximum token size
68 // may be smaller as the buffer may need to include, for instance, a newline.
69 MaxScanTokenSize = 64 * 1024
72 // NewScanner returns a new Scanner to read from r.
73 // The split function defaults to ScanLines.
74 func NewScanner(r io.Reader) *Scanner {
75 return &Scanner{
76 r: r,
77 split: ScanLines,
78 maxTokenSize: MaxScanTokenSize,
79 buf: make([]byte, 4096), // Plausible starting size; needn't be large.
83 // Err returns the first non-EOF error that was encountered by the Scanner.
84 func (s *Scanner) Err() error {
85 if s.err == io.EOF {
86 return nil
88 return s.err
91 // Bytes returns the most recent token generated by a call to Scan.
92 // The underlying array may point to data that will be overwritten
93 // by a subsequent call to Scan. It does no allocation.
94 func (s *Scanner) Bytes() []byte {
95 return s.token
98 // Text returns the most recent token generated by a call to Scan
99 // as a newly allocated string holding its bytes.
100 func (s *Scanner) Text() string {
101 return string(s.token)
104 // Scan advances the Scanner to the next token, which will then be
105 // available through the Bytes or Text method. It returns false when the
106 // scan stops, either by reaching the end of the input or an error.
107 // After Scan returns false, the Err method will return any error that
108 // occurred during scanning, except that if it was io.EOF, Err
109 // will return nil.
110 func (s *Scanner) Scan() bool {
111 // Loop until we have a token.
112 for {
113 // See if we can get a token with what we already have.
114 if s.end > s.start {
115 advance, token, err := s.split(s.buf[s.start:s.end], s.err != nil)
116 if err != nil {
117 s.setErr(err)
118 return false
120 if !s.advance(advance) {
121 return false
123 s.token = token
124 if token != nil {
125 return true
128 // We cannot generate a token with what we are holding.
129 // If we've already hit EOF or an I/O error, we are done.
130 if s.err != nil {
131 // Shut it down.
132 s.start = 0
133 s.end = 0
134 return false
136 // Must read more data.
137 // First, shift data to beginning of buffer if there's lots of empty space
138 // or space is needed.
139 if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
140 copy(s.buf, s.buf[s.start:s.end])
141 s.end -= s.start
142 s.start = 0
144 // Is the buffer full? If so, resize.
145 if s.end == len(s.buf) {
146 if len(s.buf) >= s.maxTokenSize {
147 s.setErr(ErrTooLong)
148 return false
150 newSize := len(s.buf) * 2
151 if newSize > s.maxTokenSize {
152 newSize = s.maxTokenSize
154 newBuf := make([]byte, newSize)
155 copy(newBuf, s.buf[s.start:s.end])
156 s.buf = newBuf
157 s.end -= s.start
158 s.start = 0
159 continue
161 // Finally we can read some input. Make sure we don't get stuck with
162 // a misbehaving Reader. Officially we don't need to do this, but let's
163 // be extra careful: Scanner is for safe, simple jobs.
164 for loop := 0; ; {
165 n, err := s.r.Read(s.buf[s.end:len(s.buf)])
166 s.end += n
167 if err != nil {
168 s.setErr(err)
169 break
171 if n > 0 {
172 break
174 loop++
175 if loop > maxConsecutiveEmptyReads {
176 s.setErr(io.ErrNoProgress)
177 break
183 // advance consumes n bytes of the buffer. It reports whether the advance was legal.
184 func (s *Scanner) advance(n int) bool {
185 if n < 0 {
186 s.setErr(ErrNegativeAdvance)
187 return false
189 if n > s.end-s.start {
190 s.setErr(ErrAdvanceTooFar)
191 return false
193 s.start += n
194 return true
197 // setErr records the first error encountered.
198 func (s *Scanner) setErr(err error) {
199 if s.err == nil || s.err == io.EOF {
200 s.err = err
204 // Split sets the split function for the Scanner. If called, it must be
205 // called before Scan. The default split function is ScanLines.
206 func (s *Scanner) Split(split SplitFunc) {
207 s.split = split
210 // Split functions
212 // ScanBytes is a split function for a Scanner that returns each byte as a token.
213 func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error) {
214 if atEOF && len(data) == 0 {
215 return 0, nil, nil
217 return 1, data[0:1], nil
220 var errorRune = []byte(string(utf8.RuneError))
222 // ScanRunes is a split function for a Scanner that returns each
223 // UTF-8-encoded rune as a token. The sequence of runes returned is
224 // equivalent to that from a range loop over the input as a string, which
225 // means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd".
226 // Because of the Scan interface, this makes it impossible for the client to
227 // distinguish correctly encoded replacement runes from encoding errors.
228 func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error) {
229 if atEOF && len(data) == 0 {
230 return 0, nil, nil
233 // Fast path 1: ASCII.
234 if data[0] < utf8.RuneSelf {
235 return 1, data[0:1], nil
238 // Fast path 2: Correct UTF-8 decode without error.
239 _, width := utf8.DecodeRune(data)
240 if width > 1 {
241 // It's a valid encoding. Width cannot be one for a correctly encoded
242 // non-ASCII rune.
243 return width, data[0:width], nil
246 // We know it's an error: we have width==1 and implicitly r==utf8.RuneError.
247 // Is the error because there wasn't a full rune to be decoded?
248 // FullRune distinguishes correctly between erroneous and incomplete encodings.
249 if !atEOF && !utf8.FullRune(data) {
250 // Incomplete; get more bytes.
251 return 0, nil, nil
254 // We have a real UTF-8 encoding error. Return a properly encoded error rune
255 // but advance only one byte. This matches the behavior of a range loop over
256 // an incorrectly encoded string.
257 return 1, errorRune, nil
260 // dropCR drops a terminal \r from the data.
261 func dropCR(data []byte) []byte {
262 if len(data) > 0 && data[len(data)-1] == '\r' {
263 return data[0 : len(data)-1]
265 return data
268 // ScanLines is a split function for a Scanner that returns each line of
269 // text, stripped of any trailing end-of-line marker. The returned line may
270 // be empty. The end-of-line marker is one optional carriage return followed
271 // by one mandatory newline. In regular expression notation, it is `\r?\n`.
272 // The last non-empty line of input will be returned even if it has no
273 // newline.
274 func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
275 if atEOF && len(data) == 0 {
276 return 0, nil, nil
278 if i := bytes.IndexByte(data, '\n'); i >= 0 {
279 // We have a full newline-terminated line.
280 return i + 1, dropCR(data[0:i]), nil
282 // If we're at EOF, we have a final, non-terminated line. Return it.
283 if atEOF {
284 return len(data), dropCR(data), nil
286 // Request more data.
287 return 0, nil, nil
290 // isSpace reports whether the character is a Unicode white space character.
291 // We avoid dependency on the unicode package, but check validity of the implementation
292 // in the tests.
293 func isSpace(r rune) bool {
294 if r <= '\u00FF' {
295 // Obvious ASCII ones: \t through \r plus space. Plus two Latin-1 oddballs.
296 switch r {
297 case ' ', '\t', '\n', '\v', '\f', '\r':
298 return true
299 case '\u0085', '\u00A0':
300 return true
302 return false
304 // High-valued ones.
305 if '\u2000' <= r && r <= '\u200a' {
306 return true
308 switch r {
309 case '\u1680', '\u2028', '\u2029', '\u202f', '\u205f', '\u3000':
310 return true
312 return false
315 // ScanWords is a split function for a Scanner that returns each
316 // space-separated word of text, with surrounding spaces deleted. It will
317 // never return an empty string. The definition of space is set by
318 // unicode.IsSpace.
319 func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
320 // Skip leading spaces.
321 start := 0
322 for width := 0; start < len(data); start += width {
323 var r rune
324 r, width = utf8.DecodeRune(data[start:])
325 if !isSpace(r) {
326 break
329 if atEOF && len(data) == 0 {
330 return 0, nil, nil
332 // Scan until space, marking end of word.
333 for width, i := 0, start; i < len(data); i += width {
334 var r rune
335 r, width = utf8.DecodeRune(data[i:])
336 if isSpace(r) {
337 return i + width, data[start:i], nil
340 // If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
341 if atEOF && len(data) > start {
342 return len(data), data[start:], nil
344 // Request more data.
345 return 0, nil, nil