Documentation
¶
Overview ¶
Package scriptre is the regular-expression matching a managed script is given, as a Starlark module (#2050).
It is separate from the engine for the reason internal/scriptdate is: every function is a pure transformation of its arguments, there is no caller and no state but a per-run cache of compiled patterns, and the engine's only use of it is to predeclare the module.
The engine is Go's regexp, which is RE2: matching is linear in the input, with no backtracking, so a pattern run over an untrusted page cannot spend a run's deadline on one match. The price is the two Python features RE2 does not have, backreferences in a pattern and lookaround; a pattern using either is refused when it is compiled, naming both.
The functions follow Python's re: their names, their arguments, and what they return, including findall's shape and split's captured groups. Offsets are byte offsets, which is what Starlark's own string indexing and slicing use, so s[m.start(1):m.end(1)] is m.group(1).
Index ¶
- Constants
- Variables
- func Validate(pattern string) error
- type Match
- type Pattern
- func (p *Pattern) Attr(name string) (starlark.Value, error)
- func (*Pattern) AttrNames() []string
- func (p *Pattern) CompareSameType(op syntax.Token, y starlark.Value, _ int) (bool, error)
- func (*Pattern) Freeze()
- func (p *Pattern) Hash() (uint32, error)
- func (p *Pattern) String() string
- func (*Pattern) Truth() starlark.Bool
- func (*Pattern) Type() string
Constants ¶
const BytesPerStep = 1024
BytesPerStep is how much input one interpreter step pays for. A match costs ceil(len(s)/BytesPerStep) steps on top of the call's own, so a script's step budget still bounds the text it scans, without a pattern over a 300 KB page costing more than the hand-written find() loop it replaces.
const Name = "re"
Name is the global the module is bound to.
Variables ¶
var Module = &starlarkstruct.Module{ Name: Name, Members: starlark.StringDict{ "compile": starlark.NewBuiltin(Name+".compile", compileFn), "search": patternCall("search"), "match": patternCall("match"), "fullmatch": patternCall("fullmatch"), "findall": patternCall("findall"), "finditer": patternCall("finditer"), "sub": patternCall("sub"), "split": patternCall("split"), "escape": starlark.NewBuiltin(Name+".escape", escapeFn), "I": starlark.MakeInt(flagIgnoreCase), "IGNORECASE": starlark.MakeInt(flagIgnoreCase), "M": starlark.MakeInt(flagMultiline), "MULTILINE": starlark.MakeInt(flagMultiline), "S": starlark.MakeInt(flagDotAll), "DOTALL": starlark.MakeInt(flagDotAll), }, }
Module is the re module the engine predeclares.
Functions ¶
Types ¶
type Match ¶
type Match struct {
// contains filtered or unexported fields
}
Match is one match, as search, match, fullmatch and finditer return it.
type Pattern ¶
type Pattern struct {
// contains filtered or unexported fields
}
Pattern is a compiled pattern, as re.compile returns it. Its methods are the module's functions without the pattern argument.
func (*Pattern) CompareSameType ¶
CompareSameType makes two patterns equal when their text and flags are, as Python's are, so a compiled pattern can key a dict.