github.com/julianthome/gore@v0.0.0-20231109011145-b3a6bbe6fe55/command_name.go (about) 1 package gore 2 3 import "strings" 4 5 type commandName string 6 7 func (s commandName) String() string { 8 var b strings.Builder 9 for _, c := range s { 10 if c != '[' && c != ']' { 11 b.WriteRune(c) 12 } 13 } 14 return b.String() 15 } 16 17 func (s commandName) matches(t string) bool { 18 var grouping bool 19 for i, j := 0, 0; j <= len(t); i, j = i+1, j+1 { 20 if i >= len(s) { 21 return j == len(t) 22 } 23 if s[i] == '[' { 24 grouping = true 25 i++ 26 } 27 if j == len(t) { 28 break 29 } 30 if s[i] == ']' { 31 return false 32 } 33 if s[i] != t[j] { 34 return false 35 } 36 } 37 return grouping 38 } 39 40 func (s commandName) matchesPrefix(t string) bool { 41 for i, j := 0, 0; j < len(t); i, j = i+1, j+1 { 42 if i >= len(s) { 43 return false 44 } 45 if s[i] == '[' { 46 i++ 47 } 48 if s[i] == ']' { 49 return false 50 } 51 if s[i] != t[j] { 52 return false 53 } 54 } 55 return true 56 }