Finished support for -m flag; refactoring pending

master
Aadhavan Srinivasan 3 weeks ago
parent 8a0586d107
commit 6f9173f771

@ -698,6 +698,12 @@ func main() {
}
test_runes = []rune(test_str)
matchIndices := findAllMatches(startState, test_runes, numGroups)
// If we are trying to print an invalid index, we just assume no specific matches will be printed.
if matchNumFlagEnabled && *matchNum > len(matchIndices) {
matchNumFlagEnabled = false
}
if *printMatchesFlag {
// if we are in single line mode, print the line on which
// the matches occur
@ -759,21 +765,29 @@ func main() {
if substituteFlagEnabled {
for i := range test_runes {
inMatchIndex := false
for _, idx := range matchIndices {
if i == idx[0].startIdx {
for idx, m := range matchIndices {
if !matchNumFlagEnabled || (idx+1) == *matchNum {
if i == m[0].startIdx {
fmt.Fprintf(out, "%s", *substituteText)
i = idx[0].endIdx
i = m[0].endIdx
inMatchIndex = true
break
}
}
}
if !inMatchIndex {
fmt.Fprintf(out, "%c", test_runes[i])
}
}
} else {
for i, c := range test_runes {
if indicesToPrint.contains(i) {
// Explanation:
// We print a letter in red if:
// 1. It is in the 'indicesToPrint'
// 2. One of the following:
// a. The '-m' flag is disabled
// b. The '-m' flag is enabled, and our current index is in the bounds of the specific match
if indicesToPrint.contains(i) && (!matchNumFlagEnabled || (i >= matchIndices[*matchNum-1][0].startIdx && i < matchIndices[*matchNum-1][0].endIdx)) {
color.New(color.FgRed).Fprintf(out, "%c", c)
// Newline after every match - only if -o is enabled and -v is disabled.
if *onlyFlag && !(*invertFlag) {

Loading…
Cancel
Save