Browse Source

chore(inputfield): Compile regex only once

tablepad
Andreas Bieber 2 years ago
parent
commit
32ed5f11d7
  1. 12
      inputfield.go

12
inputfield.go

@ -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*)`)
)

Loading…
Cancel
Save