Added substitute flag - substitute matched text with given text

master
Aadhavan Srinivasan 4 weeks ago
parent ee02e7575e
commit 7916629c4d

@ -507,6 +507,7 @@ func main() {
multiLineFlag := flag.Bool("t", false, "Multi-line mode. Treats newline just like any character.")
printMatchesFlag := flag.Bool("p", false, "Prints start and end index of each match. Can only be used with '-t' for multi-line mode.")
caseInsensitiveFlag = flag.Bool("i", false, "Case-insensitive. Disregard the case of all characters.")
substituteText := flag.String("s", "", "Substitute the contents of each match with the given string. Overrides -o and -v")
flag.Parse()
// In multi-line mode, 'dot' metacharacter also matches newline
@ -519,6 +520,13 @@ func main() {
if *onlyFlag {
*lineFlag = false
}
// Check if substitute text has been enabled
substituteFlagEnabled := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "s" {
substituteFlagEnabled = true
}
})
// Process:
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
@ -607,6 +615,29 @@ func main() {
continue
}
}
// If we are substituting, we need a different behavior, as follows:
// For every character in the test string:
// 1. Check if the index is the start of any matchIndex
// 2. If so, print the substitute text, and set our index to
// the corresponding end index.
// 3. If not, just print the character.
if substituteFlagEnabled {
for i := range test_runes {
inMatchIndex := false
for _, idx := range matchIndices {
if i == idx.startIdx {
fmt.Fprintf(out, "%s", *substituteText)
i = idx.endIdx
inMatchIndex = true
break
}
}
if !inMatchIndex {
fmt.Fprintf(out, "%c", test_runes[i])
}
}
} else {
for i, c := range test_runes {
if indicesToPrint.contains(i) {
color.New(color.FgRed).Fprintf(out, "%c", c)
@ -625,6 +656,7 @@ func main() {
}
}
}
}
err = out.Flush()
if err != nil {
panic(err)

Loading…
Cancel
Save