1
0
mirror of https://github.com/golang/go synced 2024-11-19 18:54:41 -07:00
go/src/bufio
Rob Pike ec12754700 bufio: fix scanning with a final empty token.
The Scan function's interface to the split function was not sufficient
to handle an empty final token in a pure function; state was required.
This was ugly.

We introduce a special error value that a split function can return
that signals that this token is OK, but is the last one and scanning
should stop immediately _after_ this token.

The same effect could be achieved using the same trick (a special
error value) and checking for that error after Scan finishes, but it's
a little clumsy. Providing a published sentinel value in bufio is
cleaner and means everyone can use the same trick. The result
is an error-free scan.

Rewrite the test (that was only barely working) to use the value
and be more robust.

Also write a new example showing how to do it.

Fixes #11836

Change-Id: Iaae77d0f95b4a2efa0175ced94d93c66353079e8
Reviewed-on: https://go-review.googlesource.com/14924
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-09-25 21:46:13 +00:00
..
bufio_test.go bufio: add Reader.Discard 2015-01-07 06:37:57 +00:00
bufio.go bufio: Remove unneeded error initialization 2015-07-18 18:05:23 +00:00
example_test.go bufio: fix scanning with a final empty token. 2015-09-25 21:46:13 +00:00
export_test.go
scan_test.go bufio: fix scanning with a final empty token. 2015-09-25 21:46:13 +00:00
scan.go bufio: fix scanning with a final empty token. 2015-09-25 21:46:13 +00:00