Help us shape the future of Go! Take the 2026 Go Developer Survey

scriptre

package
v1.149.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

View Source
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.

View Source
const Name = "re"

Name is the global the module is bound to.

Variables

View Source
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

func Validate

func Validate(pattern string) error

Validate reports why pattern would be refused when a run compiles it, or nil. The authoring gates call it on a pattern written as a literal, so a pattern RE2 cannot compile is refused at save rather than mid-run.

Types

type Match

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

Match is one match, as search, match, fullmatch and finditer return it.

func (*Match) Attr

func (m *Match) Attr(name string) (starlark.Value, error)

Attr returns the matched string or a method bound to this match.

func (*Match) AttrNames

func (*Match) AttrNames() []string

AttrNames lists the methods and the string field.

func (*Match) Freeze

func (*Match) Freeze()

Freeze has nothing mutable to freeze.

func (*Match) Hash

func (*Match) Hash() (uint32, error)

Hash refuses, as Python's does not define one either.

func (*Match) String

func (m *Match) String() string

String is Python's repr of a match.

func (*Match) Truth

func (*Match) Truth() starlark.Bool

Truth is always true: a failed match is None.

func (*Match) Type

func (*Match) Type() string

Type is the name a script reads from type().

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) Attr

func (p *Pattern) Attr(name string) (starlark.Value, error)

Attr returns a field, or the method bound to this pattern.

func (*Pattern) AttrNames

func (*Pattern) AttrNames() []string

AttrNames lists the methods and the three fields.

func (*Pattern) CompareSameType

func (p *Pattern) CompareSameType(op syntax.Token, y starlark.Value, _ int) (bool, error)

CompareSameType makes two patterns equal when their text and flags are, as Python's are, so a compiled pattern can key a dict.

func (*Pattern) Freeze

func (*Pattern) Freeze()

Freeze has nothing mutable to freeze.

func (*Pattern) Hash

func (p *Pattern) Hash() (uint32, error)

Hash is the pattern text's, so a compiled pattern can key a dict.

func (*Pattern) String

func (p *Pattern) String() string

String is Python's repr of a compiled pattern.

func (*Pattern) Truth

func (*Pattern) Truth() starlark.Bool

Truth is always true.

func (*Pattern) Type

func (*Pattern) Type() string

Type is the name a script reads from type().

Jump to

Keyboard shortcuts

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