You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Similar tobufio.Scanner, but wrapsbufio.Reader.ReadLine so linesof arbitrary length can be scanned. It uses a hybrid approach so thatin most cases, when lines are not unusually long, the fast code pathis taken. When lines are unusually long, it uses the per-scannerpre-allocated byte slice to reassemble the fragments into a singleslice of bytes.
Example
Enumerating lines from an io.Reader (drop in replacement for bufio.Scanner)
When you have an io.Reader that you want to enumerate, normally youwrap it inbufio.Scanner. This library is a drop in replacement forthis particular circumstance, and you can change frombufio.NewScanner(r) togobls.NewScanner(r), and no longer have toworry about token too long errors.
If you already have a slice of bytes that you want to enumerate linesfor, it is much more performant to wrap that byte slice withgobls.NewBufferScanner(buf) than to wrap the slice in a io.Readerand call either the above orbufio.NewScanner.
TheBufferScanner is faster thanbufio.Scanner for allbenchmarks. However, on my test system, the regularScanner takesfrom 2% to nearly 40% longer than bufio scanner, depending on thelength of the lines to be scanned. The 40% longer times were onlyobserved when line lengths werebufio.MaxScanTokenSize bytes long.Usually the performance penalty is 2% to 15% of bufio measurements.
Rungo test -bench=. -benchmem on your system for comparison. I'msure the testing method could be improved. Suggestions are welcomed.
For circumstances where there is no concern about enumerating lineswhose lengths are longer than the max token length frombufio, thenI recommend using the standard library.
On the other hand, if you already have a slice of bytes, library ismuch more performant than the equivalentbufio.NewScanner(bytes.NewReader(buf)).