Prevent recompilation of regexes

This commit is contained in:
Marcel Schramm 2019-02-11 17:01:48 +01:00
parent a45c8edf60
commit f4690e43a0
No known key found for this signature in database
GPG Key ID: 05971054C70EEDC7
1 changed files with 10 additions and 7 deletions

View File

@ -13,7 +13,13 @@ import (
)
// TabSize is the number of spaces with which a tab character will be replaced.
var TabSize = 4
var (
openColorRegex = regexp.MustCompile(`\[([a-zA-Z]*|#[0-9a-zA-Z]*)$`)
openRegionRegex = regexp.MustCompile(`\["[a-zA-Z0-9_,;: \-\.]*"?$`)
newLineRegex = regexp.MustCompile(`\r?\n`)
TabSize = 4
)
// textViewIndex contains information about each line displayed in the text
// view.
@ -497,8 +503,7 @@ func (t *TextView) Write(p []byte) (n int, err error) {
// If we have a trailing open dynamic color, exclude it.
if t.dynamicColors {
openColor := regexp.MustCompile(`\[([a-zA-Z]*|#[0-9a-zA-Z]*)$`)
location := openColor.FindIndex(newBytes)
location := openColorRegex.FindIndex(newBytes)
if location != nil {
t.recentBytes = newBytes[location[0]:]
newBytes = newBytes[:location[0]]
@ -507,8 +512,7 @@ func (t *TextView) Write(p []byte) (n int, err error) {
// If we have a trailing open region, exclude it.
if t.regions {
openRegion := regexp.MustCompile(`\["[a-zA-Z0-9_,;: \-\.]*"?$`)
location := openRegion.FindIndex(newBytes)
location := openRegionRegex.FindIndex(newBytes)
if location != nil {
t.recentBytes = newBytes[location[0]:]
newBytes = newBytes[:location[0]]
@ -516,9 +520,8 @@ func (t *TextView) Write(p []byte) (n int, err error) {
}
// Transform the new bytes into strings.
newLine := regexp.MustCompile(`\r?\n`)
newBytes = bytes.Replace(newBytes, []byte{'\t'}, bytes.Repeat([]byte{' '}, TabSize), -1)
for index, line := range newLine.Split(string(newBytes), -1) {
for index, line := range newLineRegex.Split(string(newBytes), -1) {
if index == 0 {
if len(t.buffer) == 0 {
t.buffer = []string{line}