1
0
mirror of https://github.com/golang/go synced 2024-09-30 06:14:31 -06:00
Commit Graph

40 Commits

Author SHA1 Message Date
Agniva De Sarker
5126feadd6 bufio: fix emptyFinalToken example to handle multiple Reads
Fixes #25909

Change-Id: I9a53a1a06aab5d1877a8e9b1b8b782d77d6027a8
Reviewed-on: https://go-review.googlesource.com/c/157758
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-02 18:28:10 +00:00
Brad Fitzpatrick
9473c044f1 bufio: document relationship between UnreadByte/UnreadRune and Peek
Fixes #29387

Change-Id: I2d9981f63ac16630ed39d6da6692c81396f4e9ea
Reviewed-on: https://go-review.googlesource.com/c/155930
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-01-08 23:47:27 +00:00
Martin Garton
e51b19a993 bufio: make Reader.Peek invalidate Unreads
Since Reader.Peek potentially reads from the underlying io.Reader,
discarding previous buffers, UnreadRune and UnreadByte cannot
necessarily work.  Change Peek to invalidate the unread buffers in all
cases (as allowed according to the documentation) and thus prevent
hiding bugs in the caller.

(This change was previoiusly merged and then reverted due concern about
being too close to a release)

Fixes #18556

Change-Id: I9027d75aa834d4b27703f37711ba25de04d89f3c
GitHub-Last-Rev: 917ef1e511
GitHub-Pull-Request: golang/go#28768
Reviewed-on: https://go-review.googlesource.com/c/149297
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-11-13 15:08:13 +00:00
Russ Cox
de50ea3cd8 bufio: put notes about len(p) together
CL 145577 added the part about io.ReadFull to read len(p)
but it should be next to the existing sentence about not
reading len(p) bytes.

Change-Id: Idfa037c59a3085d44d5da6129188473db0e96d23
Reviewed-on: https://go-review.googlesource.com/c/148903
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-13 03:25:22 +00:00
Ryan Dahl
410d63dbe9 bufio: fix indexes in TestWriter
Change-Id: I393c53d6f7b526d156226502544725a4cb9fb118
GitHub-Last-Rev: 5d53406c70
GitHub-Pull-Request: golang/go#28693
Reviewed-on: https://go-review.googlesource.com/c/148818
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-09 19:15:11 +00:00
Alan Donovan
891682c825 bufio: suggest io.ReadFull at (*Reader).Read
Many times when using bufio.Reader I imagine, incorrectly, that it
implements the retry loop itself, being a high-level buffered wrapper
around, say, a file descriptor prone to short reads. This comment
would have saved me much time.

Change-Id: I34c790e0d7c1515430a76d02ce4739b586a36ba7
Reviewed-on: https://go-review.googlesource.com/c/145577
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
2018-10-30 13:03:46 +00:00
andrius4669
a21ae28f39 bufio: avoid rescanning buffer multiple times in ReadSlice
When existing data in buffer does not have delimiter,
and new data is added with b.fill(), continue search from
previous point instead of starting from beginning.

Change-Id: Id78332afe2b0281b4a3c86bd1ffe9449cfea7848
GitHub-Last-Rev: 08e7d2f501
GitHub-Pull-Request: golang/go#25441
Reviewed-on: https://go-review.googlesource.com/113535
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-08-22 17:06:28 +00:00
nogoegst
a1addf15df bufio: make Reader naming consistent
All the readers are denoted as `b` while for `Reader.Size()` it is `r`.

Change-Id: Ib6f97306c11b3abb2ff30edbc9f9362cad36d080
GitHub-Last-Rev: 992f88b374
GitHub-Pull-Request: golang/go#26205
Reviewed-on: https://go-review.googlesource.com/122156
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-08-20 21:51:48 +00:00
Ian Lance Taylor
1e721cfc43 bufio: clarify SplitFunc docs for nil token
Fixes #25472

Change-Id: Idb72ed06a3dc43c49ab984a80f8885352b036465
Reviewed-on: https://go-review.googlesource.com/118695
Reviewed-by: Rob Pike <r@golang.org>
2018-06-13 21:43:06 +00:00
Ian Lance Taylor
c53ff2818a bufio: document ReadFrom/WriteTo calls to underlying methods
In general use of these magic methods must be documented so that
users understand what will happen.

