ecmaregex

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

corvus-regex-ecma262 for Go

Regular expressions with the grammar and the answers of ECMA-262, the RegExp of JavaScript, for Go 1.25 and later.

  • Every flag: d, g, i, m, s, u, v and y, with the grammar of each. Without u or v a pattern is read by the Annex B grammar and matched by UTF-16 code unit, as ECMA-262 matches it; with them, by code point. The v flag has nested classes, the difference and the intersection of classes, strings in a class and the seven properties of strings.
  • ECMAScript 2025: lookbehind, named groups (and a name shared between alternatives), modifier groups such as (?i:...), and every Unicode property ECMA-262 lists, from tables of Unicode the module carries, so a pattern answers the same whatever Go builds it.
  • The grammar of ECMAScript 2020, which JSON Schema 2020-12 names, and a choice of Unicode version, each independent of the other: CompileOptions takes an Edition (EditionCurrent, the default, or EditionES2020) and a UnicodeVersion (UnicodeDefault for V8's version, which is 17 today, or Unicode13, Unicode17, Unicode18 or UnicodeLatest, which is 18 today). Both are settled when a pattern is compiled and cost a match nothing. The 2020 grammar refuses what later editions added: the d and v flags, modifier groups, a group name used twice, and property names outside its tables.
  • Text as UTF-8 (a string, a []byte, or a Text, which is checked once) or as UTF-16 ([]uint16, lone surrogates and all). Nothing is transcoded. Text that is not UTF-8 is refused, never read as U+FFFD. A match into a Captures allocates nothing once the working memory has grown.
  • The method set of the standard library's regexp (MatchString, FindStringIndex, FindAllStringIndex, FindStringSubmatchIndex, ReplaceAllString, Split and their []byte forms), with ECMA-262's answers: the global search moves on as matchAll does, and replace has every $ form of GetSubstitution.
  • The search layer of the crate: literals found by their rarest bytes, the windows of an alternation, the search after loops, the shapes answered without the matcher, and automata over ASCII for where a match starts and ends (docs/port-checklist.md). A global search through Matches into a Captures allocates nothing.
  • A pattern without a backreference is matched in time proportional to its size times the text. A pattern can be given a match timeout, as a Regex of .NET can, which bounds the time and the backtracking memory of every match; the Try methods return the error. With a timeout and the module's limits of pattern size, neither a pattern nor a text from outside can make a program hang. SetDefaultMatchTimeout gives every pattern one; CompileFlags and CompileOptions give a pattern its own, and return an error for a pattern that is not valid, so a validator needs neither the default nor a panic.
  • The matcher keeps its choices on a stack of its own; reading a deeply nested pattern recurses on the goroutine's stack, which Go grows as it needs to. The module needs 2 MB of it at most (tools/stackprobe), so a program that lowers the largest stack below that with debug.SetMaxStack can end on such a pattern, as it can on any deep recursion.

Compliance. Over the ECMA-262 test data (what V8 says about its patterns and texts, test262's patterns and exec calls, the global search, replace and split among them, and texts of bytes that are not UTF-8, which it refuses), the module gives every answer right and none wrong, refuses no valid pattern, accepts no invalid one and reaches no limit, over UTF-8 and over UTF-16. It does the same on the data as the data's own scripts write it for Unicode 13 and 18 and for the grammar of ECMAScript 2020. CI runs all of it on every change.

It is a port of the Rust crate corvus-regex-ecma262, and it follows the design of the .NET engine Corvus.Regex.Ecma262.

import ecmaregex "github.com/corvus-dotnet/corvus-regex-ecma262-go"

re := ecmaregex.MustCompile(`(?<year>\d{4})-(?<month>\d{2})`, "u")
re.MatchString("on 2026-10-25")                           // true
re.ReplaceAllString("2026-10-25", "$<month>/$<year>")     // "10/2026-25"

c := re.NewCaptures()
if re.CapturesString(c, "on 2026-10-25", 0) {
    month := c.Named("month")                             // 2
    _ = c.Start(month)                                    // 8
}

// ECMAScript 2020 as its edition was published, with the Unicode of its year, and a match timeout of its own.
greek, err := ecmaregex.CompileOptions(`^\p{Script=Greek}+$`, ecmaregex.Options{
    Flags:          ecmaregex.Flags{Unicode: true},
    MatchTimeout:   time.Second,
    Edition:        ecmaregex.EditionES2020,
    UnicodeVersion: ecmaregex.Unicode13,
})

Status

0.1.0 is the first release. The parser, the compiler, the matcher and the crate's search layer (where a match can start, and the automata, docs/port-checklist.md) are in, the whole test data passes, and the central benchmark site runs the harness each night.

Installing

go get github.com/corvus-dotnet/corvus-regex-ecma262-go

The module has no dependency.

Benchmarks

The central benchmark site runs this repository's harness image, ghcr.io/corvus-dotnet/corvus-regex-ecma262-go, each night beside the engines of every other source: this library over UTF-8 and UTF-16, Go's regexp, goja (a JavaScript engine written in Go) and V8, over the Industry workloads of dotnet/performance, with each ECMA-262 engine's compliance over the test data. This repository publishes no figures of its own. tools/benchmarks/run.sh <workloads.json> <texts> <out.json> runs the same harness anywhere (see the script for what it needs).

goja's compliance takes some minutes to work out, so it is worked out once (GOJA_COMPLIANCE=1, which runs tools/benchmarks/goja-dataset) and checked in as tools/benchmarks/compliance-goja.json, with the commit of the data it was made at. goja cannot be stopped in a match, so each file is judged in a child process whose watchdog ends it at an answer that passes 2 seconds; that answer is unanswered, and a new child goes on from the next. goja has no d flag, so the answers that need the positions of groups are wrong, and no v flag, so that grammar is left out, as its compliance declares.

Building and testing

go generate ./...          # after changing vm_template.go or regex_template.go
go test ./...
go run ./tools/dataset <ecma262-regex-test-data>/data wrong-cases.tsv

vm8_gen.go, vm16_gen.go, regex8_gen.go and regex16_gen.go are written from the templates by internal/expand, one for each encoding, and CI checks they are current. The Unicode tables of each version (internal/ucd/tables<major>_gen.go, strings<major>_gen.go) and the property names of ECMAScript 2020 (es2020_gen.go) are written from the Rust crate's by tools/unicode, and CI checks them against the crate.

Releasing

Every pull request merged into main without the no_release label releases the module with the next patch version (.github/workflows/auto-release.yml, tools/release): a v<version> tag on a commit of the merge with the version written into Version, which is what the Go module proxy serves. A pull request that needs a minor or major version sets nextVersion in .github/release.json.

License

Apache 2.0.

Documentation

Overview

Package ecmaregex is regular expressions with the grammar and the answers of ECMA-262, the RegExp of JavaScript.

re := ecmaregex.MustCompile(`(?<year>\d{4})-(?<month>\d{2})`, "u")
re.MatchString("on 2026-10-25")                    // true
re.ReplaceAllString("2026-10-25", "$<month>/$<year>") // "10/2026-25"

A pattern is read as new RegExp(pattern, flags) reads it: with the u or v flag by the grammar of the u flag and matched by code point; with neither by the Annex B grammar and matched by UTF-16 code unit, as ECMA-262 matches a pattern with no u flag. The i, m and s flags are the modifiers in force at the start of the pattern, which a modifier group of ECMAScript 2025 ((?i:...), (?-m:...)) changes for part of it.

Text is UTF-8 (a string) or UTF-16 ([]uint16, which may hold lone surrogates, with the methods ending in UTF16). Positions are in the units of the text: bytes of UTF-8, or UTF-16 code units. Nothing is transcoded. Text that is not UTF-8 is refused: a Try method returns an error, and any other panics.

A pattern has a match timeout, as a Regex of .NET's System.Text.RegularExpressions has: the time each match may take, which brings a cap on the memory of its backtracking with it. A pattern compiled with one should be matched with the Try methods, which return ErrTime or ErrMemory when a match reaches a limit; the others panic then. A pattern without a timeout never reaches a limit. SetDefaultMatchTimeout gives every pattern compiled after it one.

CompileOptions reads a pattern with the grammar of ECMAScript 2020 (EditionES2020), the grammar JSON Schema 2020-12 names, and with the tables of Unicode 13, 17 or 18 (UnicodeVersion), each chosen on its own. Both are read when the pattern is compiled, so a match costs the same whichever is chosen.

The engine is a port of the Rust crate corvus-regex-ecma262, file for file (parse.go is parse.rs, compile.go is compile.rs, and so on), held to the same ECMA-262 test data.

Index

Constants

View Source
const (
	// MaxPatternLength is the longest pattern that is read, in bytes of UTF-8.
	MaxPatternLength = 1 << 20
	// MaxNesting caps the depth of groups, which bounds the recursion of the parser and of the compilers.
	MaxNesting = 1000
	// MaxSize is the largest size a pattern may have, in the bytes the parser counts for it: nodeSize for each node of
	// its syntax tree, and rangeSize for each range of a class as its members are read, for each range a case
	// insensitive group or a negated property adds, for each alternative that encloses a named group and for each
	// group that shares its name. The tree, the program compiled from it and the time to make both are proportional
	// to that size, so it bounds them all: a pattern at the limit takes a few milliseconds and a few megabytes.
	MaxSize = 1 << 20
)

The limits on the size of one pattern. A pattern may come from outside, so reading and compiling one must take bounded time and memory whatever it holds. A pattern over a limit is refused with a *TooLargeError, which is not the error of a pattern that is not valid.

View Source
const MaxMemoWords = 1 << 20

MaxMemoWords is the largest memory of failed places, in words of 64 bits: 8 MB. A match that would need more goes without it.

View Source
const MaxStack = 1 << 20

MaxStack is the number of entries the backtracking stack of a match with a limit may hold. A match that needs more is abandoned. A pattern that keeps a choice for every character of its text needs one entry or a few for each, so this is room for a text of some hundred thousand characters, and a pattern that pushes without consuming, such as (?:a?){2147483647}, reaches it in a few milliseconds. It is looked at with the clock, so the stack can go past it by checkEvery entries.

View Source
const Version = "0.1.4"

Version is the version of the module. A release writes its tag's version here; in the repository it is 0.0.0.

Variables

View Source
var (
	// ErrTime says the match ran for longer than its limit.
	ErrTime = errors.New("ecmaregex: the match ran for longer than its limit")
	// ErrMemory says the match needed more backtracking memory than its limit: its stack reached MaxStack entries.
	ErrMemory = errors.New("ecmaregex: the match needed more backtracking memory than its limit")
)

ErrTime and ErrMemory say why a match was abandoned. Neither is an answer.

Functions

func DefaultMatchTimeout

func DefaultMatchTimeout() time.Duration

DefaultMatchTimeout returns the match timeout a pattern compiled now without one of its own takes, or 0 for none.

func IsValid

func IsValid(pattern, flags string) bool

IsValid reports whether a pattern is valid with the flags.

func IsValidOptions added in v0.1.1

func IsValidOptions(pattern string, opts Options) bool

IsValidOptions reports whether a pattern is valid with the flags of opts, in the grammar of its edition and under its version of Unicode, without compiling it. The match timeout is not read.

func SetDefaultMatchTimeout

func SetDefaultMatchTimeout(d time.Duration)

SetDefaultMatchTimeout gives every pattern compiled after it, without a timeout of its own, the match timeout d. Zero is no timeout.

Types

type Captures

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

Captures holds the start and end of a match and of each group of a pattern. A match into one allocates nothing, so a program that matches many times makes one with NewCaptures and passes it to each match. A Captures is not safe for concurrent use.

func (*Captures) End

func (c *Captures) End(g int) int

End returns where group g ends, or -1 when it did not take part.

func (*Captures) Found

func (c *Captures) Found() bool

Found reports whether the last match into the Captures found one.

func (*Captures) Index

func (c *Captures) Index() []int

Index returns the start and end of each group, -1 for a group that did not take part, as FindStringSubmatchIndex does, or nil when the last match found none. It is the Captures' own memory, which the next match overwrites.

func (*Captures) Len

func (c *Captures) Len() int

Len returns the number of groups, the whole match counted as group 0.

func (*Captures) Matched

func (c *Captures) Matched(g int) bool

Matched reports whether group g took part in the match.

func (*Captures) Named

func (c *Captures) Named(name string) int

Named returns the group with the name that took part in the match, the first of them where groups in separate alternatives share it, or -1.

func (*Captures) Start

func (c *Captures) Start(g int) int

Start returns where group g starts, or -1 when it did not take part.

type Edition added in v0.1.1

type Edition uint8

Edition is the edition of ECMA-262 whose grammar a pattern is read with.

const (
	// EditionCurrent is today's grammar, ECMAScript 2025: the default.
	EditionCurrent Edition = iota
	// EditionES2020 is the grammar of ECMAScript 2020 (ECMA-262, 11th edition), which JSON Schema 2020-12 names. It
	// refuses what later editions added, and nothing else: the d and v flags, modifier groups such as (?i:...), a group
	// name used by two groups, and a property name or value that is not in the edition's tables. A pattern it accepts
	// has the same answers as in today's grammar, under the same version of Unicode.
	EditionES2020
)

func (Edition) String added in v0.1.1

func (e Edition) String() string

String returns the name of the edition: "current" or "es2020".

type Error

type Error struct {
	Kind ErrorKind
	// TooLarge says which limit a pattern of kind ErrTooLarge is over.
	TooLarge *TooLargeError
	// contains filtered or unexported fields
}

Error is why a pattern was not compiled, or why a text was refused.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the *TooLargeError of a pattern over a limit of size.

type ErrorKind

type ErrorKind uint8

ErrorKind says why a pattern was not compiled.

const (
	// ErrInvalid says the pattern is not valid under the grammar its flags choose.
	ErrInvalid ErrorKind = iota
	// ErrFlags says the flags are not valid.
	ErrFlags
	// ErrTooLarge says the pattern is over a limit of size (see TooLargeError), which says nothing of whether it is
	// valid.
	ErrTooLarge
	// ErrEncoding says the pattern or a text is not UTF-8.
	ErrEncoding
)

type Flags

type Flags struct {
	HasIndices  bool // d
	Global      bool // g
	IgnoreCase  bool // i
	Multiline   bool // m
	DotAll      bool // s
	Unicode     bool // u
	UnicodeSets bool // v
	Sticky      bool // y
}

Flags are the flags of a pattern, as new RegExp(pattern, flags) reads them.

func ParseFlags

func ParseFlags(flags string) (Flags, error)

ParseFlags reads flags written as new RegExp reads them, such as "giu". A flag that is not one of dgimsuvy, a flag given twice, or u with v, is an error of kind ErrFlags.

func (Flags) String

func (f Flags) String() string

String returns the flags as RegExp writes them, in the order dgimsuvy.

type Limit

type Limit uint8

Limit says which limit of size a pattern is over.

const (
	// LimitLength is MaxPatternLength.
	LimitLength Limit = iota
	// LimitNesting is MaxNesting.
	LimitNesting
	// LimitSize is MaxSize.
	LimitSize
)

func (Limit) String

func (l Limit) String() string

String returns the name of the limit: "length", "nesting" or "size".

type Matches

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

Matches is a global search that can be run again over another text, so that counting or reading the matches of many texts allocates nothing once the working memory has grown. After an empty match the search moves on one character, as matchAll does. A Matches is not safe for concurrent use.

m := re.NewMatches()
c := re.NewCaptures()
for m.ResetString(s); m.Next(c); {
	fmt.Println(c.Start(0), c.End(0))
}

func (*Matches) Count

func (m *Matches) Count() int

Count is TryCount, which panics when a match reaches a limit or the text is not UTF-8.

func (*Matches) Next

func (m *Matches) Next(c *Captures) bool

Next is TryNext, which panics when a match reaches a limit or the text is not UTF-8.

func (*Matches) NextIndex

func (m *Matches) NextIndex() (int, int, bool)

NextIndex is TryNextIndex, which panics when a match reaches a limit or the text is not UTF-8.

func (*Matches) ResetString

func (m *Matches) ResetString(s string) *Matches

ResetString starts the search over a string, which is checked to be UTF-8 when the search starts. A string that is not UTF-8 makes TryNext return an error of kind ErrEncoding, and Next panic.

func (*Matches) ResetText

func (m *Matches) ResetText(t Text) *Matches

ResetText starts the search over a text.

func (*Matches) ResetUTF16

func (m *Matches) ResetUTF16(u []uint16) *Matches

ResetUTF16 starts the search over UTF-16 text.

func (*Matches) TryCount

func (m *Matches) TryCount() (int, error)

TryCount counts the matches left.

func (*Matches) TryNext

func (m *Matches) TryNext(c *Captures) (bool, error)

TryNext finds the next match into c, and reports whether there was one.

func (*Matches) TryNextIndex

func (m *Matches) TryNextIndex() (int, int, bool, error)

TryNextIndex finds the next match and returns where it starts and ends, with false when there is none. It asks for no group, so a pattern can take a faster way.

type Options added in v0.1.1

type Options struct {
	// Flags are the flags, as new RegExp(pattern, flags) takes them (see ParseFlags).
	Flags Flags
	// MatchTimeout is the time each match may take before it is abandoned, which brings a cap on the memory of its
	// backtracking with it: 0 for no limit. DefaultMatchTimeout is the timeout Compile gives.
	MatchTimeout time.Duration
	// Edition is the edition of ECMA-262 whose grammar the pattern is read with.
	Edition Edition
	// UnicodeVersion is the version of Unicode whose tables the pattern is read with.
	UnicodeVersion UnicodeVersion
}

Options are the settings of CompileOptions and IsValidOptions: the flags, and those beyond them, each independent of the others. The zero value is a pattern with no flags, no match timeout, today's grammar and V8's Unicode.

type Piece

type Piece struct {
	Text    string
	Defined bool
}

Piece is one item of a split: a piece of the text, or what a group that did not take part leaves, which ECMA-262 gives as undefined.

type Piece16

type Piece16 struct {
	Text    []uint16
	Defined bool
}

Piece16 is a Piece of UTF-16 text.

type Regexp

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

Regexp is a compiled pattern. It is safe for concurrent use.

func Compile

func Compile(pattern, flags string) (*Regexp, error)

Compile compiles a pattern with flags written as new RegExp reads them, such as "giu", and the default match timeout.

func CompileFlags

func CompileFlags(pattern string, flags Flags, matchTimeout time.Duration) (*Regexp, error)

CompileFlags compiles a pattern with the flags and the match timeout (0 for none).

func CompileOptions added in v0.1.1

func CompileOptions(pattern string, opts Options) (*Regexp, error)

CompileOptions compiles a pattern with the settings of opts: the flags, the match timeout, the edition of ECMA-262 whose grammar it is read with and the version of Unicode whose tables it is read with, each independent of the others. It returns an error, and never panics, for a pattern or flags that are not valid, so a validator can compile a pattern from outside with a match timeout of its own and no default.

// ECMAScript 2020 as its edition was published, with the Unicode of its year, as JSON Schema 2020-12 reads a
// pattern.
re, err := ecmaregex.CompileOptions(`^\p{Script=Greek}+$`, ecmaregex.Options{
	Flags:          ecmaregex.Flags{Unicode: true},
	MatchTimeout:   time.Second,
	Edition:        ecmaregex.EditionES2020,
	UnicodeVersion: ecmaregex.Unicode13,
})

Under EditionES2020 the d and v flags are an error of kind ErrFlags.

func MustCompile

func MustCompile(pattern, flags string) *Regexp

MustCompile is Compile, which panics when the pattern is not compiled.

func (*Regexp) CapturesString

func (re *Regexp) CapturesString(c *Captures, s string, start int) bool

CapturesString is TryCapturesString, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) CapturesText

func (re *Regexp) CapturesText(c *Captures, t Text, start int) bool

CapturesText is TryCapturesText, which panics when a match reaches a limit.

func (*Regexp) Edition added in v0.1.1

func (re *Regexp) Edition() Edition

Edition returns the edition of ECMA-262 whose grammar the pattern was read with.

func (*Regexp) FindAllIndex

func (re *Regexp) FindAllIndex(b []byte, n int) [][]int

FindAllIndex is TryFindAllIndex, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) FindAllStringIndex

