Skip to content
Myr

Fix --with-nth performance; use simpler regular expression

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Related #317

authored by Junegunn Choi · committed by Junegunn Choi · parent 766427d · Browse files · 5 files+33 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+33 added−15 removed

  • other:+33 added−15 removed

5 files+33 added−15 removed

modified:src/options_test.go+13 added-9 removed
1package fzf23import (4 "fmt"5 "strings"5 "testing"67 "github.com/junegunn/fzf/src/curses"8)
10 unmodified lines
19 t.Error(delim)20 }21 // Valid regex22 delim = delimiterRegexp("[0-9]")24 if strings.Index(delim.regex.String(), "[0-9]") < 0 || delim.str != nil {23 if delim.regex.String() != "[0-9]" || delim.str != nil {24 t.Error(delim)25 }26 // Tab character27 delim = delimiterRegexp("\t")
13 unmodified lines
41}4243func TestDelimiterRegexString(t *testing.T) {44 delim := delimiterRegexp("*")46 tokens := strings.Split("-*--*---**---", *delim.str)47 if delim.regex != nil || tokens[0] != "-" || tokens[1] != "--" ||48 tokens[2] != "---" || tokens[3] != "" || tokens[4] != "---" {45 tokens := Tokenize([]rune("-*--*---**---"), delim)46 if delim.regex != nil ||47 string(tokens[0].text) != "-*" ||48 string(tokens[1].text) != "--*" ||49 string(tokens[2].text) != "---*" ||50 string(tokens[3].text) != "*" ||51 string(tokens[4].text) != "---" {52 t.Errorf("%s %s %d", delim, tokens, len(tokens))53 }54}5556func TestDelimiterRegexRegex(t *testing.T) {57 delim := delimiterRegexp("--\\*")55 rx := delim.regex56 tokens := rx.FindAllString("-*--*---**---", -1)58 tokens := Tokenize([]rune("-*--*---**---"), delim)59 if delim.str != nil ||58 tokens[0] != "-*--*" || tokens[1] != "---*" || tokens[2] != "*---" {59 t.Errorf("%s %s %d", rx, tokens, len(tokens))60 string(tokens[0].text) != "-*--*" ||61 string(tokens[1].text) != "---*" ||62 string(tokens[2].text) != "*---" {63 t.Errorf("%s %d", tokens, len(tokens))64 }65}6667func TestSplitNth(t *testing.T) {
251 unmodified lines
modified:src/options.go-5 removed
1package fzf23import (4 "fmt"4 "io/ioutil"5 "os"6 "regexp"7 "strconv"
274 unmodified lines
282 return Delimiter{str: &str}283 }284285 // 3. Pattern as regular expression. Slow.287 rx, e = regexp.Compile(fmt.Sprintf("(?:.*?%s)|(?:.+?$)", str))288 if e != nil {289 errorExit("invalid regular expression: " + e.Error())290 }286 return Delimiter{regex: rx}287}288289func isAlphabet(char uint8) bool {
613 unmodified lines
modified:src/tokenizer_test.go+9 added
51 unmodified lines
52 tokens = Tokenize([]rune(input), delimiterRegexp(":"))53 if string(tokens[0].text) != " abc:" || tokens[0].prefixLength != 0 {54 t.Errorf("%s", tokens)55 }5657 // With delimiter regex58 tokens = Tokenize([]rune(input), delimiterRegexp("\\s+"))59 if string(tokens[0].text) != " " || tokens[0].prefixLength != 0 ||60 string(tokens[1].text) != "abc: " || tokens[1].prefixLength != 2 ||61 string(tokens[2].text) != "def: " || tokens[2].prefixLength != 8 ||62 string(tokens[3].text) != "ghi " || tokens[3].prefixLength != 14 {63 t.Errorf("%s", tokens)64 }65}6667func TestTransform(t *testing.T) {68 input := " abc: def: ghi: jkl"
42 unmodified lines
modified:src/tokenizer.go+10 added-1 removed
143 unmodified lines
144 for i := 0; i < len(tokens)-1; i++ {145 tokens[i] = tokens[i] + *delimiter.str146 }147 } else if delimiter.regex != nil {148 tokens = delimiter.regex.FindAllString(string(runes), -1)148 str := string(runes)149 for len(str) > 0 {150 loc := delimiter.regex.FindStringIndex(str)151 if loc == nil {152 loc = []int{0, len(str)}153 }154 last := util.Max(loc[1], 1)155 tokens = append(tokens, str[:last])156 str = str[last:]157 }158 }159 asRunes := make([][]rune, len(tokens))160 for i, token := range tokens {161 asRunes[i] = []rune(token)
78 unmodified lines
modified:CHANGELOG.md+1 added
7 unmodified lines
8 - Regular expression engine of Golang as of now is very slow, so the fixed9 version will treat the given delimiter pattern as a plain string instead10 of a regular expression unless it contains special characters and is11 a valid regular expression.12 - Simpler regular expression for delimiter for better performance13140.10.215------16
265 unmodified lines