Documentation
¶
Overview ¶
Package rxp is an experiment in doing regexp-like things, without actually using regexp to do the work
Index ¶
- Constants
- Variables
- func IndexEnd(input []rune, index int) bool
- func IndexGet(input []rune, index int) (r rune, ok bool)
- func IndexInvalid(input []rune, index int) bool
- func IndexReady(input []rune, index int) bool
- func IndexValid(input []rune, index int) bool
- func ParseFlags(flags ...string) (Reps, Flags)
- func RuneIsALNUM(r rune) bool
- func RuneIsALPHA(r rune) bool
- func RuneIsASCII(r rune) bool
- func RuneIsBLANK(r rune) bool
- func RuneIsCNTRL(r rune) bool
- func RuneIsDIGIT(r rune) bool
- func RuneIsDashUnder(r rune) bool
- func RuneIsDashUnderALNUM(r rune) bool
- func RuneIsGRAPH(r rune) bool
- func RuneIsLOWER(r rune) bool
- func RuneIsPRINT(r rune) bool
- func RuneIsPUNCT(r rune) bool
- func RuneIsPlusMinus(r rune) bool
- func RuneIsSPACE(r rune) bool
- func RuneIsSpace(r rune) bool
- func RuneIsUPPER(r rune) bool
- func RuneIsWord(r rune) bool
- func RuneIsXDIGIT(r rune) bool
- type AsciiNames
- type Flags
- func (f Flags) AnyCase() bool
- func (f Flags) Capture() bool
- func (f Flags) Clone() Flags
- func (f Flags) DotNL() bool
- func (f Flags) Less() bool
- func (f Flags) Merge(other Flags) Flags
- func (f Flags) Multiline() bool
- func (f Flags) Negated() bool
- func (f Flags) OneOrMore() bool
- func (f Flags) SetCapture() Flags
- func (f Flags) SetNegated() Flags
- func (f Flags) String() string
- func (f Flags) ZeroOrMore() bool
- func (f Flags) ZeroOrOne() bool
- type Matcher
- func A(flags ...string) Matcher
- func Alnum(flags ...string) Matcher
- func Alpha(flags ...string) Matcher
- func Ascii(flags ...string) Matcher
- func B(flags ...string) Matcher
- func BackRef(gid int, flags ...string) Matcher
- func Blank(flags ...string) Matcher
- func Caret(flags ...string) Matcher
- func Cntrl(flags ...string) Matcher
- func D(flags ...string) Matcher
- func Digit(flags ...string) Matcher
- func Dollar(flags ...string) Matcher
- func Dot(flags ...string) Matcher
- func FieldKey(flags ...string) Matcher
- func FieldWord(flags ...string) Matcher
- func Graph(flags ...string) Matcher
- func Group(options ...interface{}) Matcher
- func IsUnicodeRange(table *unicode.RangeTable, flags ...string) Matcher
- func Keyword(flags ...string) Matcher
- func Lower(flags ...string) Matcher
- func MakeMatcher(match Matcher, flags ...string) Matcher
- func NamedClass(name AsciiNames, flags ...string) Matcher
- func Not(options ...interface{}) Matcher
- func Or(options ...interface{}) Matcher
- func Print(flags ...string) Matcher
- func Punct(flags ...string) Matcher
- func R(characters string, flags ...string) Matcher
- func S(flags ...string) Matcher
- func Space(flags ...string) Matcher
- func Text(text string, flags ...string) Matcher
- func Upper(flags ...string) Matcher
- func W(flags ...string) Matcher
- func Word(flags ...string) Matcher
- func WrapMatcher(matcher RuneMatcher, flags ...string) Matcher
- func Xdigit(flags ...string) Matcher
- func Z(flags ...string) Matcher
- type Matches
- type Pattern
- func (p Pattern) A(flags ...string) Pattern
- func (p Pattern) Add(matcher Matcher) Pattern
- func (p Pattern) Alnum(flags ...string) Pattern
- func (p Pattern) Alpha(flags ...string) Pattern
- func (p Pattern) Ascii(flags ...string) Pattern
- func (p Pattern) B(flags ...string) Pattern
- func (p Pattern) BackRef(idx int, flags ...string) Pattern
- func (p Pattern) Blank(flags ...string) Pattern
- func (p Pattern) Caret(flags ...string) Pattern
- func (p Pattern) Cntrl(flags ...string) Pattern
- func (p Pattern) D(flags ...string) Pattern
- func (p Pattern) Digit(flags ...string) Pattern
- func (p Pattern) Dollar(flags ...string) Pattern
- func (p Pattern) Dot(flags ...string) Pattern
- func (p Pattern) FindAllString(input string, count int) (found []string)
- func (p Pattern) FindAllStringIndex(input string, count int) (found [][]int)
- func (p Pattern) FindAllStringSubmatch(input string, count int) (found [][]string)
- func (p Pattern) FindAllStringSubmatchIndex(input string, count int) (found Matches)
- func (p Pattern) FindIndex(input string) (found []int)
- func (p Pattern) FindString(input string) string
- func (p Pattern) FindStringSubmatch(input string) (found []string)
- func (p Pattern) Graph(flags ...string) Pattern
- func (p Pattern) Group(options ...interface{}) Pattern
- func (p Pattern) Lower(flags ...string) Pattern
- func (p Pattern) Match(input []rune) (ok bool)
- func (p Pattern) MatchString(input string) (ok bool)
- func (p Pattern) NamedClass(name AsciiNames, flags ...string) Pattern
- func (p Pattern) Not(options ...interface{}) Pattern
- func (p Pattern) Or(options ...interface{}) Pattern
- func (p Pattern) Print(flags ...string) Pattern
- func (p Pattern) Punct(flags ...string) Pattern
- func (p Pattern) R(characters string, flags ...string) Pattern
- func (p Pattern) RangeTable(table *unicode.RangeTable, flags ...string) Pattern
- func (p Pattern) ReplaceAllString(input string, repl Replace) string
- func (p Pattern) ReplaceAllStringFunc(input string, repl Transform) string
- func (p Pattern) S(flags ...string) Pattern
- func (p Pattern) ScanStrings(input string) (segments Segments)
- func (p Pattern) Space(flags ...string) Pattern
- func (p Pattern) Text(text string, flags ...string) Pattern
- func (p Pattern) Upper(flags ...string) Pattern
- func (p Pattern) W(flags ...string) Pattern
- func (p Pattern) Word(flags ...string) Pattern
- func (p Pattern) Xdigit(flags ...string) Pattern
- func (p Pattern) Z(flags ...string) Pattern
- type Pipeline
- func (p Pipeline) Process(input string) (output string)
- func (p Pipeline) Replace(search interface{}, replace Replace) Pipeline
- func (p Pipeline) ReplaceText(search interface{}, text string) Pipeline
- func (p Pipeline) ReplaceWith(search interface{}, transform Transform) Pipeline
- func (p Pipeline) Transform(transform Transform) Pipeline
- type Replace
- type Replacement
- type Reps
- type RuneMatcher
- type Segment
- type Segments
- type Stage
- type SubMatch
- type SubMatches
- type Transform
Constants ¶
const ( DefaultMinReps = 1 DefaultMaxReps = 1 )
Variables ¶
var LookupAsciiClass = map[AsciiNames]func(r rune) bool{ ALNUM: RuneIsALNUM, ALPHA: RuneIsALPHA, ASCII: RuneIsASCII, BLANK: RuneIsBLANK, CNTRL: RuneIsCNTRL, DIGIT: RuneIsDIGIT, GRAPH: RuneIsGRAPH, LOWER: RuneIsLOWER, PRINT: RuneIsPRINT, PUNCT: RuneIsPUNCT, SPACE: RuneIsSpace, UPPER: RuneIsUPPER, WORD: RuneIsWord, XDIGIT: RuneIsXDIGIT, }
Functions ¶
func IndexEnd ¶ added in v0.4.0
IndexEnd returns true if this index is exactly the input length, denoting the Dollar zero-width position
func IndexInvalid ¶ added in v0.4.0
IndexInvalid returns true if this index is greater than or equal to the input length
func IndexReady ¶ added in v0.4.0
IndexReady returns true if this index is less than the input length
func IndexValid ¶ added in v0.4.0
IndexValid returns true if this index is less than or equal to the input length
func ParseFlags ¶
ParseFlags parses a regexp-like option string into a Flags instance and two integers, the low and high range of repetitions
| Flags | Description |
|---------|-----------------------------------------------------------------------------------------|
| ^ | Invert the meaning of this match group |
| m | Multiline mode Caret and Dollar match begin/end of line in addition to begin/end text |
| s | DotNL allows Dot to match newlines (\n) |
| i | AnyCase is case-insensitive matching of unicode text |
| c | Capture allows this Matcher to be included in Pattern substring results |
| * | zero or more repetitions, prefer more |
| + | one or more repetitions, prefer more |
| ? | zero or one repetition, prefer one |
| {l,h} | range of repetitions, l minimum and up to h maximum, prefer more |
| {l,} | range of repetitions, l minimum, prefer more |
| {l} | range of repetitions, l minimum, prefer more |
| *? | zero or more repetitions, prefer less |
| +? | one or more repetitions, prefer less |
| ?? | zero or one repetition, prefer zero |
| {l,h}? | range of repetitions, l minimum and up to h maximum, prefer less |
| {l,}? | range of repetitions, l minimum, prefer less |
| {l}? | range of repetitions, l minimum, prefer less |
The flags presented above can be combined into a single string argument, or can be individually given to ParseFlags
Any parsing errors will result in a runtime panic
func RuneIsALNUM ¶
RuneIsALNUM returns true for alphanumeric characters [a-zA-Z0-9]
func RuneIsALPHA ¶
RuneIsALPHA returns true for alphanumeric characters [a-zA-Z]
func RuneIsASCII ¶
RuneIsASCII returns true for valid ASCII characters [\x00-\x7F]
func RuneIsBLANK ¶
RuneIsBLANK returns true for tab and space characters [\t ]
func RuneIsCNTRL ¶
RuneIsCNTRL returns true for control characters [\x00-\x1F\x7F]
func RuneIsDashUnder ¶
func RuneIsDashUnderALNUM ¶
func RuneIsGRAPH ¶
RuneIsGRAPH returns true for graphical characters [a-zA-Z0-9!"$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~]
Note: upon the first use of RuneIsGRAPH, a lookup map is cached in a global variable and used for detecting the specific runes supported by the regexp [:graph:] class
func RuneIsLOWER ¶
RuneIsLOWER returns true for lowercase alphabetic characters [a-z]
func RuneIsPRINT ¶
RuneIsPRINT returns true for space and RuneIsGRAPH characters [ [:graph:]]
Note: uses RuneIsGRAPH
func RuneIsPUNCT ¶
RuneIsPUNCT returns true for punctuation characters [!-/:-@[-`{-~]
func RuneIsPlusMinus ¶
func RuneIsSPACE ¶
RuneIsSPACE returns true for empty space characters [\t\n\v\f\r ]
func RuneIsSpace ¶
RuneIsSpace returns true for space characters [\t\n\f\r ]
func RuneIsUPPER ¶
RuneIsUPPER returns true for lowercase alphabetic characters [A-Z]
func RuneIsWord ¶
RuneIsWord returns true for word characters [_a-zA-Z0-9]
func RuneIsXDIGIT ¶
RuneIsXDIGIT returns true for hexadecimal digits [z-fA-F0-9]
Types ¶
type AsciiNames ¶
type AsciiNames string
const ( ALNUM AsciiNames = "alnum" ALPHA AsciiNames = "alpha" ASCII AsciiNames = "ascii" BLANK AsciiNames = "blank" CNTRL AsciiNames = "cntrl" DIGIT AsciiNames = "digit" GRAPH AsciiNames = "graph" LOWER AsciiNames = "lower" PRINT AsciiNames = "print" PUNCT AsciiNames = "punct" SPACE AsciiNames = "space" UPPER AsciiNames = "upper" WORD AsciiNames = "word" XDIGIT AsciiNames = "xdigit" )
type Flags ¶ added in v0.2.0
type Flags uint16
func (Flags) SetCapture ¶ added in v0.2.0
func (Flags) SetNegated ¶ added in v0.2.0
func (Flags) ZeroOrMore ¶ added in v0.4.0
type Matcher ¶
type Matcher func(scope Flags, reps Reps, input []rune, index int, sm SubMatches) (consumed int, captured, negated, proceed bool)
Matcher is a single string matching function
| Argument | Description | |----------|------------------------------------| | scope | current Flags for this iteration | | reps | min and max repetition settings | | input | input rune slice (do not modify!) | | index | current input rune index to match | | Return | Description | |----------|------------------------------------| | consumed | number of runes matched from index | | captured | indicate this is a capture group | | negated | indicate this group is negated | | proceed | matched success, match for more |
func BackRef ¶ added in v0.8.0
BackRef is a Matcher equivalent to Perl backreferences where the gid argument is the match group to use
BackRef will panic if the gid argument is less than one
func FieldKey ¶
FieldKey creates a Matcher equivalent to:
(?:\b[a-zA-Z][-_a-zA-Z0-9]+?[a-zA-Z0-9]\b)
func FieldWord ¶
FieldWord creates a Matcher equivalent to:
(?:\b[a-zA-Z0-9]+?['a-zA-Z0-9]*[a-zA-Z0-9]+\b|\b[a-zA-Z0-9]+\b)
func Group ¶
func Group(options ...interface{}) Matcher
Group processes the list of Matcher instances, in the order they were given, and stops at the first one that does not match, discarding any consumed runes. If all Matcher calls succeed, all consumed runes are accepted together as this group (sub-sub-matches are not a thing)
func IsUnicodeRange ¶
func IsUnicodeRange(table *unicode.RangeTable, flags ...string) Matcher
IsUnicodeRange creates a Matcher equivalent to the regexp \pN where N is a unicode character class, passed to IsUnicodeRange as a unicode.RangeTable instance
For example, creating a Matcher for a single braille character:
IsUnicodeRange(unicode.Braille)
func Keyword ¶
Keyword is intended for Go-Enjin parsing of simple search keywords from user input and creates a Matcher equivalent to:
(?:\b[-+]?[a-zA-Z][-_a-zA-Z0-9]+?[a-zA-Z0-9]\b)
func MakeMatcher ¶ added in v0.4.0
MakeMatcher creates a rxp standard Matcher implementation wrapped around a given RuneMatcher
func NamedClass ¶ added in v0.8.0
func NamedClass(name AsciiNames, flags ...string) Matcher
NamedClass creates a Matcher equivalent to the regexp [:AsciiNames:], see the AsciiNames constants for the list of supported ASCII class names
NamedClass will panic if given an invalid class name
func Not ¶
func Not(options ...interface{}) Matcher
Not processes the given Matcher and inverts the next response without consuming any runes (zero-width Matcher)
Not accepts Pattern, Matcher and string types and will panic on all others
func Or ¶
func Or(options ...interface{}) Matcher
Or processes the list of Matcher instances, in the order they were given, and stops at the first one that returns a true next
Or accepts Pattern, Matcher and string types and will panic on all others
func R ¶ added in v0.8.0
R creates a Matcher equivalent to regexp character class ranges such as: [xyza-f] where x, y and z are individual runes to accept and a-f is the inclusive range of letters from lowercase a to lowercase f to accept
Note: do not include the [] brackets unless the intent is to actually accept those characters
func WrapMatcher ¶ added in v0.4.0
func WrapMatcher(matcher RuneMatcher, flags ...string) Matcher
WrapMatcher wraps a RuneMatcher with MakeMatcher with support for negations
type Matches ¶ added in v0.4.0
type Matches []SubMatches
Matches is the list of all SubMatches for each of the Pattern matches
type Pattern ¶
type Pattern []Matcher
Pattern is a list of Matcher functions, all of which must match, in the order present, in order to consider the Pattern to match
func ParseOptions ¶
ParseOptions accepts Pattern, Matcher and string options and recasts them into their specific types
ParseOptions will panic with any type other than Pattern, Matcher or string
func (Pattern) FindAllString ¶
func (Pattern) FindAllStringIndex ¶
func (Pattern) FindAllStringSubmatch ¶
func (Pattern) FindAllStringSubmatchIndex ¶ added in v0.4.0
func (Pattern) FindString ¶
func (Pattern) FindStringSubmatch ¶
func (Pattern) MatchString ¶
func (Pattern) NamedClass ¶ added in v0.8.0
func (p Pattern) NamedClass(name AsciiNames, flags ...string) Pattern
func (Pattern) RangeTable ¶ added in v0.8.0
func (p Pattern) RangeTable(table *unicode.RangeTable, flags ...string) Pattern
func (Pattern) ReplaceAllString ¶
func (Pattern) ReplaceAllStringFunc ¶
func (Pattern) ScanStrings ¶ added in v0.4.0
type Pipeline ¶
type Pipeline []Stage
Pipeline is a list of stages for transforming text in a single procedure
Pipeline is also a pseudo-buildable thing using the Transform, Replace, ReplaceText and ReplaceWith methods which return the updated Pipeline
func (Pipeline) Process ¶
Process returns the output of a complete Pipeline transformation of the input string, and is obviously not a buildable method as it returns a string instead of an updated Pipeline
func (Pipeline) ReplaceText ¶
func (Pipeline) ReplaceWith ¶
type Replace ¶
type Replace []Replacement
Replace is a Replacement pipeline
func (Replace) WithReplace ¶
func (r Replace) WithReplace(replacer Replacement) Replace
WithReplace replaces matches using a Replacement function
func (Replace) WithTransform ¶
WithTransform replaces matches with a Transform function
type Replacement ¶
Replacement is a Segment.String replacement function
type Reps ¶ added in v0.2.0
type Reps []int
func (Reps) IsNil ¶ added in v0.4.0
IsNil returns true if the length of the Reps int slice is not exactly two
func (Reps) Max ¶ added in v0.2.0
Max returns the maximum number of repetitions, or the DefaultMaxReps for nil instances
func (Reps) Min ¶ added in v0.2.0
Min returns the minimum number of repetitions, or the DefaultMinReps for nil instances
type RuneMatcher ¶
RuneMatcher is the signature for the basic character matching functions such as RuneIsWord
Implementations are expected to operate using the least amount of CPU instructions possible
type Segment ¶ added in v0.4.0
type Segment interface {
// Match returns true if this is a matched segment
Match() bool
// Runes returns the complete rune slice for this Pattern Segment
Runes() []rune
// String returns the complete string for this Pattern match
String() (complete string)
// Index returns the start and end input rune indexes
//
// Note: rune slice indexes, not byte or string indexes
Index() []int
// Submatch returns the specific submatch value, if it exists
Submatch(idx int) (value string, ok bool)
// contains filtered or unexported methods
}
type Stage ¶
type Stage struct {
// Search is a Pattern of text, used with Replace to modify the matching
// text
Search Pattern
Replace Replace
Transform Transform
}
Stage is one phase of a text replacement Pipeline and receives an input string from the previous stage (or the initial input text) and returns the output provided to the next stage (or is finally returned to the caller)
func (Stage) Process ¶
Process is the Pipeline processor method for a Stage
If there is a Search Pattern present, and there is at least one Replacement functions present, then the process returns a Search Pattern.ReplaceAllString
If there is a Search Pattern present, and there are no Replacement functions present, then the process returns a Search Pattern.ReplaceAllStringFunc
type SubMatch ¶ added in v0.4.0
type SubMatch []int
SubMatch is a single pair of start and end input rune indices
type SubMatches ¶ added in v0.4.0
type SubMatches []SubMatch
SubMatches is the list of SubMatch indices for a single matched Pattern
func (SubMatches) End ¶ added in v0.4.0
func (m SubMatches) End() int
func (SubMatches) Len ¶ added in v0.8.0
func (m SubMatches) Len() int
func (SubMatches) Start ¶ added in v0.4.0
func (m SubMatches) Start() int
func (SubMatches) Valid ¶ added in v0.8.0
func (m SubMatches) Valid() bool
Source Files
¶
- rxp-flags-reps.go
- rxp-flags.go
- rxp-internals.go
- rxp-matcher.go
- rxp-matchers-common.go
- rxp-matchers-compositors.go
- rxp-matchers-perl.go
- rxp-matchers-zero.go
- rxp-pattern-builder.go
- rxp-pattern-matches.go
- rxp-pattern-state.go
- rxp-pattern.go
- rxp-pipeline-stage.go
- rxp-pipeline.go
- rxp-replace.go
- rxp-rune-matchers-ascii-names.go
- rxp-rune-matchers-ascii.go
- rxp-rune-matchers-common.go
- rxp-rune-matchers-index.go
- rxp-rune-matchers-perl.go
- rxp-segment.go
- rxp-segments.go
- rxp-transform.go
- rxp.go