func (re *Regexp) FindAllStringIndex(s string, n int) [][]int

FindAllStringIndex is TryFindAllStringIndex, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) FindAllStringSubmatchIndex

func (re *Regexp) FindAllStringSubmatchIndex(s string, n int) [][]int

FindAllStringSubmatchIndex is TryFindAllStringSubmatchIndex, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) FindAllSubmatchIndex

func (re *Regexp) FindAllSubmatchIndex(b []byte, n int) [][]int

FindAllSubmatchIndex is TryFindAllSubmatchIndex, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) FindIndex

func (re *Regexp) FindIndex(b []byte) []int

FindIndex is TryFindIndex, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) FindStringIndex

func (re *Regexp) FindStringIndex(s string) []int

FindStringIndex is TryFindStringIndex, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) FindStringSubmatchIndex

func (re *Regexp) FindStringSubmatchIndex(s string) []int

FindStringSubmatchIndex is TryFindStringSubmatchIndex, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) FindSubmatchIndex

func (re *Regexp) FindSubmatchIndex(b []byte) []int

FindSubmatchIndex is TryFindSubmatchIndex, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) Flags

func (re *Regexp) Flags() Flags

Flags returns the pattern's flags.

func (*Regexp) Match

func (re *Regexp) Match(b []byte) bool

