Better handling of NFD chars
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 84921df · Browse files · 3 files·+30 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−25 removed
- tests:+9 added−7 removed
- other:+21 added−18 removed
Provenance
- Commit
423e26b- Tree
101708a- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
84921df
3 files changed+30 added−25 removed
modified:test/test_fzf.rb+9 added-7 removed
291 unmodified lines292 293 def test_nfd294 nfc = '한글'295 nfd = FZF::UConv.nfd(nfc)296- assert_equal 6, nfd.length297- assert_equal NFD, nfd296+ assert_equal 2, nfd.length297+ assert_equal 6, nfd.join.length298+ assert_equal NFD, nfd.join299 end300 301 def test_nfd_fuzzy_matcher302 matcher = FZF::FuzzyMatcher.new 0303+ assert_equal [], matcher.match([NFD + NFD], '할', '', '')302- match = matcher.match([NFD], '할', '', '')303- assert_equal [[NFD, [[0, 6]]]], match304- assert_equal ['한글', [[0, 2]]], FZF::UConv.nfc(*match.first)304+ match = matcher.match([NFD + NFD], '글글', '', '')305+ assert_equal [[NFD + NFD, [[3, 12]]]], match306+ assert_equal ['한글한글', [[1, 4]]], FZF::UConv.nfc(*match.first)307 end308 309 def test_nfd_extended_fuzzy_matcher310 matcher = FZF::ExtendedFuzzyMatcher.new 0309- assert_equal [], matcher.match([NFD], "'할", '', '')310- match = matcher.match([NFD], "'한글", '', '')311+ assert_equal [], matcher.match([NFD], "'글글", '', '')312+ match = matcher.match([NFD], "'한글", '', '')313 assert_equal [[NFD, [[0, 6]]]], match314 assert_equal ['한글', [[0, 2]]], FZF::UConv.nfc(*match.first)315 end316 end7 unmodified linesmodified:fzf+20 added-17 removed
135 unmodified lines136 NFC_BEGIN = 0xAC00137 NFC_END = NFC_BEGIN + CHOSUNGS * JUNGSUNGS * JONGSUNGS138 139 def self.nfd str140- ret = ''141- str.split(//).each do |c|140+ str.split(//).map do |c|141 cp = c.ord142 if cp >= NFC_BEGIN && cp < NFC_END143+ chr = ''144 idx = cp - NFC_BEGIN145 cho = CHOSUNG + idx / JJCOUNT146 jung = JUNGSUNG + (idx % JJCOUNT) / JONGSUNGS147 jong = JONGSUNG + idx % JONGSUNGS148- ret << cho << jung149- ret << jong if jong != JONGSUNG148+ chr << cho << jung149+ chr << jong if jong != JONGSUNG150+ chr151 else151- ret << c152+ c153 end154 end154- ret155 end156 157 def self.to_nfc arr158 [NFC_BEGIN + arr[0] * JJCOUNT +55 unmodified lines214 end215 216 class Matcher217 def query_chars q218- UConv.nfd(q).split(//)218+ UConv.nfd(q)219 end220- end221- else222- module UConv223- def self.nfd str224- str220+221+ def sanitize q222+ UConv.nfd(q).join223 end224 end227-225+ else226 def convert_item item227 item228 end229 230 class Matcher231 def query_chars q232 q.split(//)233 end234+235+ def sanitize q236+ q237+ end238 end239 end240 241 def emit event514 unmodified lines756 @regexp[q] ||= begin757 q = q.downcase if @rxflag != 0758 Regexp.new(query_chars(q).inject('') { |sum, e|759 e = Regexp.escape e758- sum << "#{e}[^#{e}]*?"760+ sum << (e.length > 1 ? "(?:#{e}).*?" : # FIXME: not equivalent761+ "#{e}[^#{e}]*?")762 }, @rxflag)763 end764 end765 46 unmodified lines812 when ''813 nil814 when /^'/815 w.length > 1 ?813- Regexp.new(UConv.nfd(Regexp.escape(w[1..-1])), rxflag) : nil816+ Regexp.new(sanitize(Regexp.escape(w[1..-1])), rxflag) : nil817 when /^\^/818 w.length > 1 ?816- Regexp.new('^' << UConv.nfd(Regexp.escape(w[1..-1])), rxflag) : nil819+ Regexp.new('^' << sanitize(Regexp.escape(w[1..-1])), rxflag) : nil820 when /\$$/821 w.length > 1 ?819- Regexp.new(UConv.nfd(Regexp.escape(w[0..-2])) << '$', rxflag) : nil822+ Regexp.new(sanitize(Regexp.escape(w[0..-2])) << '$', rxflag) : nil823 else824 fuzzy_regex w825 end, invert ]826 }.select { |pair| pair.first }32 unmodified linesmodified:fzf.gemspec+1 added-1 removed
1 # coding: utf-82 Gem::Specification.new do |spec|3 spec.name = 'fzf'4- spec.version = '0.4.1'4+ spec.version = '0.4.2'5 spec.authors = ['Junegunn Choi']6 spec.email = ['junegunn.c@gmail.com']7 spec.description = %q{Fuzzy finder for your shell}8 spec.summary = %q{Fuzzy finder for your shell}7 unmodified lines