diff options
| author | favonia <favonia@gmail.com> | 2023-05-24 21:43:15 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-10-23 09:06:30 +0000 |
| commit | bc2124dab14fa292e18df2937037d782f7868635 (patch) | |
| tree | f7e2a17ee720b67b592680a00a3041af64b85ca3 /src/bufio/example_test.go | |
| parent | 6ba6e72e39ac5be92f604832640c3131ad3d0ade (diff) | |
| download | go-bc2124dab14fa292e18df2937037d782f7868635.tar.xz | |
bufio: allow terminating Scanner early cleanly without a final token or an error
Fixes #56381
Change-Id: I95cd603831a7032d764ab312869fe9fb05848a4b
Reviewed-on: https://go-review.googlesource.com/c/go/+/498117
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Benny Siegert <bsiegert@gmail.com>
Diffstat (limited to 'src/bufio/example_test.go')
| -rw-r--r-- | src/bufio/example_test.go | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/bufio/example_test.go b/src/bufio/example_test.go index a864d11012..6d219aecc6 100644 --- a/src/bufio/example_test.go +++ b/src/bufio/example_test.go @@ -6,6 +6,7 @@ package bufio_test import ( "bufio" + "bytes" "fmt" "os" "strconv" @@ -137,3 +138,36 @@ func ExampleScanner_emptyFinalToken() { } // Output: "1" "2" "3" "4" "" } + +// Use a Scanner with a custom split function to parse a comma-separated +// list with an empty final value but stops at the token "STOP". +func ExampleScanner_earlyStop() { + onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) { + i := bytes.IndexByte(data, ',') + if i == -1 { + if !atEOF { + return 0, nil, nil + } + // If we have reached the end, return the last token. + return 0, data, bufio.ErrFinalToken + } + // If the token is "STOP", stop the scanning and ignore the rest. + if string(data[:i]) == "STOP" { + return i + 1, nil, bufio.ErrFinalToken + } + // Otherwise, return the token before the comma. + return i + 1, data[:i], nil + } + const input = "1,2,STOP,4," + scanner := bufio.NewScanner(strings.NewReader(input)) + scanner.Split(onComma) + for scanner.Scan() { + fmt.Printf("Got a token %q\n", scanner.Text()) + } + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "reading input:", err) + } + // Output: + // Got a token "1" + // Got a token "2" +} |
