substring

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 4 Imported by: 0

README

substring Build Status GoDoc

Very fast one-time string searches in Go. Simple and composable.

Interop with regexp for backwards compatibility (easy migration from your current system to substring).

Installation

The recommended way to install substring is by using go get:

go get github.com/toqueteos/substring

Go Modules are supported!

Examples

A basic example with two matchers:

package main

import (
	"fmt"
	"regexp"

	"github.com/toqueteos/substring/v2"
)

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: true
	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
	fmt.Println()

	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
	fmt.Println()

	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 -bench .!

$ go test -bench .
pkg: github.com/toqueteos/substring
BenchmarkExample1-16            30759529                38.4 ns/op
BenchmarkExample2-16            26659675                40.0 ns/op
BenchmarkExample3-16            30760317                37.7 ns/op
BenchmarkExample4-16            31566652                36.8 ns/op
BenchmarkExample5-16           123704845                9.70 ns/op
BenchmarkExampleRe1-16           2739574                 436 ns/op
BenchmarkExampleRe2-16           2494791                 480 ns/op
BenchmarkExampleRe3-16           1681654                 713 ns/op
BenchmarkExampleRe4-16           2205490                 540 ns/op
BenchmarkExampleRe5-16          19673001                55.0 ns/op
PASS
ok      github.com/toqueteos/substring  15.016s

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