From 2c7a4f5d97df1028e0bd12769e72221298429273 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 26 Mar 2025 03:30:08 +0100 Subject: [PATCH] Add a lot more logs to regex What the fuck is going on? --- processor/regex.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/processor/regex.go b/processor/regex.go index ae3251d..0b1c938 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -2,6 +2,7 @@ package processor import ( "fmt" + "log" "regexp" "strconv" "strings" @@ -70,22 +71,27 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr // Handle special pattern modifications if !strings.HasPrefix(pattern, "(?s)") { pattern = "(?s)" + pattern + log.Printf("Pattern modified to include (?s): %s", pattern) } compiledPattern, err := regexp.Compile(pattern) if err != nil { + log.Printf("Error compiling pattern: %v", err) return "", 0, 0, fmt.Errorf("error compiling pattern: %v", err) } + log.Printf("Compiled pattern successfully: %s", pattern) previous := luaExpr luaExpr = BuildLuaScript(luaExpr) - fmt.Printf("Changing Lua expression from: %s to: %s\n", previous, luaExpr) + log.Printf("Changing Lua expression from: %s to: %s", previous, luaExpr) L, err := NewLuaState() if err != nil { + log.Printf("Error creating Lua state: %v", err) return "", 0, 0, fmt.Errorf("error creating Lua state: %v", err) } defer L.Close() + log.Printf("Lua state created successfully") // Initialize Lua environment modificationCount := 0 @@ -93,12 +99,16 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr // Process all regex matches result := content indices := compiledPattern.FindAllStringSubmatchIndex(content, -1) + log.Printf("Found %d matches in the content", len(indices)) + // 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-- { matchIndices := indices[i] + log.Printf("Processing match indices: %v", matchIndices) + // 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 @@ -107,14 +117,15 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr // As if concatenating in the middle of the array // Plus it supports lookarounds match := content[matchIndices[0]:matchIndices[1]] + log.Printf("Matched content: %s", match) groups := matchIndices[2:] if len(groups) <= 0 { - fmt.Println("No capture groups for lua to chew on") + log.Println("No capture groups for lua to chew on") continue } if len(groups)%2 == 1 { - fmt.Println("Odd number of indices of groups, what the fuck?") + log.Println("Odd number of indices of groups, what the fuck?") continue } @@ -122,28 +133,31 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr for j := 0; j < len(groups); j += 2 { captures = append(captures, content[groups[j]:groups[j+1]]) } + log.Printf("Captured groups: %v", captures) if err := p.ToLua(L, captures); err != nil { - fmt.Println("Error setting Lua variables:", err) + log.Printf("Error setting Lua variables: %v", err) continue } + log.Println("Lua variables set successfully") if err := L.DoString(luaExpr); err != nil { - fmt.Printf("Error executing Lua code %s for group %s: %v", luaExpr, captures, err) + log.Printf("Error executing Lua code %s for group %s: %v", luaExpr, captures, err) continue } + log.Println("Lua code executed successfully") // Get modifications from Lua modResult, err := p.FromLua(L) if err != nil { - fmt.Println("Error getting modifications:", err) + log.Printf("Error getting modifications: %v", err) continue } // Apply modifications to the matched text modsMap, ok := modResult.(map[int]string) if !ok || len(modsMap) == 0 { - fmt.Println("No modifications to apply") + log.Println("No modifications to apply") continue } @@ -151,6 +165,7 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr replacement := match for i := len(modsMap) - 1; i >= 0; i-- { newVal := modsMap[i] + log.Printf("Applying modification: %s", newVal) // Indices of the group are relative to content // To relate them to match we have to subtract the match start index groupStart := groups[i*2] - matchIndices[0] @@ -160,7 +175,9 @@ func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr modificationCount++ result = result[:matchIndices[0]] + replacement + result[matchIndices[1]:] + log.Printf("Modification count updated: %d", modificationCount) } + log.Printf("Process completed with %d modifications", modificationCount) return result, modificationCount, len(indices), nil }