rubex

package
v0.0.0-...-586e974 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2019 License: BSD-2-Clause, MIT Imports: 10 Imported by: 0

README

Rubex : Super Fast Regexp for Go

by Zhigang Chen (zhigang.chen@moovweb.com or zhigangc@gmail.com)

A simple regular expression library that supports Ruby's regexp syntax. It implements all the public functions of Go's Regexp package, except LiteralPrefix. By the benchmark tests in Regexp, the library is 40% to 10X faster than Regexp on all but one test. Unlike Go's Regrexp, this library supports named capture groups and also allow "\1" and "\k" in replacement strings.

The library calls the Oniguruma regex library (6.9.2, the latest release as of now) for regex pattern searching. All replacement code is done in Go. This library can be easily adapted to support the regex syntax used by other programming languages or tools, like Java, Perl, grep, and emacs.

Installation

First, ensure you have Oniguruma installed. On OS X with brew, its as simple as

brew install oniguruma

Use oniguruma >= 6.9.2

Now that we've got Oniguruma installed, we can install Rubex!

go install github.com/limetext/rubex

Example Usage

import "rubex"

rxp := rubex.MustCompile("[a-z]*")
if err != nil {
    // whoops
}
result := rxp.FindString("a me my")
if result != "" {
    // FOUND A STRING!! YAY! Must be "a" in this instance
} else {
    // no good
}

TODO

Multi-thread problem for Regex?

Documentation

Index

Constants

View Source
const (
	ONIG_OPTION_DEFAULT = ONIG_OPTION_NONE
	/* options */
	ONIG_OPTION_NONE               = 0
	ONIG_OPTION_IGNORECASE         = 1
	ONIG_OPTION_EXTEND             = (ONIG_OPTION_IGNORECASE << 1)
	ONIG_OPTION_MULTILINE          = (ONIG_OPTION_EXTEND << 1)
	ONIG_OPTION_SINGLELINE         = (ONIG_OPTION_MULTILINE << 1)
	ONIG_OPTION_FIND_LONGEST       = (ONIG_OPTION_SINGLELINE << 1)
	ONIG_OPTION_FIND_NOT_EMPTY     = (ONIG_OPTION_FIND_LONGEST << 1)
	ONIG_OPTION_NEGATE_SINGLELINE  = (ONIG_OPTION_FIND_NOT_EMPTY << 1)
	ONIG_OPTION_DONT_CAPTURE_GROUP = (ONIG_OPTION_NEGATE_SINGLELINE << 1)
	ONIG_OPTION_CAPTURE_GROUP      = (ONIG_OPTION_DONT_CAPTURE_GROUP << 1)
	/* options (search time) */
	ONIG_OPTION_NOTBOL       = (ONIG_OPTION_CAPTURE_GROUP << 1)
	ONIG_OPTION_NOTEOL       = (ONIG_OPTION_NOTBOL << 1)
	ONIG_OPTION_POSIX_REGION = (ONIG_OPTION_NOTEOL << 1)
	ONIG_OPTION_MAXBIT       = ONIG_OPTION_POSIX_REGION /* limit */

	ONIG_NORMAL   = 0
	ONIG_MISMATCH = -1

	ONIG_MISMATCH_STR                = "mismatch"
	ONIGERR_UNDEFINED_NAME_REFERENCE = -217
)

Variables

This section is empty.

Functions

func MatchString

func MatchString(pattern string, s string) (matched bool, error error)

func QuoteMeta

func QuoteMeta(s string) string

QuoteMeta returns a string that quotes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text. For example, QuoteMeta(`[foo]`) returns `\[foo\]`.

Types

type MatchData

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

type NamedGroupInfo

type NamedGroupInfo map[string]int

type Regexp

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

func Compile

func Compile(str string) (*Regexp, error)

func CompileWithOption

func CompileWithOption(str string, option int) (*Regexp, error)

func MustCompile

func MustCompile(str string) *Regexp

func MustCompileWithOption

func MustCompileWithOption(str string, option int) *Regexp

func NewRegexp

func NewRegexp(pattern string, option int) (re *Regexp, err error)

func (*Regexp) ClearMatchData

func (re *Regexp) ClearMatchData()

func (*Regexp) Find

func (re *Regexp) Find(b []byte) []byte

func (*Regexp) FindAll

func (re *Regexp) FindAll(b []byte, n int) [][]byte

func (*Regexp) FindAllIndex

func (re *Regexp) FindAllIndex(b []byte, n int) [][]int

func (*Regexp) FindAllString

func (re *Regexp) FindAllString(s string, n int) []string

func (*Regexp) FindAllStringIndex

func (re *Regexp) FindAllStringIndex(s string, n int) [][]int

func (*Regexp) FindAllStringSubmatch

func (re *Regexp) FindAllStringSubmatch(s string, n int) [][]string

func (*Regexp) FindAllStringSubmatchIndex

func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int

func (*Regexp) FindAllSubmatch

func (re *Regexp) FindAllSubmatch(b []byte, n int) [][][]byte

func (*Regexp) FindAllSubmatchIndex

func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int

func (*Regexp) FindIndex

func (re *Regexp) FindIndex(b []byte) []int

func (*Regexp) FindReaderIndex

func (re *Regexp) FindReaderIndex(r io.RuneReader) []int

func (*Regexp) FindReaderSubmatchIndex

func (re *Regexp) FindReaderSubmatchIndex(r io.RuneReader) []int

func (*Regexp) FindString

func (re *Regexp) FindString(s string) string

func (*Regexp) FindStringIndex

func (re *Regexp) FindStringIndex(s string) []int

func (*Regexp) FindStringSubmatch

func (re *Regexp) FindStringSubmatch(s string) []string

func (*Regexp) FindStringSubmatchIndex

func (re *Regexp) FindStringSubmatchIndex(s string) []int

func (*Regexp) FindSubmatch

func (re *Regexp) FindSubmatch(b []byte) [][]byte

func (*Regexp) FindSubmatchIndex

func (re *Regexp) FindSubmatchIndex(b []byte) []int

func (*Regexp) Free

func (re *Regexp) Free()

func (*Regexp) Gsub

func (re *Regexp) Gsub(src, repl string) string

func (*Regexp) GsubFunc

func (re *Regexp) GsubFunc(src string, replFunc func(string, map[string]string) string) string

func (*Regexp) LiteralPrefix

func (re *Regexp) LiteralPrefix() (prefix string, complete bool)

func (*Regexp) Match

func (re *Regexp) Match(b []byte) bool

func (*Regexp) MatchReader

func (re *Regexp) MatchReader(r io.RuneReader) bool

func (*Regexp) MatchString

func (re *Regexp) MatchString(s string) bool

func (*Regexp) NumSubexp

func (re *Regexp) NumSubexp() int

func (*Regexp) ReplaceAll

func (re *Regexp) ReplaceAll(src, repl []byte) []byte

func (*Regexp) ReplaceAllFunc

func (re *Regexp) ReplaceAllFunc(src []byte, repl func([]byte) []byte) []byte

func (*Regexp) ReplaceAllString

func (re *Regexp) ReplaceAllString(src, repl string) string

func (*Regexp) ReplaceAllStringFunc

func (re *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string

func (*Regexp) String

func (re *Regexp) String() string

Jump to

Keyboard shortcuts

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