Here is an example of how to use a suffix array for automatic completion. ( playground ).
Note that I appended all lines along with a prefix \x00that cannot happen on the first lines.
package main
import (
"fmt"
"index/suffixarray"
"regexp"
"strings"
)
func main() {
words := []string{
"aardvark",
"happy",
"hello",
"hero",
"he",
"hotel",
}
joinedStrings := "\x00" + strings.Join(words, "\x00")
sa := suffixarray.New([]byte(joinedStrings))
match, err := regexp.Compile("\x00he[^\x00]*")
if err != nil {
panic(err)
}
ms := sa.FindAllIndex(match, -1)
for _, m := range ms {
start, end := m[0], m[1]
fmt.Printf("match = %q\n", joinedStrings[start+1:end])
}
}
Print
match = "hello"
match = "hero"
match = "he"
source
share