Clean up regex.go a little

This commit is contained in:
2025-03-27 23:02:33 +01:00
parent cc30c2bdcb
commit bf72734b90
4 changed files with 24 additions and 15 deletions

2
.vscode/launch.json vendored
View File

@@ -26,7 +26,7 @@
"mode": "auto",
"program": "${workspaceFolder}",
"args": [
"loglevel",
"-loglevel",
"trace",
"(?-s)LightComponent!anyrange=\"(!num)\"",
"*4",

View File

@@ -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)

View File

@@ -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,
})
}
}

View File

@@ -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)