From bd98f988f0502251d13650afddde1916ad87d28d Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 21 Jul 2017 17:29:14 +0900 Subject: [PATCH] Further reduce unnecessary rune array conversion I was too quick to release 0.16.9, this commit makes --ansi processing even faster. --- src/algo/algo_test.go | 2 +- src/core.go | 4 ++-- src/merger_test.go | 2 +- src/pattern_test.go | 6 +++--- src/terminal.go | 5 ++--- src/terminal_test.go | 2 +- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/algo/algo_test.go b/src/algo/algo_test.go index df8b227..2da0b3c 100644 --- a/src/algo/algo_test.go +++ b/src/algo/algo_test.go @@ -17,7 +17,7 @@ func assertMatch2(t *testing.T, fun Algo, caseSensitive, normalize, forward bool if !caseSensitive { pattern = strings.ToLower(pattern) } - res, pos := fun(caseSensitive, normalize, forward, util.RunesToChars([]rune(input)), []rune(pattern), true, nil) + res, pos := fun(caseSensitive, normalize, forward, util.ToChars([]byte(input)), []rune(pattern), true, nil) var start, end int if pos == nil || len(*pos) == 0 { start = res.Start diff --git a/src/core.go b/src/core.go index 3e60934..74ff9e7 100644 --- a/src/core.go +++ b/src/core.go @@ -69,14 +69,14 @@ func Run(opts *Options, revision string) { ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) { trimmed, offsets, newState := extractColor(string(data), state, nil) state = newState - return util.RunesToChars([]rune(trimmed)), offsets + return util.ToChars([]byte(trimmed)), offsets } } else { // When color is disabled but ansi option is given, // we simply strip out ANSI codes from the input ansiProcessor = func(data []byte) (util.Chars, *[]ansiOffset) { trimmed, _, _ := extractColor(string(data), nil, nil) - return util.RunesToChars([]rune(trimmed)), nil + return util.ToChars([]byte(trimmed)), nil } } } diff --git a/src/merger_test.go b/src/merger_test.go index b98aca8..c6af4f6 100644 --- a/src/merger_test.go +++ b/src/merger_test.go @@ -17,7 +17,7 @@ func assert(t *testing.T, cond bool, msg ...string) { func randResult() Result { str := fmt.Sprintf("%d", rand.Uint32()) - chars := util.RunesToChars([]rune(str)) + chars := util.ToChars([]byte(str)) chars.Index = rand.Int31() return Result{item: &Item{text: chars}} } diff --git a/src/pattern_test.go b/src/pattern_test.go index 85c174c..81d158a 100644 --- a/src/pattern_test.go +++ b/src/pattern_test.go @@ -78,7 +78,7 @@ func TestExact(t *testing.T) { pattern := BuildPattern(true, algo.FuzzyMatchV2, true, CaseSmart, false, true, true, []Range{}, Delimiter{}, []rune("'abc")) res, pos := algo.ExactMatchNaive( - pattern.caseSensitive, pattern.normalize, pattern.forward, util.RunesToChars([]rune("aabbcc abc")), pattern.termSets[0][0].text, true, nil) + pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte("aabbcc abc")), pattern.termSets[0][0].text, true, nil) if res.Start != 7 || res.End != 10 { t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) } @@ -94,7 +94,7 @@ func TestEqual(t *testing.T) { match := func(str string, sidxExpected int, eidxExpected int) { res, pos := algo.EqualMatch( - pattern.caseSensitive, pattern.normalize, pattern.forward, util.RunesToChars([]rune(str)), pattern.termSets[0][0].text, true, nil) + pattern.caseSensitive, pattern.normalize, pattern.forward, util.ToChars([]byte(str)), pattern.termSets[0][0].text, true, nil) if res.Start != sidxExpected || res.End != eidxExpected { t.Errorf("%s / %d / %d", pattern.termSets, res.Start, res.End) } @@ -140,7 +140,7 @@ func TestOrigTextAndTransformed(t *testing.T) { for _, extended := range []bool{false, true} { chunk := Chunk{ Item{ - text: util.RunesToChars([]rune("junegunn")), + text: util.ToChars([]byte("junegunn")), origText: &origBytes, transformed: &trans}, } diff --git a/src/terminal.go b/src/terminal.go index 8d0b6bf..d6d2155 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -712,7 +712,7 @@ func (t *Terminal) printHeader() { trimmed, colors, newState := extractColor(lineStr, state, nil) state = newState item := &Item{ - text: util.RunesToChars([]rune(trimmed)), + text: util.ToChars([]byte(trimmed)), colors: colors} t.move(line, 2, true) @@ -1173,8 +1173,7 @@ func replacePlaceholder(template string, stripAnsi bool, delimiter Delimiter, fo } for idx, item := range items { - chars := util.RunesToChars([]rune(item.AsString(stripAnsi))) - tokens := Tokenize(chars.ToString(), delimiter) + tokens := Tokenize(item.AsString(stripAnsi), delimiter) trans := Transform(tokens, ranges) str := string(joinTokens(trans)) if delimiter.str != nil { diff --git a/src/terminal_test.go b/src/terminal_test.go index 41941ee..d42d2b8 100644 --- a/src/terminal_test.go +++ b/src/terminal_test.go @@ -10,7 +10,7 @@ import ( func newItem(str string) *Item { bytes := []byte(str) trimmed, _, _ := extractColor(str, nil, nil) - return &Item{origText: &bytes, text: util.RunesToChars([]rune(trimmed))} + return &Item{origText: &bytes, text: util.ToChars([]byte(trimmed))} } func TestReplacePlaceholder(t *testing.T) {