wildcard

package module
Version: v1.1.2 Latest Latest
Warning

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

Go to latest
Published: May 20, 2020 License: MIT Imports: 1 Imported by: 0

README

wildcard

GoDoc Maintainability Test Coverage Go Report Card

Package wildcard provides utils for working with a classic wildcard matching (only ? and * are supported).

Match function is a golang's adaptation of Java's O(1) space solution and ~ 3.37 times faster (76.0ns/22.5ns) then standard filepath.Match.

Example

package main

import (
  "fmt"

  "github.com/iamolegga/wildcard"
)

func main() {
  fmt.Println(wildcard.Match("value", "v?l*"))
  fmt.Println(wildcard.IsPattern("v?l*"))
}

Performance

It was compared with filepath.Match function with several runs:

$ for i in {1..10}; do go test -run=NONE -bench=. ./... >> old.txt; done
$ # change implementation to `filepath.Match`
$ for i in {1..10}; do go test -run=NONE -bench=. ./... >> new.txt; done
$
$ benchstat old.txt new.txt
name     old time/op  new time/op  delta
Match-4  22.5ns ± 2%  76.0ns ±19%  +238.01%  (p=0.000 n=10+10)

old - current implementation

new - utilize filepath.Match

Documentation

Overview

Package `wildcard` provides utils for working with a classic wildcard matching (only `?` and `*` are supported).

`Match` function is a golang's adaptation of Java's O(1) space solution:

https://www.programcreek.com/2014/06/leetcode-wildcard-matching-java/

and ~ 3.37 times faster then standard `filepath.Match`.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsPattern added in v1.1.0

func IsPattern(value string) bool

IsPattern - check if provided value is a wildcard pattern, i.e. it has special characters "*" or "?"

Example
package main

import (
	"fmt"

	"github.com/iamolegga/wildcard"
)

func main() {
	fmt.Println(wildcard.IsPattern("f*"))
	fmt.Println(wildcard.IsPattern("b?r"))
	fmt.Println(wildcard.IsPattern("baz"))
}
Output:

true
true
false

func Match

func Match(value, pattern string) bool

Match - check if provided value match wildcard pattern

Example
package main

import (
	"fmt"

	"github.com/iamolegga/wildcard"
)

func main() {
	fmt.Println(wildcard.Match("foo", "f*"))
	fmt.Println(wildcard.Match("bar", "b?r"))
	fmt.Println(wildcard.Match("baz", "az"))
}
Output:

true
true
false

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL