package meg

import "github.com/multiformats/go-multiaddr/x/meg"

package meg implements Regular Expressions for multiaddr Components. It's short for "Megular Expressions"

Index

Variables

var Any int = matchAny

Any is a special code that matches any value.

Functions

func Match

func Match[L ListOfMatchable](matcher Matcher, components L) (bool, error)

Match returns whether the given Components match the Matcher

Errors are used to communicate capture errors. If the error is non-nil the returned bool will be false.

Components must be a ListOfMatchable to allow us to use a slice of []T as a []Matchable when *T implements Matchable.

Types

type CaptureFunc

type CaptureFunc func(Matchable) error

type ListOfMatchable

type ListOfMatchable interface {
	Get(i int) Matchable
	Len() int
}

ListOfMatchable is anything list-like that contains Matchable items. This allows us to convert a slice of []T as a []Matchable when *T implements Matchable. In the future, this may not be required if Go generics allows us to say S ~[]T, and *T implements Matchable. This may also not be required if we move this out of its own package and depend on Multiaddr and Components directly.

type MatchState

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

MatchState is the Thompson NFA for a regular expression.

func (MatchState) String

func (s MatchState) String() string

type Matchable

type Matchable interface {
	Code() int
	// Value() returns the string representation of the matchable.
	Value() string
	// RawValue() returns the byte representation of the Value
	RawValue() []byte
	// Bytes() returns the underlying bytes of the matchable. For multiaddr
	// Components, this includes the protocol code and possibly the varint
	// encoded size.
	Bytes() []byte
}

Matchable is an interface for any thing that can be matched against by this package. In the future, we may use multiaddr.Component types directly.

type Matcher

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

Matcher holds a graph of match state nodes. Use PatternToMatcher to create.

func PatternToMatcher

func PatternToMatcher(patterns ...Pattern) Matcher

func (Matcher) String

func (s Matcher) String() string

type Pattern

type Pattern = func(states []MatchState, nextIdx int) ([]MatchState, int)

Pattern is a curried MatchState. Given the slice of current MatchStates and a handle (int index) to the next MatchState, it returns a (possibly modified) slice of next MatchStates and handle to the inserted MatchState.

func CaptureBytes

func CaptureBytes(code int, val *[]byte) Pattern

func CaptureOneOrMoreBytes

func CaptureOneOrMoreBytes(code int, vals *[][]byte) Pattern

func CaptureOneOrMoreStrings

func CaptureOneOrMoreStrings(code int, vals *[]string) Pattern

func CaptureString

func CaptureString(code int, val *string) Pattern

func CaptureWithF

func CaptureWithF(code int, f CaptureFunc) Pattern

func CaptureZeroOrMoreBytes

func CaptureZeroOrMoreBytes(code int, vals *[][]byte) Pattern

func CaptureZeroOrMoreStrings

func CaptureZeroOrMoreStrings(code int, vals *[]string) Pattern

func CaptureZeroOrMoreWithF

func CaptureZeroOrMoreWithF(code int, f CaptureFunc) Pattern

func Cat

func Cat(patterns ...Pattern) Pattern

func OneOrMore

func OneOrMore(code int) Pattern

func Optional

func Optional(s Pattern) Pattern

func Or

func Or(p ...Pattern) Pattern

func Val

func Val(code int) Pattern

func ZeroOrMore

func ZeroOrMore(code int) Pattern

Source Files

meg.go sugar.go

Version
v0.16.0 (latest)
Published
Jun 6, 2025
Platform
linux/amd64
Imports
4 packages
Last checked
1 week ago

Tools for package owners.