Also report submatch positions
This commit is contained in:
parent
dffb87ced9
commit
3078b9074a
@ -51,9 +51,9 @@ func New(allow_escape_codes bool) *Context {
|
||||
|
||||
func replace_all_rst_roles(str string, repl func(rst_format_match) string) string {
|
||||
var m rst_format_match
|
||||
rf := func(full_match string, groupdict map[string]string) string {
|
||||
m.payload = groupdict["payload"]
|
||||
m.role = groupdict["role"]
|
||||
rf := func(full_match string, groupdict map[string]utils.SubMatch) string {
|
||||
m.payload = groupdict["payload"].Text
|
||||
m.role = groupdict["role"].Text
|
||||
return repl(m)
|
||||
}
|
||||
return utils.ReplaceAll(":(?P<role>[a-z]+):(?:(?:`(?P<payload>[^`]+)`)|(?:'(?P<payload>[^']+)'))", str, rf)
|
||||
|
||||
@ -53,7 +53,12 @@ func (self *UnboundedCache[K, V]) MustGetOrCreate(key K, create func(key K) V) V
|
||||
|
||||
var pat_cache = NewUnboundedCache[string, *regexp.Regexp]()
|
||||
|
||||
func ReplaceAll(pat, str string, repl func(full_match string, groupdict map[string]string) string) string {
|
||||
type SubMatch struct {
|
||||
Text string
|
||||
Start, End int
|
||||
}
|
||||
|
||||
func ReplaceAll(pat, str string, repl func(full_match string, groupdict map[string]SubMatch) string) string {
|
||||
cpat := pat_cache.MustGetOrCreate(pat, regexp.MustCompile)
|
||||
result := strings.Builder{}
|
||||
result.Grow(len(str) + 256)
|
||||
@ -63,14 +68,14 @@ func ReplaceAll(pat, str string, repl func(full_match string, groupdict map[stri
|
||||
for _, v := range matches {
|
||||
match_start, match_end := v[0], v[1]
|
||||
full_match := str[match_start:match_end]
|
||||
groupdict := make(map[string]string, len(names))
|
||||
groupdict := make(map[string]SubMatch, len(names))
|
||||
for i, name := range names {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
idx := 2 * i
|
||||
if v[idx] > -1 && v[idx+1] > -1 {
|
||||
groupdict[name] = str[v[idx]:v[idx+1]]
|
||||
groupdict[name] = SubMatch{Text: str[v[idx]:v[idx+1]], Start: v[idx] - match_start, End: v[idx+1] - match_start}
|
||||
}
|
||||
}
|
||||
result.WriteString(str[last_index:match_start])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user