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) test_runes = []rune(test_str)
matchIndices := findAllMatches(startState, test_runes, numGroups) 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 *printMatchesFlag {
// if we are in single line mode, print the line on which // if we are in single line mode, print the line on which
// the matches occur // the matches occur
@ -759,12 +765,14 @@ func main() {
if substituteFlagEnabled { if substituteFlagEnabled {
for i := range test_runes { for i := range test_runes {
inMatchIndex := false inMatchIndex := false
for _, idx := range matchIndices { for idx, m := range matchIndices {
if i == idx[0].startIdx { if !matchNumFlagEnabled || (idx+1) == *matchNum {
fmt.Fprintf(out, "%s", *substituteText) if i == m[0].startIdx {
i = idx[0].endIdx fmt.Fprintf(out, "%s", *substituteText)
inMatchIndex = true i = m[0].endIdx
break inMatchIndex = true
break
}
} }
} }
if !inMatchIndex { if !inMatchIndex {
@ -773,7 +781,13 @@ func main() {
} }
} else { } else {
for i, c := range test_runes { 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) color.New(color.FgRed).Fprintf(out, "%c", c)
// Newline after every match - only if -o is enabled and -v is disabled. // Newline after every match - only if -o is enabled and -v is disabled.
if *onlyFlag && !(*invertFlag) { if *onlyFlag && !(*invertFlag) {

Loading…
Cancel
Save