match

package module
Version: v0.0.0-...-9983ea1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2015 License: MIT Imports: 1 Imported by: 0

README

match

Byte matching in Go

Under development....

speed

Match a 4 byte string in 1024 characters. There is 1 match.

BenchmarkMatch4String-8          5000000               292 ns/op        3495.75 MB/s

Match a 8 bytes in 32K byte slice. There is 1 match.

BenchmarkMatch8-8                 100000             11821 ns/op        2771.86 MB/s

Match a 8 bytes in 32K byte slice, as well as matching first 4 bytes. There is 1 match.

BenchmarkMatch8And4-8             100000             14423 ns/op        2271.88 MB/s

Determine if two 256 byte slice matches. All bytes match.

BenchmarkMatchLen256-8          30000000                46.7 ns/op      5485.93 MB/s

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var UseSse41 bool
View Source
var UseSse42 bool

Functions

func Match4

func Match4(needle, haystack []byte, indices []int) []int

Match4 will return start indeces of all matches of a 4 byte needle in a haystack that is a multiple of 16 in length. Indeces are returned ordered from index 0 and upwards.

Example
var haystack = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
}
found := Match4(haystack[8:12], haystack, nil)
fmt.Printf("%#v", found)
Output:

[]int{8, 24, 40, 56}

func Match4String

func Match4String(needle, haystack string, indices []int) []int

Match4String performs the same operation as Match4 on strings

func Match8

func Match8(needle, haystack []byte, indices []int) []int

Match8 will return start indeces of all matches of a 8 byte needle in a haystack that is a multiple of 16 in length. Indeces are returned ordered from index 0 and upwards.

Example
var haystack = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
}
found := Match8(haystack[8:16], haystack, nil)
fmt.Printf("%#v", found)
Output:

[]int{8, 24, 40, 56}

func Match8And4

func Match8And4(needle, haystack []byte, indices8 []int, indices4 []int) ([]int, []int)

Match4And8 will return start indeces of all matches of a 8 byte needle in a haystack that is a multiple of 16 in length. Matches for the first four bytes are returned in the first index, and 8 byte matches are returned in the second. An index that is an 8 byte match will not be present in the 4-byte matches. Indeces are returned ordered from index 0 and upwards.

Example
var haystack = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
}
// Match 12 and 8 bytes
f8, f4 := Match8And4(haystack[12:20], haystack, nil, nil)

fmt.Printf("Length 8 match: %#v\n", f8)
fmt.Printf("Length 4 match: %#v", f4)
Output:

Length 8 match: []int{12}
Length 4 match: []int{28}

func MatchLen

func MatchLen(a []byte, b []byte, max int) int
Example
var data = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
	100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115,
}
// Get match length
length := MatchLen(data[10:], data[26:], 16)

fmt.Printf("Number of matching bytes: %d, first mismatch %d/%d\n", length, data[10+length], data[26+length])
Output:

Number of matching bytes: 6, first mismatch 0/100

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL