Clean up regex.go a little
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -26,7 +26,7 @@
|
|||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${workspaceFolder}",
|
"program": "${workspaceFolder}",
|
||||||
"args": [
|
"args": [
|
||||||
"loglevel",
|
"-loglevel",
|
||||||
"trace",
|
"trace",
|
||||||
"(?-s)LightComponent!anyrange=\"(!num)\"",
|
"(?-s)LightComponent!anyrange=\"(!num)\"",
|
||||||
"*4",
|
"*4",
|
||||||
|
6
main.go
6
main.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"modify/processor"
|
"modify/processor"
|
||||||
@@ -124,11 +123,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sort commands in reverse order for safe replacements
|
// 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)
|
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
||||||
logger.Info("Executed %d modifications for file %q", count, file)
|
logger.Info("Executed %d modifications for file %q", count, file)
|
||||||
|
|
||||||
|
@@ -44,13 +44,18 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
// Process all regex matches
|
// Process all regex matches
|
||||||
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
|
indices := compiledPattern.FindAllStringSubmatchIndex(content, -1)
|
||||||
logger.Debug("Found %d matches in content of length %d", len(indices), len(content))
|
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
|
// 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
|
// 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
|
// 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)
|
// 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.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()
|
L, err := NewLuaState()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -63,9 +68,6 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
defer L.Close()
|
defer L.Close()
|
||||||
logger.Trace("Lua state created successfully for match %d", i+1)
|
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
|
// 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
|
// 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
|
// 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++
|
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 {
|
for _, capture := range captureGroups {
|
||||||
if capture.Value == capture.Updated {
|
if capture.Value == capture.Updated {
|
||||||
logger.Trace("Capture group unchanged: %s", capture.Value)
|
logger.Info("Capture group unchanged: %s", capture.Value)
|
||||||
continue
|
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
|
// To relate them to match we have to subtract the match start index
|
||||||
// replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
// replacement = replacement[:groupStart] + newVal + replacement[groupEnd:]
|
||||||
commands = append(commands, utils.ReplaceCommand{
|
commands = append(commands, utils.ReplaceCommand{
|
||||||
From: capture.Range[0] - matchIndices[0],
|
From: capture.Range[0],
|
||||||
To: capture.Range[1] - matchIndices[0],
|
To: capture.Range[1],
|
||||||
With: capture.Updated,
|
With: capture.Updated,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
commands = append(commands, utils.ReplaceCommand{
|
||||||
|
From: matchIndices[0],
|
||||||
|
To: matchIndices[1],
|
||||||
|
With: replacement,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
"modify/logger"
|
||||||
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ReplaceCommand struct {
|
type ReplaceCommand struct {
|
||||||
@@ -13,6 +14,12 @@ type ReplaceCommand struct {
|
|||||||
|
|
||||||
func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) {
|
func ExecuteModifications(modifications []ReplaceCommand, fileData string) (string, int) {
|
||||||
var err error
|
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
|
executed := 0
|
||||||
for _, modification := range modifications {
|
for _, modification := range modifications {
|
||||||
fileData, err = modification.Execute(fileData)
|
fileData, err = modification.Execute(fileData)
|
||||||
|
Reference in New Issue
Block a user