Implement "replacement" variable that simply replaces the match

This commit is contained in:
2025-03-26 12:58:51 +01:00
parent 7e19cf4e2c
commit 7f4392b10e
2 changed files with 29 additions and 23 deletions

View File

@@ -213,29 +213,35 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
continue
}
// Apply the modifications to the original match
replacement := match
for i := len(modsMap) - 1; i >= 0; i-- {
newVal := modsMap[i]
log.Printf("Applying modification: %s", newVal)
// Indices of the group are relative to content
// To relate them to match we have to subtract the match start index
groupStart := groups[i*2] - matchIndices[0]
groupEnd := groups[i*2+1] - matchIndices[0]
replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
replacement := ""
replacementVar := L.GetGlobal("replacement")
if replacementVar.Type() != lua.LTNil {
replacement = replacementVar.String()
}
for i := len(namedCaptures) - 1; i >= 0; i-- {
capture := namedCaptures[i]
if capture.Name == "" {
continue
if replacement == "" {
// Apply the modifications to the original match
replacement = match
for i := len(modsMap) - 1; i >= 0; i-- {
newVal := modsMap[i]
log.Printf("Applying modification: %s", newVal)
// Indices of the group are relative to content
// To relate them to match we have to subtract the match start index
groupStart := groups[i*2] - matchIndices[0]
groupEnd := groups[i*2+1] - matchIndices[0]
replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
}
groupStart := capture.Range[0] - matchIndices[0]
groupEnd := capture.Range[1] - matchIndices[0]
luaValue := L.GetGlobal(capture.Name).String()
replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:]
}
for i := len(namedCaptures) - 1; i >= 0; i-- {
capture := namedCaptures[i]
if capture.Name == "" {
continue
}
groupStart := capture.Range[0] - matchIndices[0]
groupEnd := capture.Range[1] - matchIndices[0]
luaValue := L.GetGlobal(capture.Name).String()
replacement = replacement[:groupStart] + luaValue + replacement[groupEnd:]
}
}
modificationCount++
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
log.Printf("Modification count updated: %d", modificationCount)