func BenchmarkBoyerMoore5_100_100000(b *testing.B) {
	b.StopTimer()
	p := makeTestString4(100, 4, 0)
	t := makeTestString4(100000, 4, 1)
	bmd := core.BoyerMoorePreprocess(p)
	b.StartTimer()
	var matches []int
	for i := 0; i < b.N; i++ {
		matches = matches[0:0]
		core.BoyerMoore(bmd, t, &matches)
	}
}
func BenchmarkBoyerMooreReader5_100_100000(b *testing.B) {
	b.StopTimer()
	p := makeTestString4(100, 4, 0)
	t := makeTestString4(100000, 4, 1)
	bmd := core.BoyerMoorePreprocess(p)
	b.StartTimer()
	var matches []int
	buf := make([]byte, 5000)
	for i := 0; i < b.N; i++ {
		matches = matches[0:0]
		b.StopTimer()
		in := bytes.NewBuffer(t)
		b.StartTimer()
		core.BoyerMooreFromReader(bmd, in, buf, &matches)
	}
}
Beispiel #3
0
// Preprocesses p and returns a *StringFinder that can be used to quickly
// search for occurrences of p in other strings.  Uses Boyer-Moore, which
// requires O(n) time to preprocess p, and O(n) space to store the result.
// Methods on StringFinder can be called concurrently from multiple
// go-routines.
func Find(p []byte) *StringFinder {
	return &StringFinder{bmd: core.BoyerMoorePreprocess(p)}
}
func BoyerMooreReaderSpec(c gospec.Context) {
	c.Specify("Basic test", func() {
		var matches []int
		buffer := make([]byte, 500)
		p := []byte("a")
		t := []byte("aaaaaaaaa")
		bmd := core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaaaaaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("b")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("ba")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaabaaaaa")
		t = []byte("aaaabaaaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		p = []byte("aaaaaaaaaaaaaaaaabaaa")
		t = []byte("aaaaaabaaa")
		core.BoyerMoorePreprocess(p)
		bmd = core.BoyerMoorePreprocess(p)
		matches = matches[0:0]
		core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
	})

	c.Specify("Comprehensive test 2^17", func() {
		var matches []int
		buffer := make([]byte, 15)
		b := make([]byte, 17)
		for augment(b, 2) {
			p := b[0:5]
			t := b[5:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Comprehensive test 3^11", func() {
		var matches []int
		buffer := make([]byte, 15)
		b := make([]byte, 11)
		for augment(b, 3) {
			p := b[0:4]
			t := b[4:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Random test", func() {
		var matches []int
		buffer := make([]byte, 15)
		for i := 0; i < 10000; i += 2 {
			p := makeTestString4(15, 7, i)
			t := makeTestString4(1000, 7, i+1)
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMooreFromReader(bmd, bytes.NewBuffer(t), buffer, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})
}
func BoyerMooreSpec(c gospec.Context) {
	c.Specify("Basic test", func() {
		p := []byte("a")
		t := []byte("aaaaaaaaaa")
		var matches []int
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaaaaaaaaaaaaa")
		t = []byte("aaaaaaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("b")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("ba")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaabaaaaa")
		t = []byte("aaaabaaaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
		p = []byte("aaaaaaaaaaaaaaaaabaaa")
		t = []byte("aaaaaabaaa")
		core.BoyerMoore(core.BoyerMoorePreprocess(p), t, &matches)
		c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		matches = matches[0:0]
	})

	c.Specify("Comprehensive test 2^17", func() {
		b := make([]byte, 17)
		var matches []int
		for augment(b, 2) {
			p := b[0:5]
			t := b[5:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Comprehensive test 3^11", func() {
		b := make([]byte, 11)
		var matches []int
		for augment(b, 3) {
			p := b[0:4]
			t := b[4:]
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})

	c.Specify("Random test", func() {
		var matches []int
		for i := 0; i < 10000; i += 2 {
			p := makeTestString4(15, 7, i)
			t := makeTestString4(1000, 7, i+1)
			bmd := core.BoyerMoorePreprocess(p)
			matches = matches[0:0]
			core.BoyerMoore(bmd, t, &matches)
			c.Expect(matches, ContainsExactly, idiotStringSearch(p, t))
		}
	})
}