Match is TryMatch, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) MatchString

func (re *Regexp) MatchString(s string) bool

MatchString is TryMatchString, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) MatchText

func (re *Regexp) MatchText(t Text) bool

MatchText is TryMatchText, which panics when a match reaches a limit.

func (*Regexp) MatchTimeout

func (re *Regexp) MatchTimeout() time.Duration

MatchTimeout returns the time each match may take, or 0 for no limit.

func (*Regexp) MatchUTF16

func (re *Regexp) MatchUTF16(u []uint16) bool

MatchUTF16 is TryMatchUTF16, which panics when a match reaches a limit.

func (*Regexp) NewCaptures

func (re *Regexp) NewCaptures() *Captures

NewCaptures returns a Captures for the pattern's matches.

func (*Regexp) NewMatches

func (re *Regexp) NewMatches() *Matches

NewMatches returns a global search of the pattern, over no text until it is reset.

func (*Regexp) NumSubexp

func (re *Regexp) NumSubexp() int

NumSubexp returns the number of capturing groups of the pattern.

func (*Regexp) Pattern

func (re *Regexp) Pattern() string

Pattern returns the pattern as it was compiled.

func (*Regexp) Replace

func (re *Regexp) Replace(src, template []byte) []byte

Replace is TryReplace, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) ReplaceAll

