word_matcher.go 598 B

12345678910111213141516171819202122232425262728293031
  1. package clickhouse
  2. import (
  3. "strings"
  4. "unicode"
  5. )
  6. // wordMatcher is a simple automata to match a single word (case insensitive)
  7. type wordMatcher struct {
  8. word []rune
  9. position uint8
  10. }
  11. // newMatcher returns matcher for word needle
  12. func newMatcher(needle string) *wordMatcher {
  13. return &wordMatcher{word: []rune(strings.ToUpper(needle)),
  14. position: 0}
  15. }
  16. func (m *wordMatcher) matchRune(r rune) bool {
  17. if m.word[m.position] == unicode.ToUpper(r) {
  18. if m.position == uint8(len(m.word)-1) {
  19. m.position = 0
  20. return true
  21. }
  22. m.position++
  23. } else {
  24. m.position = 0
  25. }
  26. return false
  27. }