Skip to content
Myr

Fix Unicode case handling (#186)

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

authored by Junegunn Choi · committed by Junegunn Choi · parent 319d6ce · Browse files · 6 files+50 added−17 removed
This commit

State

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

Diff stats

6 files+50 added−17 removed

  • tests:+7 added−0 removed
  • other:+43 added−17 removed

6 files+50 added−17 removed

modified:src/algo/algo.go+25 added-12 removed
1package algo23import "strings"3import (4 "strings"5 "unicode"6)78/*9 * String matching algorithms here do not use strings.ToLower to avoid10 * performance penalty. And they assume pattern runes are given in lowercase
25 unmodified lines
3637 for index, char := range runes {38 // This is considerably faster than blindly applying strings.ToLower to the39 // whole string37 if !caseSensitive && char >= 65 && char <= 90 {38 char += 3240 if !caseSensitive {41 // Partially inlining `unicode.ToLower`. Ugly, but makes a noticeable42 // difference in CPU cost. (Measured on Go 1.4.1. Also note that the Go43 // compiler as of now does not inline non-leaf functions.)44 if char >= 'A' && char <= 'Z' {45 char += 3246 runes[index] = char47 } else if char > unicode.MaxASCII {48 char = unicode.To(unicode.LowerCase, char)49 runes[index] = char50 }51 }52 if char == pattern[pidx] {53 if sidx < 0 {54 sidx = index
8 unmodified lines
63 if sidx >= 0 && eidx >= 0 {64 pidx--65 for index := eidx - 1; index >= sidx; index-- {66 char := runes[index]55 if !caseSensitive && char >= 65 && char <= 90 {56 char += 3257 }67 if char == pattern[pidx] {68 if pidx--; pidx < 0 {69 sidx = index70 break
47 unmodified lines
118119 pidx := 0120 for index := 0; index < numRunes; index++ {121 char := runes[index]113 if !caseSensitive && char >= 65 && char <= 90 {114 char += 32122 if !caseSensitive {123 if char >= 'A' && char <= 'Z' {124 char += 32125 } else if char > unicode.MaxASCII {126 char = unicode.To(unicode.LowerCase, char)127 }128 }129 if pattern[pidx] == char {130 pidx++131 if pidx == plen {
15 unmodified lines
147 }148149 for index, r := range pattern {150 char := runes[index]138 if !caseSensitive && char >= 65 && char <= 90 {139 char += 32151 if !caseSensitive {152 char = unicode.ToLower(char)153 }154 if char != r {155 return -1, -1156 }
11 unmodified lines
168 }169170 for index, r := range pattern {171 char := runes[index+diff]159 if !caseSensitive && char >= 65 && char <= 90 {160 char += 32172 if !caseSensitive {173 char = unicode.ToLower(char)174 }175 if char != r {176 return -1, -1177 }
3 unmodified lines
modified:src/constants.go+1 added-1 removed
3 unmodified lines
4 "github.com/junegunn/fzf/src/util"5)67// Current version8const Version = "0.9.7"8const Version = "0.9.8"910// fzf events11const (12 EvtReadNew util.EventType = iota
6 unmodified lines
modified:src/pattern.go+9 added-3 removed
2 unmodified lines
3import (4 "regexp"5 "sort"6 "strings"7 "unicode"89 "github.com/junegunn/fzf/src/algo"10)1111const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"1212// fuzzy13// 'exact14// ^exact-prefix15// exact-suffix$
73 unmodified lines
89 terms := []term{}9091 switch caseMode {92 case CaseSmart:94 if !strings.ContainsAny(asString, uppercaseLetters) {93 hasUppercase := false94 for _, r := range runes {95 if unicode.IsUpper(r) {96 hasUppercase = true97 break98 }99 }100 if !hasUppercase {101 runes, caseSensitive = []rune(strings.ToLower(asString)), false102 }103 case CaseIgnore:104 runes, caseSensitive = []rune(strings.ToLower(asString)), false
227 unmodified lines
modified:test/test_go.rb+7 added
468 unmodified lines
469 tmux.until { |lines| lines[-2].include? '4/111 (2)' }470 tmux.send_keys :Enter471 assert_equal ['111', '11'], readonce.split($/)472 end473474 def test_unicode_case475 assert_equal %w[СТРОКА2 Строка4],476 `printf "строКА1\\nСТРОКА2\\nстрока3\\nСтрока4" | fzf -fС`.split($/)477 assert_equal %w[строКА1 СТРОКА2 строка3 Строка4],478 `printf "строКА1\\nСТРОКА2\\nстрока3\\nСтрока4" | fzf -fс`.split($/)479 end480end481482module TestShell483 def setup
155 unmodified lines
modified:CHANGELOG.md+7 added
modified:install+1 added-1 removed
1#!/usr/bin/env bash23version=0.9.73version=0.9.845cd $(dirname $BASH_SOURCE)6fzf_base=$(pwd)7
268 unmodified lines