func (re *Regexp) ReplaceAll(src, template []byte) []byte

ReplaceAll is TryReplaceAll, which panics when a match reaches a limit or the bytes are not UTF-8.

func (*Regexp) ReplaceAllString

func (re *Regexp) ReplaceAllString(src, template string) string

ReplaceAllString is TryReplaceAllString, which panics when a match reaches a limit or a text is not UTF-8.

func (*Regexp) ReplaceString

func (re *Regexp) ReplaceString(src, template string) string

ReplaceString is TryReplaceString, which panics when a match reaches a limit or a text is not UTF-8.

func (*Regexp) Split

func (re *Regexp) Split(s string, limit int) []Piece

Split is TrySplit, which panics when a match reaches a limit or the text is not UTF-8.

func (*Regexp) String

func (re *Regexp) String() string

String returns the pattern as a RegExp literal writes it, /pattern/flags.

func (*Regexp) SubexpIndex

func (re *Regexp) SubexpIndex(name string) []int

SubexpIndex returns the groups with the name, which is more than one where groups in separate alternatives share it, or nil.

func (*Regexp) SubexpNames

func (re *Regexp) SubexpNames() []string

SubexpNames returns the name of each group, the whole match first (always ""). A group with no name has "". Two groups in separate alternatives can share a name.

