sre2

package module
v0.0.0-...-385a11d Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2015 License: Apache-2.0 Imports: 6 Imported by: 0

README

sre2

Implementation of RE2, done natively in Go. Not related to the native implementation. Handles pathological cases with style and does not backtrack.

There are two available matchers: a fast matcher that does not attempt to track submatches, and a slower matcher that does. Internally, sre2 acts only on runes, not bytes; thus, the only missing part of syntax is \C (consume a single byte, even in UTF-8 mode).

The code provides a small library with small suite of tests. The package also includes a tiny main test binary, mostly useful for simple tests and for speed comparisons versus the standard regexp module.

This project was previously hosted on Google Code.

Usage

// MustParse will panic on compile failure; useful for init()
m := sre2.MustParse(re)
m, err := sre2.Parse(re)

// Simpler matcher just returns true/false
match := m.Match(str)

// Complex matcher returns indexes of found result: match n will be between (n*2,(n*2)+1).
// The 0th match is reserved for the complete found string. On failure, will return nil.
index := m.MatchIndex(str)

// After this example, fooidx will equal: {3, 12, 3, 6, 7, 12}
foo := sre2.MustParse(`(foo+|bar)\w(.*)`)
fooidx := m.MatchIndex("hi fooo test")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ESCAPES = map[rune]rune{
		'a': 7, 't': 9, 'n': 10, 'v': 11, 'f': 12, 'r': 13,
	}
)

Escape constants and their mapping to actual Unicode runes.

Functions

This section is empty.

Types

type Re

type Re interface {
	NumSubexps() int
	Match(s string) bool
	MatchIndex(s string) []int
	DebugOut()
}

Public interface to a compiled regexp.

func MustParse

func MustParse(src string) Re

Generates a NFA from the given source. If the regexp could not be parsed, panics with a string error.

func Parse

func Parse(src string) (re Re, err *string)

Generates a simple, straight-forward NFA. Matches an entire regexp from the given input string. If the regexp could not be parsed, returns a non-nil error string: the regexp will be nil in this case.

type RuneFilter

type RuneFilter func(r rune) bool

RuneFilter is a unique method signature for matching true/false over a given unicode rune.

type SafeReader

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

func NewSafeReader

func NewSafeReader(str string) SafeReader

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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