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 continue
} }
// Apply the modifications to the original match replacement := ""
replacement := match replacementVar := L.GetGlobal("replacement")
for i := len(modsMap) - 1; i >= 0; i-- { if replacementVar.Type() != lua.LTNil {
newVal := modsMap[i] replacement = replacementVar.String()
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:]
} }
if replacement == "" {
for i := len(namedCaptures) - 1; i >= 0; i-- { // Apply the modifications to the original match
capture := namedCaptures[i] replacement = match
if capture.Name == "" { for i := len(modsMap) - 1; i >= 0; i-- {
continue 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++ modificationCount++
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:] result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
log.Printf("Modification count updated: %d", modificationCount) log.Printf("Modification count updated: %d", modificationCount)

View File

@@ -871,7 +871,7 @@ func TestLuaFunctionsOnNamedCaptures(t *testing.T) {
`<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`, `<user name="(?<name>[^"]+)" role="(?<role>[^"]+)"`,
`-- Capitalize first letters for regular users `-- Capitalize first letters for regular users
if role == "user" then if role == "user" then
name = name:gsub("(%w)(%w*)", function(first, rest) return first:upper()..rest end):gsub(" (%w)(%w*)", " %1%2":gsub("%%1", function(x) return x:upper() end)) name = name:gsub("(%w)(%w*)", function(first, rest) return first:upper()..rest end):gsub(" (%w)(%w*)", " %1%2")
else else
-- Uppercase for admins -- Uppercase for admins
name = string.upper(name) name = string.upper(name)
@@ -910,10 +910,10 @@ func TestNamedCaptureWithMath(t *testing.T) {
p := &RegexProcessor{} p := &RegexProcessor{}
result, mods, matches, err := p.ProcessContent( result, mods, matches, err := p.ProcessContent(
content, content,
`<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"`, `<item price="(?<price>\d+\.\d+)" quantity="(?<qty>\d+)"!any$`,
`-- Calculate and add total `-- Calculate and add total
replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />', replacement = string.format('<item price="%s" quantity="%s" total="%.2f" />',
price, qty, price * qty)`) price, qty, price * qty)`)
if err != nil { if err != nil {
t.Fatalf("Error processing content: %v", err) t.Fatalf("Error processing content: %v", err)