/* Package regexp2 is a regexp package that has an interface similar to Go's framework regexp engine but uses a more feature full regex engine behind the scenes. It doesn't have constant time guarantees, but it allows backtracking and is compatible with Perl5 and .NET. You'll likely be better off with the RE2 engine from the regexp package and should only use this if you need to write very complex patterns or require compatibility with .NET. */
package regexp2 import ( ) var ( // DefaultMatchTimeout used when running regexp matches -- "forever" DefaultMatchTimeout = time.Duration(math.MaxInt64) // ErrBacktrackingStackLimit is returned when a match exceeds its configured backtracking stack size. ErrBacktrackingStackLimit = errors.New("regexp2: maximum backtracking stack size exceeded") ) // Regexp is the representation of a compiled regular expression. // A Regexp is safe for concurrent use by multiple goroutines. type Regexp struct { // A match will time out if it takes (approximately) more than // MatchTimeout. This is a safety check in case the match // encounters catastrophic backtracking. The default value // (DefaultMatchTimeout) causes all time out checking to be // suppressed. MatchTimeout time.Duration // read-only after Compile pattern string // as passed to Compile options RegexOptions // options debug bool caps map[int]int // capnum->index capnames map[string]int //capture group name -> index capslist []string //sorted list of capture group names capsize int // size of the capture array code *syntax.Code // compiled program optimizations OptimizationOptions // cache of machines for running regexp runnerPool *sync.Pool replaceCache *replacerDataCache // hook points to override runner functions findFirstChar func(r *Runner) bool execute func(r *Runner) error executeQuick func(r *Runner) error stringPrefixFilter StringPrefixFilter quickCode *syntax.Code // bool-only program with unobservable captures removed } // Compile parses a regular expression and returns, if successful, // a Regexp object that can be used to match against text. func ( string, ...CompileOption) (*Regexp, error) { := newCompileConfig() return compile(, ) } func compile( string, compileConfig) (*Regexp, error) { // parse it := syntax.ParseOptions{ RegexOptions: syntax.RegexOptions(.regexOptions), MaintainCaptureOrder: .maintainCaptureOrder, CodeGen: .codeGen, } , := syntax.Parse(, ) if != nil { return nil, } if .debug { log.Print(.Dump()) } // translate it to code , := syntax.Write() if != nil { return nil, } if .debug { log.Print(.Dump()) } if !.optimizations.DisableCharClassASCIIBitmap { .PrepareCharSetASCIIBitmaps() } // return it := &Regexp{ pattern: , options: .regexOptions, debug: .debug, caps: .Caps, capnames: .Capnames, capslist: .Caplist, capsize: .Capsize, code: , quickCode: makeQuickCode(), MatchTimeout: DefaultMatchTimeout, optimizations: .optimizations, } .stringPrefixFilter = newStringPrefixFilter() .initCaches() return , nil } func makeQuickCode( *syntax.Code) *syntax.Code { if == nil || len(.QuickCodes) == 0 { return nil } := * .Codes = .QuickCodes .Dispatches = .QuickDispatches .QuickCodes = nil .QuickDispatches = nil return & } // MustCompile is like Compile but panics if the expression cannot be parsed. // It simplifies safe initialization of global variables holding compiled regular // expressions. func ( string, ...CompileOption) *Regexp { := newCompileConfig() // lookup if we have a pre-built state machine for this pattern and options := getEngineRegexp(, ) if != nil { return } , := compile(, ) if != nil { panic(`regexp2: Compile(` + quote() + `): ` + .Error()) } return } // Escape adds backslashes to any special characters in the input string func ( string) string { return syntax.Escape() } // Unescape removes any backslashes from previously-escaped special characters in the input string func ( string) (string, error) { return syntax.Unescape() } // SetTimeoutPeriod is a debug function that sets the frequency of the timeout goroutine's sleep cycle. // Defaults to 100ms. The only benefit of setting this lower is that the 1 background goroutine that manages // timeouts may exit slightly sooner after all the timeouts have expired. See Github issue #63 func ( time.Duration) { clockPeriod = } // StopTimeoutClock should only be used in unit tests to prevent the timeout clock goroutine // from appearing like a leaking goroutine func () { stopClock() } // String returns the source text used to compile the regular expression. func ( *Regexp) () string { return .pattern } func quote( string) string { if strconv.CanBackquote() { return "`" + + "`" } return strconv.Quote() } func ( *Regexp) () bool { return .options&RightToLeft != 0 } func ( *Regexp) () bool { return .debug } // Replace searches the input string and replaces each match found with the replacement text. // Count will limit the number of matches attempted and startAt will allow // us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). // Set startAt and count to -1 to go through the whole string func ( *Regexp) (, string, , int) (string, error) { , := .getReplacerData() if != nil { return "", } return replace(, , nil, , , ) } func ( *Regexp) ( string) (*syntax.ReplacerData, error) { := .replaceCache != nil && .optimizations.cacheReplacerData() if { if , := .replaceCache.get(); { return , nil } } , := syntax.NewReplacerData(, .caps, .capsize, .capnames, syntax.RegexOptions(.options)) if != nil { return nil, } if { .replaceCache.add(, ) } return , nil } // ReplaceFunc searches the input string and replaces each match found using the string from the evaluator // Count will limit the number of matches attempted and startAt will allow // us to skip past possible matches at the start of the input (left or right depending on RightToLeft option). // Set startAt and count to -1 to go through the whole string. func ( *Regexp) ( string, MatchEvaluator, , int) (string, error) { return replace(, nil, , , , ) } // FindStringMatch searches the input string for a Regexp match func ( *Regexp) ( string) (*Match, error) { , , := .findStringMatchStart(, -1) if != nil { return nil, } if ! { return nil, nil } , := .getRunesAndStart(, ) if < 0 { = 0 } return .run(false, , -1, , newStringMatchText(, )) } // FindRunesMatch searches the input rune slice for a Regexp match func ( *Regexp) ( []rune) (*Match, error) { return .run(false, -1, -1, , newMatchText()) } // FindStringMatchStartingAt searches the input string for a Regexp match starting at the startAt index func ( *Regexp) ( string, int) (*Match, error) { , , := .findStringMatchStart(, ) if != nil { return nil, } if ! { return nil, nil } , := .getRunesAndStart(, ) if == -1 { // we didn't find our start index in the string -- that's a problem return nil, errors.New("startAt must align to the start of a valid rune in the input string") } return .run(false, , -1, , newStringMatchText(, )) } // FindRunesMatchStartingAt searches the input rune slice for a Regexp match starting at the startAt index func ( *Regexp) ( []rune, int) (*Match, error) { return .run(false, , -1, , newMatchText()) } // FindAllStringIndex returns a slice of byte index pairs identifying all // successive matches in s. func ( *Regexp) ( string, int) ([][]int, error) { if == 0 { return nil, nil } , , := .findStringMatchStart(, -1) if != nil { return nil, } if ! { return nil, nil } := .getRunner() var []rune var *[]rune := 0 if == 0 { , = .decodeString() } else { , , = .decodeStringWithStart(, ) } defer func() { .putRunner() if != nil { * = pooledRuneBuffers.put() } }() if < 0 { = 0 } := newStringByteMapper() if .quickCode != nil { .code = .quickCode } return .findAllRunesIndex(, , , , func(, int) (int, int) { if == nil { return , + } return .byteIndex(), .byteIndex( + ) }) } // FindAllRunesIndex returns a slice of rune index pairs identifying all // successive matches in r. func ( *Regexp) ( []rune, int) ([][]int, error) { if == 0 { return nil, nil } := .getRunner() defer .putRunner() := 0 if .RightToLeft() { = len() } if .quickCode != nil { .code = .quickCode } return .findAllRunesIndex(, , , , func(, int) (int, int) { return , + }) } func ( *Regexp) ( *Runner, []rune, , int, func(, int) (int, int)) ([][]int, error) { var [][]int var []int if > 0 { = make([][]int, 0, ) = make([]int, 0, *2) } := -1 := -1 for != 0 { , := .scan(, nil, , , true, .MatchTimeout) if != nil { return nil, } if == nil { break } if .RuneLength != 0 || .RuneIndex != { , := (.RuneIndex, .RuneLength) = append(, , ) = append(, [len()-2:len():len()]) = .RuneIndex + .RuneLength if > 0 { -- } } = .textpos = .RuneLength } return , nil } type stringByteMapper struct { runeIndexes []int deltas []int } func newStringByteMapper( string) *stringByteMapper { var *stringByteMapper := 0 := 0 for , := range { := utf8.RuneLen() if == utf8.RuneError { _, = utf8.DecodeRuneInString([:]) } if != 1 { if == nil { = &stringByteMapper{} } += - 1 .runeIndexes = append(.runeIndexes, +1) .deltas = append(.deltas, ) } ++ } return } func ( *stringByteMapper) ( int) int { := sort.Search(len(.runeIndexes), func( int) bool { return .runeIndexes[] > }) - 1 if < 0 { return } return + .deltas[] } // FindNextMatch returns the next match in the same input string as the match parameter. // Will return nil if there is no next match or if given a nil match. func ( *Regexp) ( *Match) (*Match, error) { if == nil { return nil, nil } return .run(false, .textpos, .RuneLength, .text.runes, .text) } // MatchString return true if the string matches the regex // error will be set if a timeout occurs func ( *Regexp) ( string) (bool, error) { if .stringPrefixFilter != nil && !.RightToLeft() { , := .stringPrefixFilter(, 0) if ! { return false, nil } return .matchStringAt(, ) } return .matchString() } func ( *Regexp) ( string) (bool, error) { return .matchStringAt(, -1) } func ( *Regexp) ( string, int) (bool, error) { := .getRunner() var []rune var *[]rune := 0 if <= 0 { , = .decodeString() if .RightToLeft() { = len() } } else { , , = .decodeStringWithStart(, ) if < 0 { = 0 } } defer func() { .putRunner() if != nil { * = pooledRuneBuffers.put() } }() if .quickCode != nil { .code = .quickCode } , := .scan(, nil, , -1, true, .MatchTimeout) if != nil { return false, } return != nil, nil } func ( *Regexp) ( string, int) ([]rune, int) { if < 0 { if .RightToLeft() { := getRunes() return , len() } return getRunes(), 0 } := make([]rune, len()) := 0 := -1 for , := range { if == { = } [] = ++ } if == len() { = } return [:], } func getRunes( string) []rune { return []rune() } // MatchRunes return true if the runes matches the regex // error will be set if a timeout occurs func ( *Regexp) ( []rune) (bool, error) { , := .run(true, -1, -1, , nil) if != nil { return false, } return != nil, nil } // GetGroupNames Returns the set of strings used to name capturing groups in the expression. func ( *Regexp) () []string { var []string if .capslist == nil { = make([]string, .capsize) for := 0; < len(); ++ { [] = strconv.Itoa() } } else { = make([]string, len(.capslist)) copy(, .capslist) } return } // GetGroupNumbers returns the integer group numbers corresponding to a group name. func ( *Regexp) () []int { var []int if .caps == nil { = make([]int, .capsize) for := 0; < len(); ++ { [] = } } else { = make([]int, len(.caps)) for , := range .caps { [] = } } return } // GroupNameFromNumber retrieves a group name that corresponds to a group number. // It will return "" for an unknown group number. Unnamed groups automatically // receive a name that is the decimal string equivalent of its number, except in // ECMAScript mode where unnamed groups have no name. func ( *Regexp) ( int) string { if .capslist == nil { if >= 0 && < .capsize { return strconv.Itoa() } return "" } if .caps != nil { var bool if , = .caps[]; ! { return "" } } if >= 0 && < len(.capslist) { return .capslist[] } return "" } // GroupNumberFromName returns a group number that corresponds to a group name. // Returns -1 if the name is not a recognized group name. Numbered groups // automatically get a group name that is the decimal string equivalent of its // number, except in ECMAScript mode where unnamed groups have no name. func ( *Regexp) ( string) int { // look up name if we have a hashtable of names if .capnames != nil { if , := .capnames[]; { return } return -1 } // convert to an int if it looks like a number := 0 for := 0; < len(); ++ { := [] if > '9' || < '0' { return -1 } *= 10 += int( - '0') } // return int if it's in range if >= 0 && < .capsize { return } return -1 } // MarshalText implements [encoding.TextMarshaler]. The output // matches that of calling the [Regexp.String] method. func ( *Regexp) () ([]byte, error) { return []byte(.String()), nil } // UnmarshalText implements [encoding.TextUnmarshaler] by calling // [Compile] on the encoded value. func ( *Regexp) ( []byte) error { , := Compile(string(), DefaultUnmarshalOptions) if != nil { return } * = * return nil } func ( *Regexp) () { .runnerPool = &sync.Pool{ New: func() any { return &Runner{ re: , code: .code, } }, } if .optimizations.MaxCachedReplacerDataEntries > 0 { .replaceCache = newReplacerDataCache(.optimizations.MaxCachedReplacerDataEntries) } } type replacerDataCache struct { mu sync.Mutex maxSize int ll *list.List cache map[string]*list.Element } type replacerDataCacheEntry struct { key string data *syntax.ReplacerData } func newReplacerDataCache( int) *replacerDataCache { return &replacerDataCache{ maxSize: , ll: list.New(), cache: make(map[string]*list.Element), } } func ( *replacerDataCache) ( string) (*syntax.ReplacerData, bool) { .mu.Lock() defer .mu.Unlock() if , := .cache[]; { .ll.MoveToFront() return .Value.(*replacerDataCacheEntry).data, true } return nil, false } func ( *replacerDataCache) ( string, *syntax.ReplacerData) { .mu.Lock() defer .mu.Unlock() if , := .cache[]; { .Value.(*replacerDataCacheEntry).data = .ll.MoveToFront() return } := .ll.PushFront(&replacerDataCacheEntry{key: , data: }) .cache[] = if .maxSize > 0 && .ll.Len() > .maxSize { := .ll.Back() if != nil { .ll.Remove() delete(.cache, .Value.(*replacerDataCacheEntry).key) } } }