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.") 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.") 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.") 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() flag.Parse()
// In multi-line mode, 'dot' metacharacter also matches newline // In multi-line mode, 'dot' metacharacter also matches newline
@ -519,6 +520,13 @@ func main() {
if *onlyFlag { if *onlyFlag {
*lineFlag = false *lineFlag = false
} }
// Check if substitute text has been enabled
substituteFlagEnabled := false
flag.Visit(func(f *flag.Flag) {
if f.Name == "s" {
substituteFlagEnabled = true
}
})
// Process: // Process:
// 1. Convert regex into postfix notation (Shunting-Yard algorithm) // 1. Convert regex into postfix notation (Shunting-Yard algorithm)
@ -607,21 +615,45 @@ func main() {
continue continue
} }
} }
for i, c := range test_runes {
if indicesToPrint.contains(i) { // If we are substituting, we need a different behavior, as follows:
color.New(color.FgRed).Fprintf(out, "%c", c) // For every character in the test string:
// Newline after every match - only if -o is enabled and -v is disabled. // 1. Check if the index is the start of any matchIndex
if *onlyFlag && !(*invertFlag) { // 2. If so, print the substitute text, and set our index to
for _, idx := range matchIndices { // the corresponding end index.
if i+1 == idx.endIdx { // End index is one more than last index of match // 3. If not, just print the character.
fmt.Fprintf(out, "\n") if substituteFlagEnabled {
break 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
} }
} }
} else { if !inMatchIndex {
if !(*onlyFlag) { fmt.Fprintf(out, "%c", test_runes[i])
fmt.Fprintf(out, "%c", c) }
}
} else {
for i, c := range test_runes {
if indicesToPrint.contains(i) {
color.New(color.FgRed).Fprintf(out, "%c", c)
// Newline after every match - only if -o is enabled and -v is disabled.
if *onlyFlag && !(*invertFlag) {
for _, idx := range matchIndices {
if i+1 == idx.endIdx { // End index is one more than last index of match
fmt.Fprintf(out, "\n")
break
}
}
}
} else {
if !(*onlyFlag) {
fmt.Fprintf(out, "%c", c)
}
} }
} }
} }

Loading…
Cancel
Save