Clean up regex.go a little
This commit is contained in:
@@ -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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user