glob

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2019 License: BSD-2-Clause Imports: 4 Imported by: 1

README

GoDoc Go Report Card

glob - Fast and flexible globbing for Go

This module contains a globbing implementation that can be used with any rune as a wildcard character.

For documentation please read the godoc.

Documentation

Overview

Package glob contains an implementation for a globbing algorithm that uses a intermediate bytecode representation to allow efficient reuse of single pattern. The implementation supports any rune as a wildcard.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Default = PatternDescription{Wildcard: '*'}

Default is a PatternDescription with wildcard '*'

View Source
var SQLLike = PatternDescription{Wildcard: '%'}

SQLLike is a PatternDescription with wildcard '%'

Functions

func Glob

func Glob(s string, pattern string) bool

Glob is an auxiliary function that returns whether the given pattern as interpreted by the Default PatternDescription matches the given string.

For efficiency reasons, it is better to build a globber and reuse it than to call this function multiple times if the pattern doesn't change. Each call of this function recompiles the pattern to a new globber.

Example
package main

import (
	"bitbucket.org/ragnara/glob"
	"fmt"
)

func main() {
	testdata := []string{
		"cat",
		"dog",
		"bird",
		"fish",
		"lizard",
		"hedgehog",
		"fox",
	}

	for _, s := range testdata {
		if glob.Glob(s, "*i*") {
			fmt.Println(s, "contains an 'i'")
		}
	}

}
Output:

bird contains an 'i'
fish contains an 'i'
lizard contains an 'i'

Types

type Globber

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

Globber matches strings using a precompiled pattern.

func New

func New(pattern string, description PatternDescription) Globber

New creates a new globber by compiling a pattern according to the given PatternDescription.

func (Globber) Glob

func (g Globber) Glob(s string) bool

Glob returns whether the globbers pattern matches the given string.

Example
package main

import (
	"bitbucket.org/ragnara/glob"
	"fmt"
)

func main() {
	testdata := []string{
		"cat",
		"dog",
		"bird",
		"fish",
		"lizard",
		"hedgehog",
		"fox",
	}

	endingWithDGlobber := glob.New("*d", glob.Default)

	for _, s := range testdata {
		if endingWithDGlobber.Glob(s) {
			fmt.Println(s, "ends with a 'd'")
		}
	}

}
Output:

bird ends with a 'd'
lizard ends with a 'd'

type PatternDescription

type PatternDescription struct {
	//Wildcard is the rune that is supposed to match 0..n runes.
	Wildcard rune
}

PatternDescription describes the matching semantics of the pattern language.

Jump to

Keyboard shortcuts

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