func (*Regexp) TryCapturesString

func (re *Regexp) TryCapturesString(c *Captures, s string, start int) (bool, error)

TryCapturesString matches the text from the byte offset start (at it alone with the y flag) into c, and reports whether it found a match.

func (*Regexp) TryCapturesText

func (re *Regexp) TryCapturesText(c *Captures, t Text, start int) (bool, error)

TryCapturesText matches the text from the byte offset start into c, and reports whether it found a match.

func (*Regexp) TryCapturesUTF16

func (re *Regexp) TryCapturesUTF16(c *Captures, u []uint16, start int) (bool, error)

TryCapturesUTF16 matches the text from the code unit start (at it alone with the y flag) into c, and reports whether it found a match.

func (*Regexp) TryFindAllIndex

func (re *Regexp) TryFindAllIndex(b []byte, n int) ([][]int, error)

TryFindAllIndex returns the start and end of each match of a global search, at most n (all when n is below 0).

func (*Regexp) TryFindAllStringIndex

func (re *Regexp) TryFindAllStringIndex(s string, n int) ([][]int, error)

TryFindAllStringIndex returns the start and end of each match of a global search, at most n of them (all when n is below 0). After an empty match the search moves on one character, as matchAll does.

func (*Regexp) TryFindAllStringSubmatchIndex