Fixes #23289

Change-Id: Ic46915eee1d3b7e57d8d1886834ddfb2e8e66e62
Reviewed-on: https://go-review.googlesource.com/103238
Reviewed-by: Rob Pike <r@golang.org>
2018-03-28 22:21:52 +00:00
Ryoichi KATO
50ec831749 bufio: Use maxConsecutiveEmptyReads instead of 100
Use maxConsecutiveEmptyReads const instead of hardcoded
100 in scan.go too.

Change-Id: I993f353a3748f0d6bdefab38bf5cb224eea8a969
Reviewed-on: https://go-review.googlesource.com/46915
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-11-14 05:21:00 +00:00
Brad Fitzpatrick
6128ff84f1 bufio: add Reader.Size and Writer.Size accessors
Fixes #21343

Change-Id: I3582fced902592fe12bfa29acf7b40b6e5e554a7
Reviewed-on: https://go-review.googlesource.com/75150
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2017-11-02 17:17:44 +00:00
Russ Cox
e71285c4c3 Revert "bufio: make Reader.Peek invalidate Unreads"
Go 1.9 beta 2 is already out.
It's too late to break code with a change like this.
This can be rolled forward for Go 1.10.

This reverts commit ae238688d2.

Change-Id: Ib67b8629e3deac5d50d76581aba6a91ca7a7853e
Reviewed-on: https://go-review.googlesource.com/47570
Reviewed-by: Russ Cox <rsc@golang.org>
2017-07-06 03:06:44 +00:00
Martin Garton
ae238688d2 bufio: make Reader.Peek invalidate Unreads
Since Reader.Peek potentially reads from the underlying io.Reader,
discarding previous buffers, UnreadRune and UnreadByte cannot
necessarily work.  Change Peek to invalidate the unread buffers in all
cases (as allowed according to the documentation) and thus prevent
hiding bugs in the caller.

Fixes #18556

Change-Id: I8d836db7ce31c4aaecb4f61c24573b0332bbf30d
Reviewed-on: https://go-review.googlesource.com/46850
Reviewed-by: Robert Griesemer <gri@golang.org>
2017-06-27 19:51:54 +00:00
Brad Fitzpatrick
af2ac479fc all: single space after period
Done with grep & interactive search & replace, to double-check
replacements. Not many remained after CL 20022.

Fixes #18572

Change-Id: Idbe90ba3b584f9b9661d2bbd141607daaadfa41a
Reviewed-on: https://go-review.googlesource.com/45270
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2017-06-09 20:29:09 +00:00
Sam Whited
6a34765a16 bufio: clarify WriteTo docs
Fixes #19092

Change-Id: I7c0fde6a4cf460017619dbcce1c1ddaa7af1022a
Reviewed-on: https://go-review.googlesource.com/44811
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-06-08 17:15:35 +00:00
Ian Lance Taylor
2094807913 bufio: clarify that Flush returns a cached write error
Change-Id: I377403fc0981d58aec5d84a1dd0d4e08532a575c
Reviewed-on: https://go-review.googlesource.com/41291
Reviewed-by: Dan Peterson <dpiddy@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-04-21 00:48:51 +00:00
Daniel Martí
ff7994ac10 all: remove redundant returns
Returns at the end of func bodies where the funcs have no return values
are pointless.

Change-Id: I0da5ea78671503e41a9f56dd770df8c919310ce5
Reviewed-on: https://go-review.googlesource.com/41093
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-04-19 20:03:51 +00:00
Quentin Smith
0eaa8fe03f bufio: remove unnecessary "continue"
After resizing the scan buffer, we can immediately read into the
newly-resized buffer since we know there is now space.

Fixes #15712.

Change-Id: I56fcfaeb67045ee753a012c37883aa7c81b6e877
Reviewed-on: https://go-review.googlesource.com/31715
Run-TryBot: Quentin Smith <quentin@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2016-10-24 14:49:44 +00:00
Russ Cox
0794dce072 bufio: read from underlying reader at most once in Read
Fixes #17059.

