2 Commits

Author SHA1 Message Date
3fa4d0f75e Updated TODO 2025-01-03 19:18:00 -05:00
6f9173f771 Finished support for -m flag; refactoring pending 2025-01-03 19:17:24 -05:00
2 changed files with 22 additions and 7 deletions

22
main.go
View File

@@ -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) {

View File

@@ -4,3 +4,4 @@
Ideas for flags:
-m <num> : Print <num>th match (-m 1 = first match, -m 2 = second match)
-g <num> : Print the <num>th group
4. Refactor code for flags - make each flag's code a function, which modifies the result of findAllMatches