From 6b18b144cf0a608433009515cddef74737bfce56 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 10 Aug 2017 12:40:53 +0900 Subject: [PATCH] Fix escaping of meta characters after ' or ! prefix https://github.com/junegunn/fzf/issues/444#issuecomment-321432803 --- src/pattern.go | 8 +++----- test/test_go.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/pattern.go b/src/pattern.go index c98f8ddc..00e3a3a6 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -182,7 +182,9 @@ func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet } } - if strings.HasPrefix(text, "'") { + if _escapedPrefixRegex.MatchString(text) { + text = text[1:] + } else if strings.HasPrefix(text, "'") { // Flip exactness if fuzzy && !inv { typ = termExact @@ -200,10 +202,6 @@ func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet text = text[1:] } - if _escapedPrefixRegex.MatchString(text) { - text = text[1:] - } - if len(text) > 0 { if switchSet { sets = append(sets, set) diff --git a/test/test_go.rb b/test/test_go.rb index 3460f56b..f8bd6b0c 100644 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -1400,6 +1400,16 @@ class TestGoFZF < TestBase assert_equal [], `#{FZF} -f"'br" < #{tempname}`.lines.map(&:chomp) assert_equal ["foo'bar"], `#{FZF} -f"\\'br" < #{tempname}`.lines.map(&:chomp) end + + def test_escaped_meta_characters_only_on_relevant_positions + input = <<~EOF + \\^ + ^ + EOF + writelines tempname, input.lines.map(&:chomp) + assert_equal %w[^ \\^], `#{FZF} -f"\\^" < #{tempname}`.lines.map(&:chomp) + assert_equal %w[\\^], `#{FZF} -f"'\\^" < #{tempname}`.lines.map(&:chomp) + end end module TestShell