iohub / ahocorasick

A fast and memory efficient implementation of aho-corasick algorithm based on double-array trie (cedar), supports visualizing structure via graphviz.
GNU General Public License v2.0
116 stars 20 forks source link

Matching hangs forever when empty key is inserted #21

Closed bunyk closed 9 months ago

bunyk commented 1 year ago

I accidentally had empty string in my test data and was not able to figure out why tests hang. Maybe consider ingoring such keys when inserting?

func TestAhoCorasick(t *testing.T) {
    matcher := ahocorasick.NewMatcher()
    matcher.Insert([]byte("adidas"), true)
    matcher.Insert([]byte(""), true)
    matcher.Compile()
    fmt.Println("compiled matcher")

    seq := []byte("adidas sneakers")
    resp := matcher.Match(seq)
    for resp.HasNext() {
        items := resp.NextMatchItem(seq)
        for _, itr := range items {
            fmt.Println("Matched", string(matcher.Key(seq, itr)))
        }
    }
    resp.Release()
}
iohub commented 9 months ago

sure, I will fix it. thx