glob

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package glob implements a glob/capture/replace library based on github.com/pachyderm/ohmyglob

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasGlobRunes

func HasGlobRunes(s string) bool

HasGlobRunes scans a string for runes that indicate it's a glob pattern.

func HasTemplateRunes

func HasTemplateRunes(s string) bool

HasTemplateRunes scans a string for runes that indicate it's a template.

func IsGlobRune

func IsGlobRune(r rune) bool

IsGlobRune checks if a rune is a special glyph on the Glob syntax

func IsTemplateRune

func IsTemplateRune(r rune) bool

IsTemplateRune checks if a rune is a special glyph on the Template syntax

Types

type Glob

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

Glob represents a compiled glob pattern

func Compile

func Compile(pattern string, separators ...rune) (*Glob, error)

Compile creates a Glob for the given pattern and separators.

The pattern syntax is:

pattern:
    { term }

term:
    `*`         matches any sequence of non-separator characters
    `**`        matches any sequence of characters
    `?`         matches any single non-separator character
    `[` [ `!` ] { character-range } `]`
                character class (must be non-empty)
    `{` pattern-list `}`
                pattern alternatives
    c           matches character c (c != `*`, `**`, `?`, `\`, `[`, `{`, `}`)
    `\` c       matches character c

character-range:
    c           matches character c (c != `\\`, `-`, `]`)
    `\` c       matches character c
    lo `-` hi   matches character c for lo <= c <= hi

pattern-list:
    pattern { `,` pattern }
                comma-separated (without spaces) patterns

captures:
    `(` { `|` pattern } `)`
    `@(` { `|` pattern } `)`
                match and capture one of pipe-separated sub-patterns
    `*(` { `|` pattern } `)`
                match and capture any number of the pipe-separated sub-patterns
    `+(` { `|` pattern } `)`
                match and capture one or more of the pipe-separated sub-patterns
    `?(` { `|` pattern } `)`
                match and capture zero or one of the pipe-separated sub-patterns
    `!(` { `|` pattern } `)`
                match and capture anything except one of the pipe-separated sub-patterns

func MustCompile

func MustCompile(pattern string, separators ...rune) *Glob

MustCompile does the same as Compile but panics if there is an error.

func (*Glob) Capture

func (g *Glob) Capture(fixture string) ([]string, bool)

Capture returns the list of sub-expressions captured by the pattern against the given fixture, and also indicates if there was a match at all.

func (*Glob) Match

func (g *Glob) Match(fixture string) bool

Match tells if a given fixture matches the pattern.

func (*Glob) Replace

func (g *Glob) Replace(fixture, template string) (string, bool, error)

Replace performs the necessary match&capture on a given fixture, and applies the captured strings to a template.

func (*Glob) ReplaceCompiled

func (g *Glob) ReplaceCompiled(fixture string, template *Template) (string, bool, error)

ReplaceCompiled performs the necessary match&capture on a given fixture, and applies the captured strings to a pre-compiled template.

type Template

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

Template is the compiled representation of a replace pattern

func CompileTemplate

func CompileTemplate(template string) (*Template, error)

CompileTemplate creates a Template for the given template

Syntax is very simple. `${n}` and `$n` to indicate position in the captures slice, and `$$` to escape a literal `$`.

Escaping `$` isn't required when followed by anything other than a number or a `{`.

Captures are counted starting with 1.

func (Template) Equal

func (p Template) Equal(q *Template) bool

Equal tells if two instances have the same content

func (Template) Replace

func (p Template) Replace(data []string) (string, error)

Replace fills gaps in the template with elements of the given slice

func (Template) String

func (p Template) String() string

Jump to

Keyboard shortcuts

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