Made all compiled regex a field of TodoFinder

This prevents the application from needing to constantly recompiling the same regex patterns over and over
This commit is contained in:
Samuel Hawksby-Robinson 2021-01-06 16:59:11 +00:00
parent b397566aaf
commit 9792ef33d2
No known key found for this signature in database
GPG Key ID: 64CF99D4A64A1205
2 changed files with 32 additions and 20 deletions

View File

@ -15,8 +15,12 @@ const (
func main() {
tf := NewTodoFinder()
err := tf.Init()
if err != nil {
panic(err)
}
err := tf.FindInDir(statusDir)
err = tf.FindInDir(statusDir)
if err != nil {
panic(err)
}

View File

@ -16,6 +16,10 @@ type node struct {
}
type TodoFinder struct {
entityTracker *entityTracker
todoRegex *regexp.Regexp
lineRegex *regexp.Regexp
FoundTable []*todo
foundTree *node
@ -33,27 +37,31 @@ func NewTodoFinder() TodoFinder {
}
}
func (tf *TodoFinder) Init() (err error) {
tf.todoRegex, err = regexp.Compile(tf.buildRegexPattern())
if err != nil {
return err
}
tf.lineRegex, err = regexp.Compile("\n")
if err != nil {
return err
}
tf.entityTracker, err = NewEntityTracker()
if err != nil {
return err
}
return nil
}
func (tf *TodoFinder) AddTodo(t *todo) {
tf.FoundTable = append(tf.FoundTable, t)
tf.foundTree.AddToTree(t.Path(), t)
}
func (tf *TodoFinder) FindInDir(dir string) error {
r, err := regexp.Compile(tf.buildRegexPattern())
if err != nil {
return err
}
lineSplitter, err := regexp.Compile("\n")
if err != nil {
return err
}
et, err := NewEntityTracker()
if err != nil {
return err
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
@ -82,11 +90,11 @@ func (tf *TodoFinder) FindInDir(dir string) error {
return err
}
lines := lineSplitter.Split(string(file), -1)
lines := tf.lineRegex.Split(string(file), -1)
for i, l := range lines {
et.Track(l)
tf.entityTracker.Track(l)
results := r.FindSubmatch([]byte(l))
results := tf.todoRegex.FindSubmatch([]byte(l))
if results == nil {
if len(l) < 3 {
tf.openTodo = nil
@ -107,7 +115,7 @@ func (tf *TodoFinder) FindInDir(dir string) error {
Filepath: filepath,
Description: string(results[1]),
LineNumber: i + 1,
RelatedFuncOrType: et.Current(),
RelatedFuncOrType: tf.entityTracker.Current(),
}
tf.AddTodo(td)
tf.openTodo = td