|
|
@ -375,9 +375,15 @@ func main() {
|
|
|
|
// This flag has two 'modes':
|
|
|
|
// This flag has two 'modes':
|
|
|
|
// 1. Without '-v': Prints only matches. Prints a newline after every match.
|
|
|
|
// 1. Without '-v': Prints only matches. Prints a newline after every match.
|
|
|
|
// 2. With '-v': Substitutes all matches with empty string.
|
|
|
|
// 2. With '-v': Substitutes all matches with empty string.
|
|
|
|
onlyFlag := flag.Bool("o", false, "Print only colored content.")
|
|
|
|
onlyFlag := flag.Bool("o", false, "Print only colored content. Overrides -l.")
|
|
|
|
|
|
|
|
lineFlag := flag.Bool("l", false, "Only print lines with a match (or with no matches, if -v is enabled")
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -l and -o are mutually exclusive: -o overrides -l
|
|
|
|
|
|
|
|
if *onlyFlag {
|
|
|
|
|
|
|
|
*lineFlag = false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Process:
|
|
|
|
// Process:
|
|
|
|
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
|
|
|
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
|
|
|
// a. Add explicit concatenation operators to facilitate this
|
|
|
|
// a. Add explicit concatenation operators to facilitate this
|
|
|
@ -392,7 +398,7 @@ func main() {
|
|
|
|
re = flag.Args()[0]
|
|
|
|
re = flag.Args()[0]
|
|
|
|
var test_str string
|
|
|
|
var test_str string
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
// Create reader for stdin and writer for stdout
|
|
|
|
// Create reader for stdin and writer for stdout // End index is one more than last index of match
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
out := bufio.NewWriter(os.Stdout)
|
|
|
|
out := bufio.NewWriter(os.Stdout)
|
|
|
|
|
|
|
|
|
|
|
@ -419,11 +425,21 @@ func main() {
|
|
|
|
indicesToPrint.add(setDifference(genRange(0, len(test_str)), oldIndices)...)
|
|
|
|
indicesToPrint.add(setDifference(genRange(0, len(test_str)), oldIndices)...)
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If lineFlag is enabled, we should only print something if:
|
|
|
|
|
|
|
|
// a. We are not inverting, and have at least one match on the current line
|
|
|
|
|
|
|
|
// OR
|
|
|
|
|
|
|
|
// b. We are inverting, and have no matches at all on the current line.
|
|
|
|
|
|
|
|
// This checks for the inverse, and continues if it is true.
|
|
|
|
|
|
|
|
if *lineFlag {
|
|
|
|
|
|
|
|
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
for i, c := range test_str {
|
|
|
|
for i, c := range test_str {
|
|
|
|
if indicesToPrint.contains(i) {
|
|
|
|
if indicesToPrint.contains(i) {
|
|
|
|
color.New(color.FgRed).Fprintf(out, "%c", c)
|
|
|
|
color.New(color.FgRed).Fprintf(out, "%c", c)
|
|
|
|
// Newline after every match - only if -v is disabled.
|
|
|
|
// Newline after every match - only if -o is enabled and -v is disabled.
|
|
|
|
if !(*invertFlag) {
|
|
|
|
if *onlyFlag && !(*invertFlag) {
|
|
|
|
for _, idx := range matchIndices {
|
|
|
|
for _, idx := range matchIndices {
|
|
|
|
if i+1 == idx.endIdx { // End index is one more than last index of match
|
|
|
|
if i+1 == idx.endIdx { // End index is one more than last index of match
|
|
|
|
fmt.Fprintf(out, "\n")
|
|
|
|
fmt.Fprintf(out, "\n")
|
|
|
|