func (re *Regexp) TryFindAllStringSubmatchIndex(s string, n int) ([][]int, error)

TryFindAllStringSubmatchIndex is TryFindAllStringIndex with the start and end of each group of each match.

func (*Regexp) TryFindAllSubmatchIndex

func (re *Regexp) TryFindAllSubmatchIndex(b []byte, n int) ([][]int, error)

TryFindAllSubmatchIndex is TryFindAllIndex with the start and end of each group of each match.

func (*Regexp) TryFindAllTextIndex

func (re *Regexp) TryFindAllTextIndex(t Text, n int) ([][]int, error)

TryFindAllTextIndex returns the start and end of each match of a global search, at most n (all when n is below 0).

func (*Regexp) TryFindAllUTF16Index

func (re *Regexp) TryFindAllUTF16Index(u []uint16, n int) ([][]int, error)

TryFindAllUTF16Index returns the start and end of each match of a global search, at most n of them (all when n is below 0).

func (*Regexp) TryFindAllUTF16SubmatchIndex

func (re *Regexp) TryFindAllUTF16SubmatchIndex(u []uint16, n int) ([][]int, error)

TryFindAllUTF16SubmatchIndex is TryFindAllUTF16Index with the start and end of each group of each match.

func (*Regexp) TryFindIndex

func (re *Regexp) TryFindIndex(b []byte) ([]int, error)

TryFindIndex returns the start and end of the first match, or nil.

func (*Regexp) TryFindStringIndex

func (re *Regexp) TryFindStringIndex(s string) ([]int, error)

TryFindStringIndex returns the start and end of the first match, or nil.

func (*Regexp) TryFindStringIndexAt

func (re *Regexp) TryFindStringIndexAt(s string, start int) ([]int, error)

TryFindStringIndexAt returns the start and end of the first match at or after the byte offset start (at it alone with the y flag), or nil.

func (*Regexp) TryFindStringSubmatchIndex

func (re *Regexp) TryFindStringSubmatchIndex(s string) ([]int, error)

TryFindStringSubmatchIndex returns the start and end of the first match and of each group, -1 for a group that did not take part, or nil.

func (*Regexp) TryFindStringSubmatchIndexAt

