This commit is contained in:
2025-03-27 22:55:52 +01:00
parent f453079c72
commit cc30c2bdcb
6 changed files with 70 additions and 133 deletions

View File

@@ -20,7 +20,9 @@ type CaptureGroup struct {
}
// ProcessContent applies regex replacement with Lua processing
func ProcessRegex(content string, command utils.ModifyCommand) (string, int, int, error) {
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
var commands []utils.ReplaceCommand
logger.Trace("Processing regex: %q", command.Pattern)
// We don't HAVE to do this multiple times for a pattern
// But it's quick enough for us to not care
pattern := resolveRegexPlaceholders(command.Pattern)
@@ -28,7 +30,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) (string, int, int
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
logger.Error("Error compiling pattern: %v", err)
return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err)
return commands, fmt.Errorf("error compiling pattern: %v", err)
}
logger.Debug("Compiled pattern successfully: %s", pattern)
@@ -39,11 +41,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) (string, int, int
luaExpr := BuildLuaScript(command.LuaExpr)
logger.Debug("Transformed Lua expression: %q → %q", previous, luaExpr)
// Initialize Lua environment
modificationCount := 0
// Process all regex matches
result := content
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
logger.Debug("Found %d matches in content of length %d", len(indices), len(content))
@@ -57,7 +55,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) (string, int, int
L, err := NewLuaState()
if err != nil {
logger.Error("Error creating Lua state: %v", err)
return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err)
return commands, fmt.Errorf("error creating Lua state: %v", err)
}
// Hmm... Maybe we don't want to defer this..
// Maybe we want to close them every iteration
@@ -173,55 +171,42 @@ func ProcessRegex(content string, command utils.ModifyCommand) (string, int, int
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,
// })
// }
// }
if replacement == "" {
// Apply the modifications to the original match
replacement = match
// Preview the replacement for logging
replacementPreview := replacement
if len(replacement) > 50 {
replacementPreview = replacement[:47] + "..."
// 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, utils.ReplaceCommand{
From: capture.Range[0] - matchIndices[0],
To: capture.Range[1] - matchIndices[0],
With: capture.Updated,
})
}
}
logger.Debug("Replacing match %q with %q", matchPreview, replacementPreview)
modificationCount++
result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:]
logger.Debug("Match #%d processed, running modification count: %d", i+1, modificationCount)
}
logger.Info("Regex processing complete: %d modifications from %d matches", modificationCount, len(indices))
return result, modificationCount, len(indices), nil
return commands, nil
}
func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {