From bf72734b90545240d875145dea354f800e90977b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 27 Mar 2025 23:02:33 +0100 Subject: [PATCH] Clean up regex.go a little --- .vscode/launch.json | 2 +- main.go | 6 ------ processor/regex.go | 24 ++++++++++++++++-------- utils/replacecommand.go | 7 +++++++ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9a8daf3..5364d53 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -26,7 +26,7 @@ "mode": "auto", "program": "${workspaceFolder}", "args": [ - "loglevel", + "-loglevel", "trace", "(?-s)LightComponent!anyrange=\"(!num)\"", "*4", diff --git a/main.go b/main.go index f003a06..035c14f 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "os" - "sort" "sync" "modify/processor" @@ -124,11 +123,6 @@ func main() { } // Sort commands in reverse order for safe replacements - sort.Slice(modifications, func(i, j int) bool { - return modifications[i].From > modifications[j].From - }) - logger.Trace("Applying %d replacement commands in reverse order", len(modifications)) - fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr) logger.Info("Executed %d modifications for file %q", count, file) diff --git a/processor/regex.go b/processor/regex.go index b2f9281..9a1369b 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -44,13 +44,18 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC // Process all regex matches indices := compiledPattern.FindAllStringSubmatchIndex(content, -1) logger.Debug("Found %d matches in content of length %d", len(indices), len(content)) + if len(indices) == 0 { + logger.Warning("No matches found for regex: %q", pattern) + return commands, nil + } // We walk backwards because we're replacing something with something else that might be longer // And in the case it is longer than the original all indicces past that change will be fucked up // By going backwards we fuck up all the indices to the end of the file that we don't care about // Because there either aren't any (last match) or they're already modified (subsequent matches) - for i := len(indices) - 1; i >= 0; i-- { + for i, matchIndices := range indices { logger.Debug("Processing match %d of %d", i+1, len(indices)) + logger.Trace("Match indices: %v (match position %d-%d)", matchIndices, matchIndices[0], matchIndices[1]) L, err := NewLuaState() if err != nil { @@ -63,9 +68,6 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC defer L.Close() logger.Trace("Lua state created successfully for match %d", i+1) - matchIndices := indices[i] - logger.Trace("Match indices: %v (match position %d-%d)", matchIndices, matchIndices[0], matchIndices[1]) - // Why we're doing this whole song and dance of indices is to properly handle empty matches // Plus it's a little cleaner to surgically replace our matches // If we were to use string.replace and encountered an empty match there'd be nothing to replace @@ -182,11 +184,11 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC modifiedGroups++ } } - logger.Debug("%d of %d capture groups were modified", modifiedGroups, len(captureGroups)) + logger.Info("%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) + logger.Info("Capture group unchanged: %s", capture.Value) continue } @@ -198,11 +200,17 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC // 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], + From: capture.Range[0], + To: capture.Range[1], With: capture.Updated, }) } + } else { + commands = append(commands, utils.ReplaceCommand{ + From: matchIndices[0], + To: matchIndices[1], + With: replacement, + }) } } diff --git a/utils/replacecommand.go b/utils/replacecommand.go index 6cabd31..c071861 100644 --- a/utils/replacecommand.go +++ b/utils/replacecommand.go @@ -3,6 +3,7 @@ package utils import ( "fmt" "modify/logger" + "sort" ) type ReplaceCommand struct { @@ -13,6 +14,12 @@ type ReplaceCommand struct { func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) { var err error + + sort.Slice(modifications, func(i, j int) bool { + return modifications[i].From > modifications[j].From + }) + logger.Trace("Applying %d replacement commands in reverse order", len(modifications)) + executed := 0 for _, modification := range modifications { fileData, err = modification.Execute(fileData)