func (re *Regexp) TryFindStringSubmatchIndexAt(s string, start int) ([]int, error)

TryFindStringSubmatchIndexAt is TryFindStringSubmatchIndex from the byte offset start.

func (*Regexp) TryFindSubmatchIndex

func (re *Regexp) TryFindSubmatchIndex(b []byte) ([]int, error)

TryFindSubmatchIndex returns the start and end of the first match and of each group, -1 for a group that did not take part, or nil.

func (*Regexp) TryFindTextIndexAt

func (re *Regexp) TryFindTextIndexAt(t Text, start int) ([]int, error)

TryFindTextIndexAt returns the start and end of the first match at or after the byte offset start, or nil.

func (*Regexp) TryFindUTF16Index

func (re *Regexp) TryFindUTF16Index(u []uint16, start int) ([]int, error)

TryFindUTF16Index returns the start and end of the first match at or after the code unit start, or nil.

func (*Regexp) TryFindUTF16SubmatchIndex

func (re *Regexp) TryFindUTF16SubmatchIndex(u []uint16, start int) ([]int, error)

TryFindUTF16SubmatchIndex returns the start and end of the first match at or after the code unit start and of each group, -1 for a group that did not take part, or nil.

func (*Regexp) TryMatch

func (re *Regexp) TryMatch(b []byte) (bool, error)

TryMatch reports whether the pattern matches the bytes.

func (*Regexp) TryMatchString

func (re *Regexp) TryMatchString(s string) (bool, error)

TryMatchString reports whether the pattern matches the text.

func (*Regexp) TryMatchStringAt

func (re *Regexp) TryMatchStringAt(s string, start int) (bool, error)

TryMatchStringAt reports whether the pattern matches the text from the byte offset start.

func (*Regexp) TryMatchText

func (re *Regexp) TryMatchText(t Text) (bool, error)

TryMatchText reports whether the pattern matches the text.

func (*Regexp) TryMatchUTF16

func (re *Regexp) TryMatchUTF16(u []uint16) (bool, error)

TryMatchUTF16 reports whether the pattern matches the text.

func (*Regexp) TryMatchUTF16At

func (re *Regexp) TryMatchUTF16At(u []uint16, start int) (bool, error)

TryMatchUTF16At reports whether the pattern matches the text from the code unit start.

func (*Regexp) TryReplace

func (re *Regexp) TryReplace(src, template []byte) ([]byte, error)

TryReplace replaces the first match, or every match with the g flag, as String.prototype.replace does. The result is a new slice, or src itself when nothing matched.

func (*Regexp) TryReplaceAll

func (re *Regexp) TryReplaceAll(src, template []byte) ([]byte, error)

TryReplaceAll replaces every match, as String.prototype.replaceAll does.

func (*Regexp) TryReplaceAllString

func (re *Regexp) TryReplaceAllString(src, template string) (string, error)

TryReplaceAllString replaces every match, as String.prototype.replaceAll does.

func (*Regexp) TryReplaceAllText

func (re *Regexp) TryReplaceAllText(src Text, template string) (string, error)

TryReplaceAllText replaces every match, as String.prototype.replaceAll does.

func (*Regexp) TryReplaceAllUTF16

func (re *Regexp) TryReplaceAllUTF16(src, template []uint16) ([]uint16, error)

TryReplaceAllUTF16 replaces every match, as String.prototype.replaceAll does.

func (*Regexp) TryReplaceString

func (re *Regexp) TryReplaceString(src, template string) (string, error)

TryReplaceString replaces the first match, or every match with the g flag, as String.prototype.replace does, with the template's $ forms ($$, $&, $`, $', $n, $nn and $<name>) replaced.

func (*Regexp) TryReplaceText

func (re *Regexp) TryReplaceText(src Text, template string) (string, error)

TryReplaceText replaces the first match, or every match with the g flag, as String.prototype.replace does.

func (*Regexp) TryReplaceUTF16

func (re *Regexp) TryReplaceUTF16(src, template []uint16) ([]uint16, error)

TryReplaceUTF16 replaces the first match, or every match with the g flag, as String.prototype.replace does.

func (*Regexp) TrySplit

func (re *Regexp) TrySplit(s string, limit int) ([]Piece, error)

TrySplit splits the text around the matches, as String.prototype.split does: the groups of each match come between the pieces, and at most limit items (all when limit is below 0).

func (*Regexp) TrySplitText

func (re *Regexp) TrySplitText(t Text, limit int) ([]Piece, error)

TrySplitText splits the text around the matches, as String.prototype.split does.

func (*Regexp) TrySplitUTF16

func (re *Regexp) TrySplitUTF16(u []uint16, limit int) ([]Piece16, error)

