LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package bufio | 5 package bufio |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "io" | 10 "io" |
11 "unicode/utf8" | 11 "unicode/utf8" |
12 ) | 12 ) |
13 | 13 |
14 // Scanner provides a convenient interface for reading data such as | 14 // Scanner provides a convenient interface for reading data such as |
15 // a file of newline-delimited lines of text. Successive calls to | 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 | 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 | 17 // the bytes between the tokens. The specification of a token is |
18 // defined by a split function of type SplitFunc; the default split | 18 // defined by a split function of type SplitFunc; the default split |
19 // function breaks the input into lines with line termination stripped. 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 | 20 // functions are defined in this package for scanning a file into |
21 // lines, bytes, UTF-8-encoded runes, and space-delimited words. The | 21 // lines, bytes, UTF-8-encoded runes, and space-delimited words. The |
22 // client may instead provide a custom split function. | 22 // client may instead provide a custom split function. |
23 // | 23 // |
24 // Scanning stops and is unrecoverable at EOF, the first I/O error, or a token t
oo | 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 | 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 | 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 | 27 // control over error handling or large tokens, or must run sequential scans |
28 // on a reader, should use bufio.Reader instead. | 28 // on a reader, should use bufio.Reader instead. |
29 // | 29 // |
30 type Scanner struct { | 30 type Scanner struct { |
31 r io.Reader // The reader provided by the client. | 31 r io.Reader // The reader provided by the client. |
32 split SplitFunc // The function to split the tokens. | 32 split SplitFunc // The function to split the tokens. |
33 maxTokenSize int // Maximum size of a token; modified by tests. | 33 maxTokenSize int // Maximum size of a token; modified by tests. |
34 token []byte // Last token returned by split. | 34 token []byte // Last token returned by split. |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 return i + width, data[start:i], nil | 337 return i + width, data[start:i], nil |
338 } | 338 } |
339 } | 339 } |
340 // If we're at EOF, we have a final, non-empty, non-terminated word. Ret
urn it. | 340 // If we're at EOF, we have a final, non-empty, non-terminated word. Ret
urn it. |
341 if atEOF && len(data) > start { | 341 if atEOF && len(data) > start { |
342 return len(data), data[start:], nil | 342 return len(data), data[start:], nil |
343 } | 343 } |
344 // Request more data. | 344 // Request more data. |
345 return 0, nil, nil | 345 return 0, nil, nil |
346 } | 346 } |
LEFT | RIGHT |