patterns

package
v1.6.4 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package patterns describes the Pattern interface. Standard patterns are also defined in this package: Sequence (as well as BMH and reverse BMH Sequence), Choice, List and Not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(id byte, l Loader)

Register a new Loader (provide an id higher than 16).

func Stringify

func Stringify(b []byte) string

Stringify returns a string version of a byte slice. If all bytes are UTF8, an ASCII string is returned Otherwise a hex string is returned.

Types

type AnyMask

type AnyMask byte

func (AnyMask) Equals

func (am AnyMask) Equals(pat Pattern) bool

func (AnyMask) Length

func (am AnyMask) Length() (int, int)

func (AnyMask) NumSequences

func (am AnyMask) NumSequences() int

func (AnyMask) Save

func (am AnyMask) Save(ls *persist.LoadSaver)

func (AnyMask) Sequences

func (am AnyMask) Sequences() []Sequence

func (AnyMask) String

func (am AnyMask) String() string

func (AnyMask) Test

func (am AnyMask) Test(b []byte) (bool, int)

func (AnyMask) TestR

func (am AnyMask) TestR(b []byte) (bool, int)

type BMHSequence

type BMHSequence struct {
	Seq   Sequence
	Shift [256]int
}

BMHSequence is an optimised version of the regular Sequence pattern. It is used behind the scenes in the Bytematcher package to speed up matching and should not be used directly in other packages (use the plain Sequence instead).

func NewBMHSequence

func NewBMHSequence(s Sequence) *BMHSequence

NewBMHSequence turns a Sequence into a BMHSequence.

func (*BMHSequence) Equals

func (s *BMHSequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (*BMHSequence) Length

func (s *BMHSequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (*BMHSequence) NumSequences

func (s *BMHSequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (*BMHSequence) Save

func (s *BMHSequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (*BMHSequence) Sequences

func (s *BMHSequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (*BMHSequence) String

func (s *BMHSequence) String() string

func (*BMHSequence) Test

func (s *BMHSequence) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (*BMHSequence) TestR

func (s *BMHSequence) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

type Choice

type Choice []Pattern

Choice is a slice of patterns, any of which can test true for the pattern to succeed. Returns the longest matching pattern

func (Choice) Equals

func (c Choice) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Choice) Length

func (c Choice) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Choice) NumSequences

func (c Choice) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Choice) Save

func (c Choice) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Choice) Sequences

func (c Choice) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Choice) String

func (c Choice) String() string

func (Choice) Test

func (c Choice) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (Choice) TestR

func (c Choice) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

type List

type List []Pattern

List is a slice of patterns, all of which must test true sequentially in order for the pattern to succeed.

func (List) Equals

func (l List) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (List) Length

func (l List) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (List) NumSequences

func (l List) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (List) Save

func (l List) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (List) Sequences

func (l List) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (List) String

func (l List) String() string

func (List) Test

func (l List) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (List) TestR

func (l List) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

type Loader

type Loader func(*persist.LoadSaver) Pattern

Loader loads a Pattern.

type Mask

type Mask byte

func (Mask) Equals

func (m Mask) Equals(pat Pattern) bool

func (Mask) Length

func (m Mask) Length() (int, int)

func (Mask) NumSequences

func (m Mask) NumSequences() int

func (Mask) Save

func (m Mask) Save(ls *persist.LoadSaver)

func (Mask) Sequences

func (m Mask) Sequences() []Sequence

func (Mask) String

func (m Mask) String() string

func (Mask) Test

func (m Mask) Test(b []byte) (bool, int)

func (Mask) TestR

func (m Mask) TestR(b []byte) (bool, int)

type Not

type Not struct{ Pattern }

Not contains a pattern and reports the opposite of that pattern's result when testing.

func (Not) Equals

func (n Not) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Not) Length

func (n Not) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Not) NumSequences

func (n Not) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Not) Save

func (n Not) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Not) Sequences

func (n Not) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Not) String

