From 7e19cf4e2c2beb77e39f3541c4dffa672bf9eaa6 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 26 Mar 2025 12:50:55 +0100 Subject: [PATCH] Rework named captures to be array To comply with the whole reverse replacing --- processor/regex.go | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/processor/regex.go b/processor/regex.go index 3abdec5..767f2b7 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -66,6 +66,12 @@ func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) { return modifications, nil } +type NamedCapture struct { + Name string + Value string + Range [2]int +} + // ProcessContent applies regex replacement with Lua processing func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) { // Handle special pattern modifications @@ -151,15 +157,21 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr } log.Printf("Captured groups: %v", captures) - namedCaptures := make(map[string]string) - namedCaptureGroups := make(map[string][2]int) + // We have to use array to preserve order + // Very important for the reconstruction step + // Because we must overwrite the values in reverse order + // See comments a few dozen lines above for more details + namedCaptures := make([]NamedCapture, 0, len(groups)/2) groupNames := compiledPattern.SubexpNames()[1:] for i, name := range groupNames { if name == "" { continue } - namedCaptures[name] = captures[i] - namedCaptureGroups[name] = [2]int{groups[i*2], groups[i*2+1]} + namedCaptures = append(namedCaptures, NamedCapture{ + Name: name, + Value: captures[i], + Range: [2]int{groups[i*2], groups[i*2+1]}, + }) } log.Printf("Named captures: %v", namedCaptures) @@ -170,14 +182,14 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr } log.Println("Lua variables set successfully") - for name, capture := range namedCaptures { - if name == "" { + for _, capture := range namedCaptures { + if capture.Name == "" { continue } - if val, err := strconv.ParseFloat(capture, 64); err == nil { - L.SetGlobal(name, lua.LNumber(val)) + if val, err := strconv.ParseFloat(capture.Value, 64); err == nil { + L.SetGlobal(capture.Name, lua.LNumber(val)) } else { - L.SetGlobal(name, lua.LString(capture)) + L.SetGlobal(capture.Name, lua.LString(capture.Value)) } } @@ -214,13 +226,13 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr } for i := len(namedCaptures) - 1; i >= 0; i-- { - name := groupNames[i] - if name == "" { + capture := namedCaptures[i] + if capture.Name == "" { continue } - groupStart := namedCaptureGroups[name][0] - matchIndices[0] - groupEnd := namedCaptureGroups[name][1] - matchIndices[0] - luaValue := L.GetGlobal(name).String() + groupStart := capture.Range[0] - matchIndices[0] + groupEnd := capture.Range[1] - matchIndices[0] + luaValue := L.GetGlobal(capture.Name).String() replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:] }