Change-Id: I5c7ee46604399f7dc3c3c49f964cbb1aa6c0d621
Reviewed-on: https://go-review.googlesource.com/31320
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-10-18 12:56:55 +00:00
Matt Layher
d166a369a8 bufio: remove redundant Writer.flush method
Fixes #17232

Change-Id: I34df86f79b643dce9f054c6df6782e6037c06910
Reviewed-on: https://go-review.googlesource.com/30158
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-10-01 15:46:15 +00:00
Joe Tsai
acc757f678 all: use SeekStart, SeekCurrent, SeekEnd
CL/19862 (f79b50b8d5) recently introduced the constants
SeekStart, SeekCurrent, and SeekEnd to the io package. We should use these constants
consistently throughout the code base.

Updates #15269

Change-Id: If7fcaca7676e4a51f588528f5ced28220d9639a2
Reviewed-on: https://go-review.googlesource.com/22097
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Joe Tsai <joetsai@digital-static.net>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-06 00:10:41 +00:00
Martin Möhrmann
012557b376 all: replace magic 0x80 with named constant utf8.RuneSelf
Change-Id: Id1c2e8e9d60588de866e8b6ca59cc83dd28f848f
Reviewed-on: https://go-review.googlesource.com/21756
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-10 15:15:57 +00:00
Dominik Honnef
b2cf571040 all: delete dead test code
This deletes unused code and helpers from tests.

Change-Id: Ie31d46115f558ceb8da6efbf90c3c204e03b0d7e
Reviewed-on: https://go-review.googlesource.com/20927
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-21 07:10:08 +00:00
djherbis
14bf9c8c07 bufio: Reader.Peek returns partial results on ErrBufferFull
The existing implementation returns nil, ErrBufferFull when n > len(b.buf),
now it will return any data in the buffer and ErrBufferFull.

Fixes #14121

Change-Id: Ie52d32ccd80e4078ebfae6e75393c89675959ead
Reviewed-on: https://go-review.googlesource.com/19091
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2016-03-04 03:51:22 +00:00
Brad Fitzpatrick
5fea2ccc77 all: single space after period.
The tree's pretty inconsistent about single space vs double space
after a period in documentation. Make it consistently a single space,
per earlier decisions. This means contributors won't be confused by
misleading precedence.

This CL doesn't use go/doc to parse. It only addresses // comments.
It was generated with:

$ perl -i -npe 's,^(\s*// .+[a-z]\.)  +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.)  +([A-Z])')
$ go test go/doc -update

Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7
Reviewed-on: https://go-review.googlesource.com/20022
Reviewed-by: Rob Pike <r@golang.org>
Reviewed-by: Dave Day <djd@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-02 00:13:47 +00:00
Brad Fitzpatrick
351c15f1ce all: remove public named return values when useless
Named returned values should only be used on public funcs and methods
when it contributes to the documentation.

Named return values should not be used if they're only saving the
programmer a few lines of code inside the body of the function,
especially if that means there's stutter in the documentation or it
was only there so the programmer could use a naked return
statement. (Naked returns should not be used except in very small
functions)

This change is a manual audit & cleanup of public func signatures.

Signatures were not changed if:

