Dropped PeekWithResult(), because it does not add any substantial performance. A simpler API which is virtually as fast wins any day.

This commit is contained in:
Maurice Makaay 2019-07-27 12:26:02 +00:00
parent fcdd3d4ea7
commit eda71f304e
1 changed files with 5 additions and 22 deletions

View File

@ -22,40 +22,23 @@ type API struct {
stopped bool // a boolean set to true by Stop()
}
// PeekWithResult checks if the upcoming input data matches the provided tokenize.Handler.
// If it does, then true will be returned, false otherwise. The read cursor
// will be kept at the same position, so the next call to Peek() or Accept()
// will start from the same cursor position.
//
// On a successful peek, the results (data + tokens) are returned by the peek.
// They are availablel (as with Accept()) through parse.API.Result.
func (parseAPI *API) PeekWithResult(tokenHandler tokenize.Handler) bool {
tokenAPI := parseAPI.tokenAPI
snap := tokenAPI.MakeSnapshot()
parseAPI.Result.Tokens = nil
parseAPI.Result.Bytes = nil
ok := parseAPI.invokeTokenizeHandler("PeekWithResult", tokenHandler)
if ok {
parseAPI.Result.Tokens = tokenAPI.Output.Tokens()
parseAPI.Result.Bytes = tokenAPI.Output.Bytes()
}
tokenAPI.RestoreSnapshot(snap)
return ok
}
// Peek checks if the upcoming input data matches the provided tokenize.Handler.
// If it does, then true will be returned, false otherwise. The read cursor
// will be kept at the same position, so the next call to Peek() or Accept()
// will start from the same cursor position.
//
// No results (data + tokens) are returned by Peek(). If want access to the data
// through parse.API.Result, make use of PeekWithResult() instead.
// through parse.API.Result, make use of Peek() instead.
func (parseAPI *API) Peek(tokenHandler tokenize.Handler) bool {
tokenAPI := parseAPI.tokenAPI
snap := tokenAPI.MakeSnapshot()
parseAPI.Result.Tokens = nil
parseAPI.Result.Bytes = nil
ok := parseAPI.invokeTokenizeHandler("Peek", tokenHandler)
if ok {
parseAPI.Result.Tokens = tokenAPI.Output.Tokens()
parseAPI.Result.Bytes = tokenAPI.Output.Bytes()
}
tokenAPI.RestoreSnapshot(snap)
return ok
}