func (n Not) String() string

func (Not) Test

func (n Not) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (Not) TestR

func (n Not) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

type Pattern

type Pattern interface {
	Test([]byte) (bool, int)  // Returns boolean for match. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test. That offset should be 0 if the remaining byte slice is smaller than the pattern.
	TestR([]byte) (bool, int) // Same as Test but for testing in reverse (from the right-most position of the byte slice).
	Equals(Pattern) bool      // Test equality with another pattern
	Length() (int, int)       // Minimum and maximum lengths of the pattern
	NumSequences() int        // Number of simple sequences represented by a pattern. Return 0 if the pattern cannot be represented by a defined number of simple sequence (e.g. for an indirect offset pattern) or, if in your opinion, the number of sequences is unreasonably large.
	Sequences() []Sequence    // Convert the pattern to a slice of sequences. Return an empty slice if the pattern cannot be represented by a defined number of simple sequences.
	String() string
	Save(*persist.LoadSaver) // encode the pattern into bytes for saving in a persist file
}

Patterns are the smallest building blocks of a format signature. Exact byte sequence matches are a type of pattern, as are byte ranges, non-sequence matches etc. You can define custom patterns (e.g. for W3C date type) by implementing this interface.

func BMH

func BMH(p Pattern, rev bool) Pattern

BMH turns patterns into BMH sequences if possible.

func Load

func Load(ls *persist.LoadSaver) Pattern

Load loads the Pattern, choosing the correct Loader by the leading id byte.

type RBMHSequence

type RBMHSequence struct {
	Seq   Sequence
	Shift [256]int
}

RBMHSequence is a variant of the BMH sequence designed for reverse (R-L) matching. It is used behind the scenes in the Bytematcher package to speed up matching and should not be used directly in other packages (use the plain Sequence instead).

func NewRBMHSequence

func NewRBMHSequence(s Sequence) *RBMHSequence

NewRBMHSequence create a reverse matching BMH sequence (apply the BMH optimisation to TestR rather than Test).

func (*RBMHSequence) Equals

func (s *RBMHSequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (*RBMHSequence) Length

func (s *RBMHSequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (*RBMHSequence) NumSequences

func (s *RBMHSequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (*RBMHSequence) Save

func (s *RBMHSequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (*RBMHSequence) Sequences

func (s *RBMHSequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (*RBMHSequence) String

func (s *RBMHSequence) String() string

func (*RBMHSequence) Test

func (s *RBMHSequence) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (*RBMHSequence) TestR

func (s *RBMHSequence) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

type Sequence

type Sequence []byte

Sequence is a matching sequence of bytes.

func (Sequence) Equals

func (s Sequence) Equals(pat Pattern) bool

Equals reports whether a pattern is identical to another pattern.

func (Sequence) Length

func (s Sequence) Length() (int, int)

Length returns a minimum and maximum length for the pattern.

func (Sequence) NumSequences

func (s Sequence) NumSequences() int

NumSequences reports how many plain sequences are needed to represent this pattern.

func (Sequence) Reverse

func (s Sequence) Reverse() Sequence

The Reverse method is unique to this pattern. It is used for the EOF byte sequence set

func (Sequence) Save

func (s Sequence) Save(ls *persist.LoadSaver)

Save persists the pattern.

func (Sequence) Sequences

func (s Sequence) Sequences() []Sequence

Sequences converts the pattern into a slice of plain sequences.

func (Sequence) String

func (s Sequence) String() string

func (Sequence) Test

func (s Sequence) Test(b []byte) (bool, int)

Test bytes against the pattern. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

func (Sequence) TestR

func (s Sequence) TestR(b []byte) (bool, int)

Test bytes against the pattern in reverse. For a positive match, the integer value represents the length of the match. For a negative match, the integer represents an offset jump before a subsequent test.

Directories

Path Synopsis
Package tests exports shared patterns for use by the other bytematcher packages
Package tests exports shared patterns for use by the other bytematcher packages

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL