Disallow escaping of meta characters except for spaces

https://github.com/junegunn/fzf/issues/444#issuecomment-321719604
This commit is contained in:
Junegunn Choi 2017-08-11 13:09:33 +09:00
parent a09e411936
commit 6c76d8cd1c
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 8 additions and 30 deletions

View File

@ -54,15 +54,13 @@ type Pattern struct {
} }
var ( var (
_patternCache map[string]*Pattern _patternCache map[string]*Pattern
_splitRegex *regexp.Regexp _splitRegex *regexp.Regexp
_escapedPrefixRegex *regexp.Regexp _cache ChunkCache
_cache ChunkCache
) )
func init() { func init() {
_splitRegex = regexp.MustCompile(" +") _splitRegex = regexp.MustCompile(" +")
_escapedPrefixRegex = regexp.MustCompile("^\\\\['!^|]")
clearPatternCache() clearPatternCache()
clearChunkCache() clearChunkCache()
} }
@ -179,17 +177,11 @@ func parseTerms(fuzzy bool, caseMode Case, normalize bool, str string) []termSet
} }
if text != "$" && strings.HasSuffix(text, "$") { if text != "$" && strings.HasSuffix(text, "$") {
if strings.HasSuffix(text, "\\$") { typ = termSuffix
text = text[:len(text)-2] + "$" text = text[:len(text)-1]
} else {
typ = termSuffix
text = text[:len(text)-1]
}
} }
if _escapedPrefixRegex.MatchString(text) { if strings.HasPrefix(text, "'") {
text = text[1:]
} else if strings.HasPrefix(text, "'") {
// Flip exactness // Flip exactness
if fuzzy && !inv { if fuzzy && !inv {
typ = termExact typ = termExact

View File

@ -1391,24 +1391,10 @@ class TestGoFZF < TestBase
writelines tempname, input.lines.map(&:chomp) writelines tempname, input.lines.map(&:chomp)
assert_equal input.lines.count, `#{FZF} -f'foo bar' < #{tempname}`.lines.count assert_equal input.lines.count, `#{FZF} -f'foo bar' < #{tempname}`.lines.count
assert_equal input.lines.count - 1, `#{FZF} -f'^foo bar$' < #{tempname}`.lines.count
assert_equal ['foo bar'], `#{FZF} -f'foo\\ bar' < #{tempname}`.lines.map(&:chomp) assert_equal ['foo bar'], `#{FZF} -f'foo\\ bar' < #{tempname}`.lines.map(&:chomp)
assert_equal ['bar foo'], `#{FZF} -f'foo$' < #{tempname}`.lines.map(&:chomp)
assert_equal ['foo$bar'], `#{FZF} -f'foo\\$' < #{tempname}`.lines.map(&:chomp)
assert_equal [], `#{FZF} -f'!bar' < #{tempname}`.lines.map(&:chomp)
assert_equal ['foo!bar'], `#{FZF} -f'\\!bar' < #{tempname}`.lines.map(&:chomp)
assert_equal ['foo bar'], `#{FZF} -f'^foo\\ bar$' < #{tempname}`.lines.map(&:chomp) assert_equal ['foo bar'], `#{FZF} -f'^foo\\ bar$' < #{tempname}`.lines.map(&:chomp)
assert_equal [], `#{FZF} -f"'br" < #{tempname}`.lines.map(&:chomp) assert_equal input.lines.count - 1, `#{FZF} -f'!^foo\\ bar$' < #{tempname}`.lines.count
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
end end