Rework named captures to be array
To comply with the whole reverse replacing
This commit is contained in:
@@ -66,6 +66,12 @@ func (p *RegexProcessor) FromLua(L *lua.LState) (interface{}, error) {
|
|||||||
return modifications, nil
|
return modifications, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NamedCapture struct {
|
||||||
|
Name string
|
||||||
|
Value string
|
||||||
|
Range [2]int
|
||||||
|
}
|
||||||
|
|
||||||
// ProcessContent applies regex replacement with Lua processing
|
// ProcessContent applies regex replacement with Lua processing
|
||||||
func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
|
func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {
|
||||||
// Handle special pattern modifications
|
// Handle special pattern modifications
|
||||||
@@ -151,15 +157,21 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
|||||||
}
|
}
|
||||||
log.Printf("Captured groups: %v", captures)
|
log.Printf("Captured groups: %v", captures)
|
||||||
|
|
||||||
namedCaptures := make(map[string]string)
|
// We have to use array to preserve order
|
||||||
namedCaptureGroups := make(map[string][2]int)
|
// 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:]
|
groupNames := compiledPattern.SubexpNames()[1:]
|
||||||
for i, name := range groupNames {
|
for i, name := range groupNames {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
namedCaptures[name] = captures[i]
|
namedCaptures = append(namedCaptures, NamedCapture{
|
||||||
namedCaptureGroups[name] = [2]int{groups[i*2], groups[i*2+1]}
|
Name: name,
|
||||||
|
Value: captures[i],
|
||||||
|
Range: [2]int{groups[i*2], groups[i*2+1]},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Named captures: %v", namedCaptures)
|
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")
|
log.Println("Lua variables set successfully")
|
||||||
|
|
||||||
for name, capture := range namedCaptures {
|
for _, capture := range namedCaptures {
|
||||||
if name == "" {
|
if capture.Name == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if val, err := strconv.ParseFloat(capture, 64); err == nil {
|
if val, err := strconv.ParseFloat(capture.Value, 64); err == nil {
|
||||||
L.SetGlobal(name, lua.LNumber(val))
|
L.SetGlobal(capture.Name, lua.LNumber(val))
|
||||||
} else {
|
} 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-- {
|
for i := len(namedCaptures) - 1; i >= 0; i-- {
|
||||||
name := groupNames[i]
|
capture := namedCaptures[i]
|
||||||
if name == "" {
|
if capture.Name == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
groupStart := namedCaptureGroups[name][0] - matchIndices[0]
|
groupStart := capture.Range[0] - matchIndices[0]
|
||||||
groupEnd := namedCaptureGroups[name][1] - matchIndices[0]
|
groupEnd := capture.Range[1] - matchIndices[0]
|
||||||
luaValue := L.GetGlobal(name).String()
|
luaValue := L.GetGlobal(capture.Name).String()
|
||||||
replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:]
|
replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user