From f6def1e5a5b1ff810de8125eacc00d4ddaf5e7e5 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 27 Mar 2025 22:11:03 +0100 Subject: [PATCH] Refactor entirety of replace command to main for now --- main.go | 50 ++++++++++++++++++++++---- processor/regex.go | 88 ++++++++++++++++++---------------------------- 2 files changed, 77 insertions(+), 61 deletions(-) diff --git a/main.go b/main.go index 8f181ee..a2244f3 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "path/filepath" + "sort" "sync" "time" @@ -135,27 +136,30 @@ func main() { return } logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file) + fileDataStr := string(fileData) + // Aggregate all the modifications and execute them modifications := []ReplaceCommand{} for _, command := range commands { logger.Info("Processing file %q with command %q", file, command.Pattern) // TODO: Run processor and return modifications } - for _, modification := range modifications { - logger.Info("Modifying file %q from %d to %d with %q", file, modification.From, modification.To, modification.With) - // TODO: Execute modifications - } + // 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)) - err = os.WriteFile(file, fileData, 0644) + fileDataStr = ExecuteModifications(modifications, fileDataStr) + + err = os.WriteFile(file, []byte(fileDataStr), 0644) if err != nil { logger.Error("Failed to write file %q: %v", file, err) return } } - // Aggregate all the modifications and execute them - // Taking care of the whole duplicates and shit // This will also relieve processor of some of the file loading // But we will also have to rework the tests....... // Also give each command its own logger, maybe prefix it with something... Maybe give commands a name? @@ -253,6 +257,38 @@ func main() { } } +func ExecuteModifications(modifications []ReplaceCommand, fileData string) string { + var err error + for _, modification := range modifications { + fileData, err = modification.Execute(fileData) + if err != nil { + logger.Error("Failed to execute modification: %v", err) + continue + } + } + return fileData +} + +func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) { + err := m.Validate(len(fileDataStr)) + if err != nil { + return "", fmt.Errorf("failed to validate modification: %v", err) + } + + logger.Trace("Replace pos %d-%d with %q", m.From, m.To, m.With) + return fileDataStr[:m.From] + m.With + fileDataStr[m.To:], nil +} + +func (m *ReplaceCommand) Validate(maxsize int) error { + if m.To < m.From { + return fmt.Errorf("command to is less than from: %v", m) + } + if m.From > maxsize || m.To > maxsize { + return fmt.Errorf("command from or to is greater than replacement length: %v", m) + } + return nil +} + func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) { associationCount := 0 fileCommands := make(map[string][]ModifyCommand) diff --git a/processor/regex.go b/processor/regex.go index 419e14b..3dbb4b9 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -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