From 6fcc5d14de042e5328a0d37972af35aa98f5a9eb Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 5 Jun 2018 10:55:00 +0200 Subject: [PATCH] Simplify strings for search. --- Library/Homebrew/cmd/desc.rb | 8 ++++--- Library/Homebrew/descriptions.rb | 9 +++++--- Library/Homebrew/search.rb | 10 +++++++-- Library/Homebrew/test/search_spec.rb | 31 ++++++++++++++-------------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Library/Homebrew/cmd/desc.rb b/Library/Homebrew/cmd/desc.rb index ccb7e99255..b704d08b85 100644 --- a/Library/Homebrew/cmd/desc.rb +++ b/Library/Homebrew/cmd/desc.rb @@ -9,11 +9,13 @@ #: first search, making that search slower than subsequent ones. require "descriptions" -require "cmd/search" +require "search" module Homebrew module_function + extend Search + def desc search_type = [] search_type << :either if ARGV.flag? "--search" @@ -28,8 +30,8 @@ module Homebrew results.print elsif search_type.size > 1 odie "Pick one, and only one, of -s/--search, -n/--name, or -d/--description." - elsif arg = ARGV.named.first - regex = Homebrew.query_regexp(arg) + elsif arg = ARGV.named.join(" ") + regex = query_regexp(arg) results = Descriptions.search(regex, search_type.first) results.print else diff --git a/Library/Homebrew/descriptions.rb b/Library/Homebrew/descriptions.rb index 4db47e77f1..9d7c16b636 100644 --- a/Library/Homebrew/descriptions.rb +++ b/Library/Homebrew/descriptions.rb @@ -1,7 +1,10 @@ require "formula" require "formula_versions" +require "search" class Descriptions + extend Homebrew::Search + CACHE_FILE = HOMEBREW_CACHE + "desc_cache.json" def self.cache @@ -99,11 +102,11 @@ class Descriptions results = case field when :name - @cache.select { |name, _| name =~ regex } + @cache.select { |name, _| simplify_string(name).match?(regex) } when :desc - @cache.select { |_, desc| desc =~ regex } + @cache.select { |_, desc| simplify_string(desc).match?(regex) } when :either - @cache.select { |name, desc| (name =~ regex) || (desc =~ regex) } + @cache.select { |name, desc| simplify_string(name).match?(regex) || simplify_string(desc).match?(regex) } end new(results) diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index fdd90f3ed6..c94a5a61f8 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -1,10 +1,14 @@ module Homebrew module Search + def simplify_string(string) + string.downcase.gsub(/[^a-z\d]/i, "") + end + def query_regexp(query) if m = query.match(%r{^/(.*)/$}) Regexp.new(m[1]) else - Regexp.new(query.chars.join('[^a-z\d]*'), Regexp::IGNORECASE) + Regexp.new(simplify_string(query), Regexp::IGNORECASE) end rescue RegexpError raise "#{query} is not a valid regex." @@ -48,7 +52,9 @@ module Homebrew $stderr.puts Formatter.headline("Searching local taps...", color: :blue) aliases = Formula.alias_full_names - results = (Formula.full_names + aliases).grep(regex).sort + results = (Formula.full_names + aliases) + .select { |name| simplify_string(name).match?(regex) } + .sort results.map do |name| begin diff --git a/Library/Homebrew/test/search_spec.rb b/Library/Homebrew/test/search_spec.rb index f7be0b805f..222ace3080 100644 --- a/Library/Homebrew/test/search_spec.rb +++ b/Library/Homebrew/test/search_spec.rb @@ -49,32 +49,31 @@ describe Homebrew::Search do end end + describe "#simplify_string" do + it "simplifies a query with dashes" do + expect(mod.query_regexp("que-ry")).to eq(/query/i) + end + + it "simplifies a query with @ symbols" do + expect(mod.query_regexp("query@1")).to eq(/query1/i) + end + end + describe "#query_regexp" do it "correctly parses a regex query" do expect(mod.query_regexp("/^query$/")).to eq(/^query$/) end it "correctly converts a query string to a regex" do - expect(mod.query_regexp("query")).to eq(/q[^a-z\d]*u[^a-z\d]*e[^a-z\d]*r[^a-z\d]*y/i) + expect(mod.query_regexp("query")).to eq(/query/i) + end + + it "simplifies a query with special symbols" do + expect(mod.query_regexp("que-ry")).to eq(/query/i) end it "raises an error if the query is an invalid regex" do expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/) end - - it "correctly matches with special symbols" do - regex = mod.query_regexp("oo-ba") - expect(regex).to match("foo-bar") - end - - it "correctly matches without special symbols" do - regex = mod.query_regexp("ooba") - expect(regex).to match("foo-bar") - end - - it "keeps special symbols" do - regex = mod.query_regexp("foo-bar") - expect(regex).not_to match("foobar") - end end end