Skip to content

Commit

Permalink
filter: tokenize query to support quoted terms
Browse files Browse the repository at this point in the history
  • Loading branch information
alonswartz committed Jan 6, 2025
1 parent 0d96a57 commit a88b94f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
45 changes: 44 additions & 1 deletion filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,52 @@ import (
"strings"
)

func tokenizeFilterQuery(query string) []string {
tokens := []string{}
var currentToken string
var inQuotes bool
var quoteChar rune

for i := 0; i < len(query); i++ {
c := rune(query[i])

// If we're not in quotes and encounter a quote, we enter 'quote mode'
if !inQuotes && (c == '"' || c == '\'') {
inQuotes = true
quoteChar = c
continue
}

// If we are in quotes and encounter the same quoteChar, we exit 'quote mode'
if inQuotes && c == quoteChar {
inQuotes = false
continue
}

// If we're not in quotes and see a space, that's a token boundary
if !inQuotes && c == ' ' {
if currentToken != "" {
tokens = append(tokens, currentToken)
currentToken = ""
}
continue
}

// Otherwise, accumulate the character
currentToken += string(c)
}

// Append the last token if non-empty
if currentToken != "" {
tokens = append(tokens, currentToken)
}

return tokens
}

func evaluateFilterQuery(query string, input string) (bool, error) {
input = strings.ToLower(input)
tokens := strings.Fields(strings.ToLower(query))
tokens := tokenizeFilterQuery(strings.ToLower(query))
matches := true

for _, token := range tokens {
Expand Down
13 changes: 13 additions & 0 deletions tests/lines.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,16 @@ setup_file() {
[ "${#lines[@]}" -eq 1 ]
assert_line "64214a1d.md:5: richard feynman [quantum mechanics](64214930.md), the theory of quantum electrodynamics,"
}

@test "lines: filter quoted vs non-quoted" {
run notesium lines --filter='theory of quantum'
[ $status -eq 0 ]
[ "${#lines[@]}" -eq 2 ]
assert_line "64214a1d.md:5: [quantum mechanics](64214930.md), the theory of quantum electrodynamics,"
assert_line "64218088.md:7: to the development of the theory of [quantum mechanics](64214930.md)."

run notesium lines --filter='"theory of quantum"'
[ $status -eq 0 ]
[ "${#lines[@]}" -eq 1 ]
assert_line "64214a1d.md:5: [quantum mechanics](64214930.md), the theory of quantum electrodynamics,"
}

0 comments on commit a88b94f

Please sign in to comment.