Refactor entirety of replace command to main for now
This commit is contained in:
@@ -3,7 +3,6 @@ package processor
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -240,59 +239,40 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr
|
||||
continue
|
||||
}
|
||||
|
||||
if replacement == "" {
|
||||
commands := make([]ReplaceCommand, 0, len(captureGroups))
|
||||
// Apply the modifications to the original match
|
||||
replacement = match
|
||||
|
||||
// Count groups that were actually modified
|
||||
modifiedGroups := 0
|
||||
for _, capture := range captureGroups {
|
||||
if capture.Value != capture.Updated {
|
||||
modifiedGroups++
|
||||
}
|
||||
}
|
||||
logger.Debug("%d of %d capture groups were modified", modifiedGroups, len(captureGroups))
|
||||
|
||||
for _, capture := range captureGroups {
|
||||
if capture.Value == capture.Updated {
|
||||
logger.Trace("Capture group unchanged: %s", capture.Value)
|
||||
continue
|
||||
}
|
||||
|
||||
// Log what changed with context
|
||||
logger.Debug("Modifying group %s: %q → %q",
|
||||
capture.Name, capture.Value, capture.Updated)
|
||||
|
||||
// Indices of the group are relative to content
|
||||
// To relate them to match we have to subtract the match start index
|
||||
// replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
||||
commands = append(commands, ReplaceCommand{
|
||||
From: capture.Range[0] - matchIndices[0],
|
||||
To: capture.Range[1] - matchIndices[0],
|
||||
With: capture.Updated,
|
||||
})
|
||||
}
|
||||
|
||||
// Sort commands in reverse order for safe replacements
|
||||
sort.Slice(commands, func(i, j int) bool {
|
||||
return commands[i].From > commands[j].From
|
||||
})
|
||||
logger.Trace("Applying %d replacement commands in reverse order", len(commands))
|
||||
|
||||
for _, command := range commands {
|
||||
logger.Trace("Replace pos %d-%d with %q", command.From, command.To, command.With)
|
||||
if command.To < command.From {
|
||||
logger.Error("Command to is less than from: %v", command)
|
||||
continue
|
||||
}
|
||||
if command.From > len(replacement) || command.To > len(replacement) {
|
||||
logger.Error("Command from or to is greater than replacement length: %v", command)
|
||||
continue
|
||||
}
|
||||
replacement = replacement[:command.From] + command.With + replacement[command.To:]
|
||||
}
|
||||
}
|
||||
// if replacement == "" {
|
||||
// commands := make([]ReplaceCommand, 0, len(captureGroups))
|
||||
// // Apply the modifications to the original match
|
||||
// replacement = match
|
||||
//
|
||||
// // Count groups that were actually modified
|
||||
// modifiedGroups := 0
|
||||
// for _, capture := range captureGroups {
|
||||
// if capture.Value != capture.Updated {
|
||||
// modifiedGroups++
|
||||
// }
|
||||
// }
|
||||
// logger.Debug("%d of %d capture groups were modified", modifiedGroups, len(captureGroups))
|
||||
//
|
||||
// for _, capture := range captureGroups {
|
||||
// if capture.Value == capture.Updated {
|
||||
// logger.Trace("Capture group unchanged: %s", capture.Value)
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// // Log what changed with context
|
||||
// logger.Debug("Modifying group %s: %q → %q",
|
||||
// capture.Name, capture.Value, capture.Updated)
|
||||
//
|
||||
// // Indices of the group are relative to content
|
||||
// // To relate them to match we have to subtract the match start index
|
||||
// // replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
||||
// commands = append(commands, ReplaceCommand{
|
||||
// From: capture.Range[0] - matchIndices[0],
|
||||
// To: capture.Range[1] - matchIndices[0],
|
||||
// With: capture.Updated,
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
// Preview the replacement for logging
|
||||
replacementPreview := replacement
|
||||
|
Reference in New Issue
Block a user