-
Notifications
You must be signed in to change notification settings - Fork 18k
bufio: allow terminating Scanner early cleanly without a final token or an error #56381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
See the issue #11836 and the commit ec12754 where Please note that a final empty token is a valid token. Apps could depend on this behavior and expect an empty final token. So this proposal could break such apps. If a caller of the It would be better to provide one or more concrete use cases to support your proposal. |
@ZekeLu No, what I meant is a
I believe we are talking across each other. The code is dealing with
For example, it is useful to write a parser for a language that supports "quit":
|
Ah, I get it now. Thank you for the clarification! According to the doc:
I think you are correct that |
Talked to @robpike (author of Scan) about this, and it seems reasonable. |
This proposal has been added to the active column of the proposals project |
As a very quick summary, right now if a SplitFunc returns 0, nil, ErrFinalToken, the result of Scan is a final "" before the end of the sequence. That empty string is almost never actually wanted. So the change is to let nil indicate "no empty string token". Users who want empty strings can still return []byte{}, but of course very few scanners actually want empty strings. |
I can confirm the quick summary is accurate. Thanks! |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/451495 mentions this issue: |
Change https://go.dev/cl/498117 mentions this issue: |
Why
Currently, the clean way to end the scanning early is to use the error
ErrFinalToken
. However, when it is used, theScanner.Scan
method will unconditionally returntrue
even if the token isnil
, generating an extra token (viaScanner.Text
). (Please note that this is different from the splitting function returning an empty slice. This proposal only covers the case where the second return value is exactlynil
; every other case, including the case where the second return value is an empty slice, should remain the same.) In other words, aSplitFunc
cannot returnas a way to cleanly terminate the scanning without adding an extra token. If I understand the logic correctly, a user thus must use a custom error or specifically check whether
Scanner.Bytes
returnsnil
to achieve token-free early termination, which is a bit awkward. However, I wonder if it makes sense forScanner
to skip thenil
token in this very special case? No matter whether this proposal is accepted or not, I hope this tricky case can be more explicitly mentioned in the documentation ofSplitFunc
and/orScanner.Scan
.Proposed code change
In
Scanner.Sacn
,Example exploiting the proposed API
(This is adapted from the existing example
ExampleScanner_emptyFinalToken
.)Output before the change:
Output after the change:
Edits
nil
tokens with empty tokens. I updated the text with more clarification. I also fixed the typo in the code.The text was updated successfully, but these errors were encountered: