pattern

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2019 License: MIT Imports: 3 Imported by: 0

README

pattern

Build Status

This library is aimed to deal with simple patterns, i.e. patterns of certain length with some positions being wildcards

Samples of patterns

Pattern text Match example Will not match example
a..a abba abbba
..:..:..:..:..:.. 00:11:22:33:44:55 0:11:22:33:44:5
..\... 11.22 12345

Sample rules

  • As you probably noticed, . plays the role of . in regular expressions, i.e. wildcard character
  • There are escape sequences, those are available
    \n
    \r
    \t
    \\
    \`
    \.
    

Code usage sample

package main

import (
	"log"
	
	"github.com/sirkon/pattern"
)

func main() {
	p, err := pattern.NewPattern("a..a")
	if err != nil {
		log.Fatal(err)
	}
	
	if !p.Match([]byte("abba")) {
		log.Fatalf("pattern %s must match against abba", p)
	}
	log.Println(string(p.Lookup([]byte("  abba-cda")))) // will output "abba-cda"
}

Benchmarking (Core i5-4670k)

Test name Iterations Operations per nanosecond
BenchmarkPattern_Match-4 10000000 139 ns/op
BenchmarkRegexp_Match-4 500000 3235 ns/op
BenchmarkRagel_Match-4 20000000 112 ns/op
BenchmarkPattern_Lookup-4 30000 45475 ns/op
BenchmarkRegexp_Lookup-4 300 4562271 ns/op
BenchmarkRagel_Lookup-4 10000 122183ns/op

As you see, regular expression are much-much slower (there's a room for optimization I believe though). And pattern vs ragel comparsion is bit tricky: ragel is 1.25 times faster at regular match but loses at lookup. Lookup comparison was done with this sample set.

Spaces in the head Pattern
0 00:00:00:00:00:00
1 00:00:00:00:00:00
2 00:00:00:00:00:00
128 … 00:00:00:00:00:00

But Ragel is as fast as current implementation when limited to 16 character in the head and faster with shorter prefixes. Obviously, other patterns and source strings may lead to different results.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPattern

func CheckPattern(pattern string) bool

CheckPattern checks if pattern is correct

Types

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

Pattern represents a pattern.

func NewPattern

func NewPattern(pattern string) (*Pattern, error)

NewPattern pattern constructor

func (*Pattern) IsPrefixOf

func (p *Pattern) IsPrefixOf(source []byte) bool

IsPrefixOf checks if a pattern matches first symbols of source

func (*Pattern) Lookup

func (p *Pattern) Lookup(source []byte) []byte

Lookup returns a rest of source with a head matching against current pattern. Returns nil if no matches weren't found

func (*Pattern) Match

func (p *Pattern) Match(source []byte) bool

Match matches against a source

func (*Pattern) String

func (p *Pattern) String() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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