TrySplitUTF16 splits the text around the matches, as String.prototype.split does.

func (*Regexp) UnicodeVersion added in v0.1.1

func (re *Regexp) UnicodeVersion() UnicodeVersion

UnicodeVersion returns the version of Unicode whose tables the pattern was read with, resolved: never UnicodeDefault or UnicodeLatest.

type Text

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

Text is UTF-8 text that has been checked once, so that the methods that take it check nothing. A program that matches one text many times makes a Text of it and pays for the check once.

func NewText

func NewText(b []byte) (Text, error)

NewText checks that the bytes are UTF-8 and returns them as a Text, which holds them without a copy: they must not be changed while it is in use. Bytes that are not UTF-8 are an error of kind ErrEncoding.

func NewTextString

func NewTextString(s string) (Text, error)

NewTextString checks that the string is UTF-8 and returns it as a Text.

func (Text) Len

func (t Text) Len() int

Len returns the length of the text in bytes.

func (Text) String

func (t Text) String() string

String returns the text.

type TooLargeError

type TooLargeError struct {
	Limit Limit
	Value int
}

TooLargeError reports a pattern over one of the limits of size. Limit names the limit and Value is its value. The pattern was not read to its end, so the error says nothing of whether it is valid.

func (*TooLargeError) Error

func (e *TooLargeError) Error() string

type UnicodeVersion added in v0.1.1

type UnicodeVersion uint8

UnicodeVersion is the version of Unicode whose character database a pattern is read with: the members of each \p{...}, the characters of a group name, and the case folding of the i flag. It is independent of the Edition.

const (
	// UnicodeDefault is the version of the V8 the answers are held to, Unicode 17 today. It moves when that V8 moves.
	UnicodeDefault UnicodeVersion = iota
	// Unicode13 is Unicode 13.0, the version of the year of ECMAScript 2020.
	Unicode13
	// Unicode17 is Unicode 17.0.
	Unicode17
	// Unicode18 is Unicode 18.0.
	Unicode18
	// UnicodeLatest is the latest version the module carries, Unicode 18 today. It moves when a later version is added.
	UnicodeLatest
)

func (UnicodeVersion) Major added in v0.1.1

func (v UnicodeVersion) Major() int

Major returns the major number of the version, resolved: 13, 17 or 18.

func (UnicodeVersion) Resolve added in v0.1.1

func (v UnicodeVersion) Resolve() UnicodeVersion

Resolve returns the version this one stands for: UnicodeDefault is V8's (Unicode17) and UnicodeLatest the newest the module carries (Unicode18). Any other is itself.

func (UnicodeVersion) String added in v0.1.1

func (v UnicodeVersion) String() string

String returns the name of the version: "default", "13", "17", "18" or "latest".

Directories

Path Synopsis
internal
expand command
Command expand writes out the templates of the matcher, one file for each encoding, as go generate runs it from the module's root:
Command expand writes out the templates of the matcher, one file for each encoding, as go generate runs it from the module's root:
ucd
Package ucd holds the Unicode Character Database properties the engine reads, for each version of Unicode the module carries (ucd.rs of the Rust crate): V13, V17 and V18, each a *UCD.
Package ucd holds the Unicode Character Database properties the engine reads, for each version of Unicode the module carries (ucd.rs of the Rust crate): V13, V17 and V18, each a *UCD.
tools
dataset command
Command dataset runs the engine over the ECMA-262 regular expression test data (github.com/corvus-dotnet/ecma262-regex-test-data), as the runner of the Rust crate does (tools/src/bin/dataset.rs there), with the same judging rules.
Command dataset runs the engine over the ECMA-262 regular expression test data (github.com/corvus-dotnet/ecma262-regex-test-data), as the runner of the Rust crate does (tools/src/bin/dataset.rs there), with the same judging rules.
stackprobe command
Command stackprobe asks the engine about patterns built to overflow the stack: deep (groups, lookarounds, modifier groups, quantified groups and the classes of the v flag, each nested to the limit, one past it and far past it), wide (many groups, classes, alternatives, backreferences, set operations and string alternatives side by side) and long (the longest pattern allowed, and one past it), in every grammar, through every member over UTF-8 and UTF-16, as the Rust crate's stack-probe does.
Command stackprobe asks the engine about patterns built to overflow the stack: deep (groups, lookarounds, modifier groups, quantified groups and the classes of the v flag, each nested to the limit, one past it and far past it), wide (many groups, classes, alternatives, backreferences, set operations and string alternatives side by side) and long (the longest pattern allowed, and one past it), in every grammar, through every member over UTF-8 and UTF-16, as the Rust crate's stack-probe does.

Jump to

Keyboard shortcuts

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