Skip to content
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

fix: Include whitespaces in extracted tokens #13738

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions pkg/pattern/tokenization/tokenization.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package tokenization

import (
"bytes"
"unsafe"
)

Expand Down Expand Up @@ -29,15 +28,15 @@ type tokenizer struct {
tokens []string
}

func (t *tokenizer) countOrSaveToken(endTokenPos, skip int) {
func (t *tokenizer) countOrSaveToken(endTokenPos int) {
if t.tokens != nil {
// Intentionally written like this and not with append(), so this can
// panic if we ever exceed the preallocated slice size, since that means
// we have a nasty bug in handleNextToken() below.
t.tokens[t.tokenCount] = t.line[t.tpos:endTokenPos]
}
t.tokenCount++
t.tpos = endTokenPos + skip
t.tpos = endTokenPos
}

func (t *tokenizer) handleNextToken() bool {
Expand All @@ -55,7 +54,7 @@ func (t *tokenizer) handleNextToken() bool {
// outside of a quoted string.
case escaped:
if curQuotePos < 0 && delimiters[c] {
t.countOrSaveToken(p, 1)
t.countOrSaveToken(p + 1)
return true
} else {
escaped = false
Expand Down Expand Up @@ -89,7 +88,7 @@ func (t *tokenizer) handleNextToken() bool {
// If we encounter a delimiter outside of a quote, count or save the
// token and skip the delimiter.
case delimiters[c]:
t.countOrSaveToken(p, 1)
t.countOrSaveToken(p + 1)
return true

// Handle likely JSON object keys that have been serialized without
Expand All @@ -108,11 +107,11 @@ func (t *tokenizer) handleNextToken() bool {
// wasn't a delimiter right before the comma.
case t.maybeJSON && p > t.tpos && (c == ':' || c == ',') && p+1 < lineLen:
if c == ':' && t.line[p-1] == '"' && !delimiters[t.line[p+1]] {
t.countOrSaveToken(p+1, 0)
t.countOrSaveToken(p + 1)
return true
}
if c == ',' && t.line[p+1] == '"' {
t.countOrSaveToken(p, 0)
t.countOrSaveToken(p)
return true
}
}
Expand All @@ -126,12 +125,12 @@ func (t *tokenizer) handleNextToken() bool {
// unterminated quote and the quote itself as a single token, and continue
// fairly normally from there.
if curQuotePos > 0 {
t.countOrSaveToken(curQuotePos+1, 0)
t.countOrSaveToken(curQuotePos + 1)
return true
}

if t.tpos < len(t.line) {
t.countOrSaveToken(len(t.line), 0)
t.countOrSaveToken(len(t.line))
return true
}

Expand Down Expand Up @@ -193,8 +192,6 @@ func (t *tokenizer) tokenize() []string {
}

func PreprocessAndTokenize(content []byte) []string {
content = bytes.TrimSpace(content)

t := tokenizer{rawLine: content, maxTokens: 100} // TODO: parametrize maxTokens

return t.tokenize()
Expand Down
Loading
Loading