substring

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2015 License: MIT Imports: 4 Imported by: 0

README

substring Build Status GoDoc GitHub release

Simple and composable alternative to regexp package for fast substring searches.

Installation

The recommended way to install substring

go get -t gopkg.in/toqueteos/substring.v1

The -t flag is for fetching gocheck, required for tests and benchmarks.

Examples

A basic example with two matchers:

package main

import (
    "fmt"
    "regexp"

    "gopkg.in/toqueteos/substring.v1"
)

func main() {
    m1 := substring.After("assets/", substring.Or(
        substring.Has("jquery"),
        substring.Has("angular"),
        substring.Suffixes(".js", ".css", ".html"),
    ))
    fmt.Println(m1.Match("assets/angular/foo/bar")) //Prints: true
    fmt.Println(m1.Match("assets/js/file.js"))      //Prints: true
    fmt.Println(m1.Match("assets/style/bar.css"))   //Prints: true
    fmt.Println(m1.Match("assets/foo/bar.html"))    //Prints: false
    fmt.Println(m1.Match("assets/js/qux.json"))     //Prints: false
    fmt.Println(m1.Match("core/file.html"))         //Prints: false
    fmt.Println(m1.Match("foobar/that.jsx"))        //Prints: false

    m2 := substring.After("vendor/", substring.Suffixes(".css", ".js", ".less"))

    fmt.Println(m2.Match("foo/vendor/bar/qux.css")) //Prints: true
    fmt.Println(m2.Match("foo/var/qux.less"))       //Prints: false

    re := regexp.MustCompile(`vendor\/.*\.(css|js|less)$`)
    fmt.Println(re.MatchString("foo/vendor/bar/qux.css")) //Prints: true
    fmt.Println(re.MatchString("foo/var/qux.less"))       //Prints: false
}

How fast?

It may vary depending on your use case but 1~2 orders of magnitude faster than regexp is pretty common.

Test it out for yourself by running go test -check.b!

$ go test -check.b
PASS: lib_test.go:18: LibSuite.BenchmarkExample1        10000000               221 ns/op
PASS: lib_test.go:23: LibSuite.BenchmarkExample2        10000000               229 ns/op
PASS: lib_test.go:28: LibSuite.BenchmarkExample3        10000000               216 ns/op
PASS: lib_test.go:33: LibSuite.BenchmarkExample4        10000000               208 ns/op
PASS: lib_test.go:38: LibSuite.BenchmarkExample5        20000000                82.1 ns/op
PASS: lib_test.go:48: LibSuite.BenchmarkExampleRe1        500000              4136 ns/op
PASS: lib_test.go:53: LibSuite.BenchmarkExampleRe2        500000              5222 ns/op
PASS: lib_test.go:58: LibSuite.BenchmarkExampleRe3        500000              5116 ns/op
PASS: lib_test.go:63: LibSuite.BenchmarkExampleRe4        500000              4020 ns/op
PASS: lib_test.go:68: LibSuite.BenchmarkExampleRe5      10000000               226 ns/op
OK: 10 passed
PASS
ok      gopkg.in/toqueteos/substring.v1 23.471s

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(first string, m StringsMatcher) *afterString

func And

func And(m ...StringsMatcher) *andString

func Any

func Any(pat string) *anyString

func BytesAfter

func BytesAfter(first string, m BytesMatcher) *afterBytes

func BytesAnd

func BytesAnd(m ...BytesMatcher) *andBytes

func BytesAny

func BytesAny(pat string) *anyBytes

func BytesExact

func BytesExact(pat string) *exactBytes

func BytesHas

func BytesHas(pat string) *hasBytes

func BytesOr

func BytesOr(m ...BytesMatcher) *orBytes

func BytesPrefix

func BytesPrefix(pat string) *prefixBytes

func BytesPrefixes

func BytesPrefixes(pats ...string) *prefixesBytes

func BytesRegexp

func BytesRegexp(pat string) *regexpBytes

func BytesSuffix

func BytesSuffix(pat string) *suffixBytes

func BytesSuffixGroup

func BytesSuffixGroup(s string, m ...BytesMatcher) *suffixGroupBytes

func BytesSuffixes

func BytesSuffixes(pats ...string) *suffixesBytes

func Exact

func Exact(pat string) *exactString

func Has

func Has(pat string) *hasString

func Or

func Or(m ...StringsMatcher) *orString

func Prefix

func Prefix(pat string) *prefixString

func Prefixes

func Prefixes(pats ...string) *prefixesString

func Regexp

func Regexp(pat string) *regexpString

func Suffix

func Suffix(pat string) *suffixString

func SuffixGroup

func SuffixGroup(s string, m ...StringsMatcher) *suffixGroupString

func Suffixes

func Suffixes(pats ...string) *suffixesString

Types

type BytesMatcher

type BytesMatcher interface {
	Match(b []byte) bool
	MatchIndex(b []byte) int
}

type StringsMatcher

type StringsMatcher interface {
	Match(s string) bool
	MatchIndex(s string) int
}

Jump to

Keyboard shortcuts

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