Moved some auxiliary functions into compile.go; use new API for compiling and finding matches

master
Aadhavan Srinivasan 2 weeks ago
parent 1da3f7f0e0
commit 24fa365be1

@ -6,29 +6,13 @@ import (
"fmt"
"io"
"os"
"slices"
"github.com/fatih/color"
)
const CONCAT rune = '~'
var notDotChars []rune
var caseInsensitiveFlag *bool // Whether we are running in case-insensitive mode
func isOperator(c rune) bool {
if c == '+' || c == '?' || c == '*' || c == '|' || c == CONCAT {
return true
}
return false
}
/* priority returns the priority of the given operator */
func priority(op rune) int {
precedence := []rune{'|', CONCAT, '+', '*', '?'}
return slices.Index(precedence, op)
}
func main() {
invertFlag := flag.Bool("v", false, "Invert match.")
// This flag has two 'modes':
@ -83,7 +67,6 @@ func main() {
var re string
re = flag.Args()[0]
var test_str string
var test_runes []rune // Rune-slice representation of test_str
var err error
var linesRead bool // Whether or not we have read the lines in the file
lineNum := 0 // Current line number
@ -91,8 +74,11 @@ func main() {
reader := bufio.NewReader(os.Stdin)
out := bufio.NewWriter(os.Stdout)
re_postfix := shuntingYard(re)
startState, numGroups := thompson(re_postfix)
regComp, err := Compile(re)
if err != nil {
fmt.Println(err)
return
}
for true {
if linesRead {
break
@ -129,15 +115,14 @@ func main() {
panic(err)
}
}
test_runes = []rune(test_str)
matchIndices := make([]Match, 0)
if matchNumFlagEnabled {
tmp, err := findNthMatch(startState, test_runes, numGroups, *matchNum)
tmp, err := findNthMatch(regComp, test_str, *matchNum)
if err == nil {
matchIndices = append(matchIndices, tmp)
}
} else {
matchIndices = findAllMatches(startState, test_runes, numGroups)
matchIndices = findAllMatches(regComp, test_str)
}
if *printMatchesFlag {
@ -172,7 +157,7 @@ func main() {
// Find all numbers from 0 to len(test_str) that are NOT in oldIndices.
// These are the values we want to print, now that we have inverted the match.
// Re-initialize indicesToPrint and add all of these values to it.
indicesToPrint.add(setDifference(genRange(0, len(test_runes)), oldIndices)...)
indicesToPrint.add(setDifference(genRange(0, len(test_str)), oldIndices)...)
}
// If lineFlag is enabled, we should only print something if:
@ -193,7 +178,7 @@ func main() {
// the corresponding end index.
// 3. If not, just print the character.
if substituteFlagEnabled {
for i := range test_runes {
for i := range test_str {
inMatchIndex := false
for _, m := range matchIndices {
if i == m[0].startIdx {
@ -204,11 +189,11 @@ func main() {
}
}
if !inMatchIndex {
fmt.Fprintf(out, "%c", test_runes[i])
fmt.Fprintf(out, "%c", test_str[i])
}
}
} else {
for i, c := range test_runes {
for i, c := range test_str {
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.

Loading…
Cancel
Save