Documentation
¶
Overview ¶
Package rxp is an experiment in doing regexp-like things, without actually using regexp to do the work
Index ¶
- Variables
- 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
- type Fragment
- type Fragments
- type IMatchState
- type Match
- type MatchState
- type Matcher
- func A() Matcher
- func Alnum(flags ...string) Matcher
- func Alpha(flags ...string) Matcher
- func Ascii(flags ...string) Matcher
- func B() Matcher
- func Blank(flags ...string) Matcher
- func Caret(options ...string) Matcher
- func Class(name AsciiNames, flags ...string) Matcher
- func Cntrl(flags ...string) Matcher
- func D(flags ...string) Matcher
- func Digit(flags ...string) Matcher
- func Dollar(options ...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 MakeRuneMatcher(match RuneMatcher, flags ...string) Matcher
- func Not(options ...interface{}) Matcher
- func Or(options ...interface{}) Matcher
- func Print(flags ...string) Matcher
- func Punct(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 WrapFn(matcher RuneMatchFn, flags ...string) Matcher
- func Xdigit(flags ...string) Matcher
- func Z() Matcher
- type Pattern
- func (p Pattern) A() 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() Pattern
- func (p Pattern) Blank(flags ...string) Pattern
- func (p Pattern) Caret(flags ...string) Pattern
- func (p Pattern) Class(name AsciiNames, 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) 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) 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) Range(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) ScanString(input string) (fragments Fragments)
- 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() 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 RuneMatchFn
- type RuneMatcher
- type Stage
- type SyncPool
- type Transform
Constants ¶
This section is empty.
Variables ¶
var DefaultReps = Reps{1, 1}
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 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 Fragment ¶
type Fragment interface { // Match returns the associated Match for this Fragment, if this // Fragment is for matched text Match() (result Match, ok bool) // Index returns the start and end indexes of this Fragment Index() []int // Runes returns the input runes for this Index range Runes() []rune // String is a convenience wrapper around Runes String() string // Bytes is a convenience wrapper around Runes Bytes() []byte Recycle() // contains filtered or unexported methods }
Fragment is the Pattern.ScanString return value and can be either a Result of a captured Matcher or unmatched plain text. The Fragment slice returned by Pattern.ScanString can be used to rebuild the original input
type IMatchState ¶
type IMatchState interface { // Input returns the complete input rune slice Input() []rune // InputLen returns the total number of input runes (not bytes) InputLen() int // Index returns the start position for this Matcher, use Index + Len to // get the correct position within Input for This rune Index() int // Len is count of runes consumed so far by this Matcher Len() int // End returns true if this MatchState position (index+len) is exactly the // InputLen, denoting the Dollar zero-width position (End Of Input) End() bool // Ready returns true if this MatchState position (index+len) is less than // the InputLen (Ready is before End, End is Valid, past End is Invalid) Ready() bool // Valid returns true if this MatchState position (index+len) is less than // or equal to the InputLen (End is Valid, past the End is Invalid) Valid() bool // Invalid returns true if this MatchState position (index+len) is greater // than to the InputLen (End is Valid, past the End is Invalid) Invalid() bool // Has returns true if the given index is an input rune Has(idx int) (ok bool) // Get returns the input rune at the given index, if there is one Get(idx int) (r rune, ok bool) // Prev returns the previous rune, if there is one Prev() (r rune, ok bool) // This is the current rune being considered for this Matcher, if there is one This() (r rune, ok bool) // Next peeks at the next rune, if there is one Next() (r rune, ok bool) Reps() Reps // Captured returns true if Capture was previously called on this MatchState Captured() bool // Flags returns a clone Flags Flags() Flags Runes() []rune String() (text string) // Equal returns true if the other MatchState is the same as this one Equal(other MatchState) bool }
type Match ¶
type Match interface { // Runes returns teh complete text for this Pattern match as a rune slice Runes() []rune // String returns the complete text for this Pattern match String() (complete string) // Submatch returns the specific value of the capture group at the given // index, if it exists Submatch(idx int) (value string, ok bool) // contains filtered or unexported methods }
type MatchState ¶
type MatchState interface { IMatchState // Capture includes this Matcher in any sub-matching results Capture() // Consume associates the current rune with this match and moves the MatchState // forward by count runes, Keep returns true if there are still more runes // remaining to process Consume(count int) (ok bool) // Clone is a wrapper around a zero offset call to CloneWith Clone() MatchState // CloneWith returns an exact copy of this MatchState with the start point // offset by the positive amount given CloneWith(offset int, reps Reps) MatchState // Apply updates the other MatchState with this MatchState consumed runes, // capture state and Config Scope Apply(other MatchState) // Scope append (if the cfg is not nil) a new Config scope within this // matcher and always returns a composite Config of all scoped configs Scope(reps Reps, other Flags) (lh Reps, scope Flags) Recycle() // contains filtered or unexported methods }
MatchState is used by Matcher functions to progress the Pattern matching process
type Matcher ¶
type Matcher func(m MatchState) (next, keep bool)
Matcher is a single string matching function
next indicates that this Matcher function does not match the current rune and to progress the Pattern to the next Matcher instance keep indicates that this Matcher function was in fact satisfied, even though stop may also be true
func Class ¶
func Class(name AsciiNames, flags ...string) Matcher
Class creates a Matcher equivalent to the regexp [:AsciiNames:], see the AsciiNames constants for the list of supported ASCII class names
Class will panic if given an invalid class name
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 applied
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 MakeRuneMatcher ¶
func MakeRuneMatcher(match RuneMatcher, flags ...string) Matcher
MakeRuneMatcher creates a rxp standard Matcher implementation wrapped around a given RuneMatcher
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 WrapFn ¶
func WrapFn(matcher RuneMatchFn, flags ...string) Matcher
WrapFn wraps a RuneMatchFn with MakeRuneMatcher
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) FindString ¶
func (Pattern) FindStringSubmatch ¶
func (Pattern) MatchString ¶
func (Pattern) ReplaceAllString ¶
func (Pattern) ReplaceAllStringFunc ¶
func (Pattern) ScanString ¶
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 Match.String replacement function
type RuneMatchFn ¶
RuneMatchFn 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 RuneMatcher ¶
RuneMatcher is the signature for the MakeRuneMatcher matching function
The MatchState provided to RuneMatcher functions is a MatchState.Clone and any changes to that MatchState by the RuneMatcher are discarded. In order to have the RuneMatcher consume any runes, return the consumed number of runes and a true proceed
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
Source Files
¶
- rxp-buffers.go
- rxp-flags-reps.go
- rxp-flags.go
- rxp-fragment.go
- rxp-fragments.go
- rxp-match-state.go
- rxp-match-states.go
- rxp-match.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-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-perl.go
- rxp-transform.go
- rxp.go