From 2d00abc7cb194c42b26016b3159a8cbee1f1ed33 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Fri, 15 Aug 2014 03:02:07 +0900 Subject: [PATCH] Improve performance of `--nth` option (#90) --- fzf | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/fzf b/fzf index 6063585..935944d 100755 --- a/fzf +++ b/fzf @@ -7,7 +7,7 @@ # / __/ / /_/ __/ # /_/ /___/_/ Fuzzy finder for your shell # -# Version: 0.8.7 (Jul 27, 2014) +# Version: 0.8.7 (Aug 15, 2014) # # Author: Junegunn Choi # URL: https://github.com/junegunn/fzf @@ -1102,26 +1102,29 @@ class FZF def tokenize str @tokens_cache[str] ||= - unless @delim - # AWK default - prefix_length = str[/^\s+/].length rescue 0 - [prefix_length, (str.strip.scan(/\S+\s*/) rescue [])] - else - prefix_length = 0 - [prefix_length, (str.scan(@delim) rescue [])] + begin + unless @delim + # AWK default + prefix_length = str.index(/\S/) || 0 + tokens = str.strip.scan(/\S+\s*/) rescue [] + else + prefix_length = 0 + tokens = str.scan(@delim) rescue [] + end + @nth.map { |n| + (part = tokens[n]) && + [prefix_length + (tokens[0...(n.begin)] || []).join.length, + part.join.sub(/\s+$/, '')] + }.compact end end def do_match str, pat if @nth - prefix_length, tokens = tokenize str - - @nth.each do |n| - if (range = tokens[n]) && (token = range.join) && - (md = token.sub(/\s+$/, '').match(pat) rescue nil) - prefix_length += (tokens[0...(n.begin)] || []).join.length - offset = md.offset(0).map { |o| o + prefix_length } - return MatchData.new(offset) + tokenize(str).each do |pair| + prefix_length, token = pair + if md = token.match(pat) rescue nil + return MatchData.new(md.offset(0).map { |o| o + prefix_length }) end end nil