Example #1
0
func AhoCorasickReaderSpec(c gospec.Context) {
	c.Specify("Basic test", func() {
		strs := [][]byte{
			[]byte("baa"),
			[]byte("anba"),
			[]byte("banana"),
		}
		acd := core.AhoCorasickPreprocess(strs)
		str := "baababanananba"
		res := core.AhoCorasickFromReader(acd, bytes.NewBuffer([]byte(str)), 2)
		c.Expect(res[0], ContainsExactly, []int{0})
		c.Expect(res[1], ContainsExactly, []int{10})
		c.Expect(res[2], ContainsExactly, []int{5})
	})

	c.Specify("Substrings test", func() {
		strs := [][]byte{
			[]byte("baa"),
			[]byte("aab"),
			[]byte("aaa"),
			[]byte("aa"),
		}
		acd := core.AhoCorasickPreprocess(strs)
		str := "abbaababbbbaaaaaabbbbaabaabaabbb"
		res := core.AhoCorasickFromReader(acd, bytes.NewBuffer([]byte(str)), 2)
		c.Expect(res[0], ContainsExactly, []int{2, 10, 20, 23, 26})
		c.Expect(res[1], ContainsExactly, []int{3, 15, 21, 24, 27})
		c.Expect(res[2], ContainsExactly, []int{11, 12, 13, 14})
		c.Expect(res[3], ContainsExactly, []int{3, 11, 12, 13, 14, 15, 21, 24, 27})
	})

	c.Specify("Comprehensive 12 x 2^3 x 2^9 test", func() {
		for buf_size := 1; buf_size <= 12; buf_size++ {
			var ps [][]byte
			b := make([]byte, 3)
			for augment(b, 2) {
				p := make([]byte, 3)
				copy(p, b)
				ps = append(ps, p)
			}
			t := make([]byte, 9)
			for augment(t, 2) {
				acd := core.AhoCorasickPreprocess(ps)
				acres := core.AhoCorasickFromReader(acd, bytes.NewBuffer(t), buf_size)
				ires := idiotAhoCorasick(ps, t)
				for i := range ps {
					c.Expect(acres[i], ContainsExactly, ires[i])
				}
			}
		}
	})
}
Example #2
0
// Like In(), but searches the data from a Reader instead of a []byte.
func (sf *StringSetFinder) InReader(input io.Reader) map[int][]int {
	return core.AhoCorasickFromReader(sf.acd, input, 100000)
}