chore(inputfield): Compile regex only once

This commit is contained in:
Andreas Bieber 2020-09-16 16:51:02 +02:00
parent e7aaa8402f
commit 32ed5f11d7
1 changed files with 8 additions and 4 deletions

View File

@ -609,10 +609,10 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p
})
}
moveWordLeft := func() {
i.cursorPos = len(regexp.MustCompile(`(\w*|\W)$`).ReplaceAllString(i.text[:i.cursorPos], ""))
i.cursorPos = len(regexRightWord.ReplaceAllString(i.text[:i.cursorPos], ""))
}
moveWordRight := func() {
i.cursorPos = len(i.text) - len(regexp.MustCompile(`^(\W|\w*)`).ReplaceAllString(i.text[i.cursorPos:], ""))
i.cursorPos = len(i.text) - len(regexLeftWord.ReplaceAllString(i.text[i.cursorPos:], ""))
}
// Add character function. Returns whether or not the rune character is
@ -670,8 +670,7 @@ func (i *InputField) InputHandler() func(event *tcell.EventKey, setFocus func(p
case tcell.KeyCtrlK: // Delete until the end of the line.
i.text = i.text[:i.cursorPos]
case tcell.KeyCtrlW: // Delete last word.
lastWord := regexp.MustCompile(`(\w*|\W)$`)
newText := lastWord.ReplaceAllString(i.text[:i.cursorPos], "") + i.text[i.cursorPos:]
newText := regexRightWord.ReplaceAllString(i.text[:i.cursorPos], "") + i.text[i.cursorPos:]
i.cursorPos -= len(i.text) - len(newText)
i.text = newText
case tcell.KeyBackspace, tcell.KeyBackspace2: // Delete character before the cursor.
@ -780,3 +779,8 @@ func (i *InputField) MouseHandler() func(action MouseAction, event *tcell.EventM
return
})
}
var (
regexRightWord = regexp.MustCompile(`(\w*|\W)$`)
regexLeftWord = regexp.MustCompile(`^(\W|\w*)`)
)