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
Provenance
- Commit
5c25984- Tree
f73b7dd- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
319d6ce
6 files changed+50 added−17 removed
modified:src/algo/algo.go+25 added-12 removed
1 package algo2 3-import "strings"3+import (4+ "strings"5+ "unicode"6+)7 8 /*9 * String matching algorithms here do not use strings.ToLower to avoid10 * performance penalty. And they assume pattern runes are given in lowercase25 unmodified lines36 37 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 = index8 unmodified lines63 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 break47 unmodified lines118 119 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 lines147 }148 149 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 lines168 }169 170 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 linesmodified:src/constants.go+1 added-1 removed
3 unmodified lines4 "github.com/junegunn/fzf/src/util"5 )6 7 // Current version8-const Version = "0.9.7"8+const Version = "0.9.8"9 10 // fzf events11 const (12 EvtReadNew util.EventType = iota6 unmodified linesmodified:src/pattern.go+9 added-3 removed
2 unmodified lines3 import (4 "regexp"5 "sort"6 "strings"7+ "unicode"8 9 "github.com/junegunn/fzf/src/algo"10 )11 11-const uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"12-12 // fuzzy13 // 'exact14 // ^exact-prefix15 // exact-suffix$73 unmodified lines89 terms := []term{}90 91 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)), false227 unmodified linesmodified:test/test_go.rb+7 added
468 unmodified lines469 tmux.until { |lines| lines[-2].include? '4/111 (2)' }470 tmux.send_keys :Enter471 assert_equal ['111', '11'], readonce.split($/)472 end473+474+ 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+ end480 end481 482 module TestShell483 def setup155 unmodified linesmodified:CHANGELOG.md+7 added
1234567891011121314115 unmodified linesExpand allCHANGELOG=========
0.9.8-----
### Bug fixes
- Fixed Unicode case handling (#186)
0.9.7-----
### New features115 unmodified linesExpand allmodified:install+1 added-1 removed
1 #!/usr/bin/env bash2 3-version=0.9.73+version=0.9.84 5 cd $(dirname $BASH_SOURCE)6 fzf_base=$(pwd)7 268 unmodified lines