Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ahocorasick.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (ac AhoCorasick) PatternCount() int {

// Iter gives an iterator over the built patterns
func (ac AhoCorasick) Iter(haystack string) Iter {
return ac.IterByte([]byte(haystack))
return ac.IterByte(unsafeBytes(haystack))
}

// IterByte gives an iterator over the built patterns
Expand All @@ -140,7 +140,7 @@ func (ac AhoCorasick) IterByte(haystack []byte) Iter {

// Iter gives an iterator over the built patterns with overlapping matches
func (ac AhoCorasick) IterOverlapping(haystack string) Iter {
return ac.IterOverlappingByte([]byte(haystack))
return ac.IterOverlappingByte(unsafeBytes(haystack))
}

// IterOverlappingByte gives an iterator over the built patterns with overlapping matches
Expand Down Expand Up @@ -293,7 +293,7 @@ func NewAhoCorasickBuilder(o Opts) AhoCorasickBuilder {
func (a *AhoCorasickBuilder) Build(patterns []string) AhoCorasick {
bytePatterns := make([][]byte, len(patterns))
for pati, pat := range patterns {
bytePatterns[pati] = []byte(pat)
bytePatterns[pati] = unsafeBytes(pat)
}

return a.BuildByte(bytePatterns)
Expand Down
12 changes: 12 additions & 0 deletions unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package aho_corasick

import (
"reflect"
"unsafe"
)

func unsafeBytes(s string) []byte {
str := (*reflect.StringHeader)(unsafe.Pointer(&s))
slice := reflect.SliceHeader{Data: str.Data, Len: str.Len, Cap: str.Len}
return *(*[]byte)(unsafe.Pointer(&slice))
}