* the func was private (wouldn't be in public godoc)
* the documentation referenced it
* the named return value was an interesting name. (i.e. it wasn't
  simply stutter, repeating the name of the type)

There should be no changes in behavior. (At least: none intended)

Change-Id: I3472ef49619678fe786e5e0994bdf2d9de76d109
Reviewed-on: https://go-review.googlesource.com/20024
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-02-29 03:31:19 +00:00
Martin Möhrmann
fdd0179bb1 all: fix typos and spelling
Change-Id: Icd06d99c42b8299fd931c7da821e1f418684d913
Reviewed-on: https://go-review.googlesource.com/19829
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-24 18:42:29 +00:00
Russ Cox
829425d3ce bufio: clarify Read docs
Or at least make them true.

Fixes #12237.

Change-Id: I3c92a07233b2174c5731d6fa7fbb9ca3a97beb6b
Reviewed-on: https://go-review.googlesource.com/17237
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2015-12-01 20:05:25 +00:00
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
Rob Pike
7b5af511a5 bufio: fix overflow calculation in Scan
I was being too clever, as usual. Write the obvious code to make sure
that when we grow the buffer we don't overflow.

Change-Id: I1641831177b0bb8a89ab6e9bcabccf6c2fcfe1d2
Reviewed-on: https://go-review.googlesource.com/14781
Reviewed-by: Minux Ma <minux@golang.org>
2015-09-20 04:46:08 +00:00
Rob Pike
13be616e56 bufio: allow Scanner to accept a user-provided buffer
Add Scanner.Buffer, which lets the user give a buffer to
the scanner and set the maximum token size.

We call it Buffer not SetBuffer for consistency with Split, which
perhaps should have been called SetSplit; too late regardless.

Both Buffer and Split panic if they are called after Scan. The
panic in Split is new, but the comment on the method already
said it needed to be called first, so we might as well add the
verification while we're doing it for Buffer.

This method allows precise user control of storage.

Fixes #11702.

Change-Id: I80e3d0e3830562fdabd4f7b08f322e1378248c39
Reviewed-on: https://go-review.googlesource.com/14599
Reviewed-by: Andrew Gerrand <adg@golang.org>
Reviewed-by: roger peppe <rogpeppe@gmail.com>
2015-09-18 18:56:49 +00:00
Silvan Jegen
c4bfd2de20 bufio: Remove unneeded error initialization
The default value for error is nil so there is no need to assign this
value here.

Change-Id: I4714ef7607996ccbf91b704390e1d1d39ee3847b
Reviewed-on: https://go-review.googlesource.com/12355
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2015-07-18 18:05:23 +00:00
Aaron Jacobs
ecd630de6d bufio: fix incorrect comment on Scanner.Scan
Change-Id: I216511a4bce431de0a468f618a7a7c4da79e2979
Reviewed-on: https://go-review.googlesource.com/7710
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-03-18 00:59:49 +00:00
Brad Fitzpatrick
ee2ecc4552 bufio: add Reader.Discard
Reader.Discard is the complement to Peek. It discards the next n bytes
of input.

We already have Reader.Buffered to see how many bytes of data are
sitting available in memory, and Reader.Peek to get that that buffer
directly. But once you're done with the Peek'd data, you can't get rid
of it, other than Reading it.
Both Read and io.CopyN(ioutil.Discard, bufReader, N) are relatively
slow. People instead resort to multiple blind ReadByte calls, just to
advance the internal b.r variable.

I've wanted this previously, several people have asked for it in the
past on golang-nuts/dev, and somebody just asked me for it again in a
private email. There are a few places in the standard library we'd use
it too.

Change-Id: I85dfad47704a58bd42f6867adbc9e4e1792bc3b0
Reviewed-on: https://go-review.googlesource.com/2260
Reviewed-by: Russ Cox <rsc@golang.org>
2015-01-07 06:37:57 +00:00
Andrew Gerrand
908dcab6f8 bufio: remove unused 'panicked' variable from test
LGTM=r
R=r
CC=golang-codereviews
https://golang.org/cl/166230044
2014-11-06 15:22:29 +11:00
Russ Cox
2d0db8e591 bufio: fix reading of many blank lines in a row
Fixes #9020.

LGTM=bradfitz, r
R=r, bradfitz
CC=golang-codereviews
https://golang.org/cl/170030043
2014-11-05 22:50:24 -05:00
Rob Pike
590f528376 bufio: don't loop generating empty tokens
The new rules for split functions mean that we are exposed
to the common bug of a function that loops forever at EOF.
Pick these off by shutting down the scanner if too many
consecutive empty tokens are delivered.

Fixes #9020.

LGTM=rsc, adg
R=golang-codereviews, rsc, adg, bradfitz
CC=golang-codereviews
https://golang.org/cl/169970043
2014-11-06 09:57:46 +11:00
Rob Pike
74c0de8fb6 bufio: fix handling of empty tokens at end of line/file
Fixes #8672.

LGTM=rsc
R=golang-codereviews, rsc
CC=golang-codereviews
https://golang.org/cl/145390043
2014-09-25 12:45:21 -07:00
Russ Cox
c007ce824d build: move package sources from src/pkg to src
Preparation was in CL 134570043.
This CL contains only the effect of 'hg mv src/pkg/* src'.
For more about the move, see golang.org/s/go14nopkg.
2014-09-08 00:08:51 -04:00