From 3abbf4447e5611270fdfdf82aebb5a0328227af8 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Wed, 27 Dec 2023 13:00:48 -0800 Subject: [PATCH 1/2] Some minor regexp match perf improvements --- Library/Homebrew/cask/artifact/abstract_artifact.rb | 2 +- Library/Homebrew/cask/audit.rb | 2 +- Library/Homebrew/dev-cmd/bottle.rb | 2 +- Library/Homebrew/extend/os/mac/formula_cellar_checks.rb | 2 +- Library/Homebrew/formula.rb | 2 +- Library/Homebrew/formula_auditor.rb | 4 ++-- Library/Homebrew/formula_text_auditor.rb | 6 +++--- Library/Homebrew/keg_relocate.rb | 2 +- Library/Homebrew/livecheck/strategy/git.rb | 2 +- Library/Homebrew/patch.rb | 2 +- Library/Homebrew/rubocops/extend/formula_cop.rb | 2 +- Library/Homebrew/rubocops/keg_only.rb | 2 +- Library/Homebrew/rubocops/options.rb | 2 +- Library/Homebrew/sandbox.rb | 2 +- Library/Homebrew/test/cask/audit_spec.rb | 2 +- Library/Homebrew/utils/bottles.rb | 2 +- Library/Homebrew/utils/curl.rb | 2 +- 17 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/cask/artifact/abstract_artifact.rb b/Library/Homebrew/cask/artifact/abstract_artifact.rb index 6882e7f2acafe..112925e2d72ec 100644 --- a/Library/Homebrew/cask/artifact/abstract_artifact.rb +++ b/Library/Homebrew/cask/artifact/abstract_artifact.rb @@ -20,7 +20,7 @@ def self.english_name end def self.english_article - @english_article ||= (english_name =~ /^[aeiou]/i) ? "an" : "a" + @english_article ||= /^[aeiou]/i.match?(english_name) ? "an" : "a" end def self.dsl_key diff --git a/Library/Homebrew/cask/audit.rb b/Library/Homebrew/cask/audit.rb index 14d3d67769155..0974f12bf0ab7 100644 --- a/Library/Homebrew/cask/audit.rb +++ b/Library/Homebrew/cask/audit.rb @@ -843,7 +843,7 @@ def get_repo_data(regex) def bad_url_format?(regex, valid_formats_array) return false unless cask.url.to_s.match?(regex) - valid_formats_array.none? { |format| cask.url.to_s =~ format } + valid_formats_array.none? { |format| cask.url.to_s&.match?(format) } end sig { returns(T::Boolean) } diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 6667c17171709..453b9df8ea7be 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -294,7 +294,7 @@ def self.formula_ignores(formula) # Ignore matches to go keg, because all go binaries are statically linked. any_go_deps = formula.deps.any? do |dep| - dep.name =~ Version.formula_optionally_versioned_regex(:go) + dep.name&.match?(Version.formula_optionally_versioned_regex(:go)) end if any_go_deps go_regex = Version.formula_optionally_versioned_regex(:go, full: false) diff --git a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb index 1d74e3c5459c7..71913128cc82c 100644 --- a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb +++ b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb @@ -32,7 +32,7 @@ def check_openssl_links keg = Keg.new(formula.prefix) system_openssl = keg.mach_o_files.select do |obj| dlls = obj.dynamically_linked_libraries - dlls.any? { |dll| %r{/usr/lib/lib(crypto|ssl|tls)\..*dylib}.match dll } + dlls.any? { |dll| %r{/usr/lib/lib(crypto|ssl|tls)\..*dylib}.match? dll } end return if system_openssl.empty? diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 85730ad3cd5fd..77be731c68a87 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1328,7 +1328,7 @@ def link_overwrite?(path) self.class.link_overwrite_paths.any? do |p| p == to_check || to_check.start_with?("#{p.chomp("/")}/") || - to_check =~ /^#{Regexp.escape(p).gsub('\*', ".*?")}$/ + /^#{Regexp.escape(p).gsub('\*', ".*?")}$/.match?(to_check) end end diff --git a/Library/Homebrew/formula_auditor.rb b/Library/Homebrew/formula_auditor.rb index 2e64090dda3ed..035c491c3db31 100644 --- a/Library/Homebrew/formula_auditor.rb +++ b/Library/Homebrew/formula_auditor.rb @@ -76,7 +76,7 @@ def audit_file !@versioned_formula && (versioned_formulae = formula.versioned_formulae - [formula]) && versioned_formulae.present? - versioned_aliases, unversioned_aliases = formula.aliases.partition { |a| a =~ /.@\d/ } + versioned_aliases, unversioned_aliases = formula.aliases.partition { |a| /.@\d/.match?(a) } _, last_alias_version = versioned_formulae.map(&:name).last.split("@") alias_name_major = "#{formula.name}@#{formula.version.major}" @@ -715,7 +715,7 @@ def audit_specs return unless stable.url version = stable.version - problem "Stable: version (#{version}) is set to a string without a digit" if version.to_s !~ /\d/ + problem "Stable: version (#{version}) is set to a string without a digit" unless /\d/.match?(version.to_s) stable_version_string = version.to_s if stable_version_string.start_with?("HEAD") diff --git a/Library/Homebrew/formula_text_auditor.rb b/Library/Homebrew/formula_text_auditor.rb index 1b8cf9059b8f3..31569ff333a75 100644 --- a/Library/Homebrew/formula_text_auditor.rb +++ b/Library/Homebrew/formula_text_auditor.rb @@ -16,7 +16,7 @@ def without_patch end def trailing_newline? - /\Z\n/ =~ @text + /\Z\n/.match?(@text) end def =~(other) @@ -32,12 +32,12 @@ def to_s end def line_number(regex, skip = 0) - index = @lines.drop(skip).index { |line| line =~ regex } + index = @lines.drop(skip).index { |line| line&.match?(regex) } index ? index + 1 : nil end def reverse_line_number(regex) - index = @lines.reverse.index { |line| line =~ regex } + index = @lines.reverse.index { |line| line&.match?(regex) } index ? @lines.count - index : nil end end diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index bbb2f87d9b8b8..dec59b5daf3e0 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -328,7 +328,7 @@ def self.text_matches_in_file(file, string, ignores, linked_libraries, formula_a Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io| until io.eof? str = io.readline.chomp - next if ignores.any? { |i| i =~ str } + next if ignores.any? { |i| i&.match?(str) } next unless str.match? path_regex offset, match = str.split(" ", 2) diff --git a/Library/Homebrew/livecheck/strategy/git.rb b/Library/Homebrew/livecheck/strategy/git.rb index 4a72b042f79e1..393e600dbf017 100644 --- a/Library/Homebrew/livecheck/strategy/git.rb +++ b/Library/Homebrew/livecheck/strategy/git.rb @@ -66,7 +66,7 @@ def self.tag_info(url, regex = nil) # Isolate tag strings and filter by regex tags = stdout.gsub(%r{^.*\trefs/tags/|\^{}$}, "").split("\n").uniq.sort - tags.select! { |t| t =~ regex } if regex + tags.select! { |t| t&.match?(regex) } if regex tags_data[:tags] = tags tags_data diff --git a/Library/Homebrew/patch.rb b/Library/Homebrew/patch.rb index 3a96d12eca17d..971f6c6dca1bf 100644 --- a/Library/Homebrew/patch.rb +++ b/Library/Homebrew/patch.rb @@ -78,7 +78,7 @@ def contents path.open("rb") do |f| loop do line = f.gets - break if line.nil? || line =~ /^__END__$/ + break if line.nil? || /^__END__$/.match?(line) end while (line = f.gets) data << line diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 6f32cb2c054ad..04e81333e7882 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -233,7 +233,7 @@ def file_path_allowed? paths_to_exclude = [%r{/Library/Homebrew/test/}] return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs - @file_path !~ Regexp.union(paths_to_exclude) + !@file_path&.match?(Regexp.union(paths_to_exclude)) end def on_system_methods diff --git a/Library/Homebrew/rubocops/keg_only.rb b/Library/Homebrew/rubocops/keg_only.rb index d28e7c56c75de..80540399efcfd 100644 --- a/Library/Homebrew/rubocops/keg_only.rb +++ b/Library/Homebrew/rubocops/keg_only.rb @@ -34,7 +34,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node) reason = string_content(reason).sub(name, "") first_word = reason.split.first - if reason =~ /\A[A-Z]/ && !reason.start_with?(*allowlist) + if /\A[A-Z]/.match?(reason) && !reason.start_with?(*allowlist) problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'." do |corrector| reason[0] = reason[0].downcase corrector.replace(@offensive_node.source_range, "\"#{reason}\"") diff --git a/Library/Homebrew/rubocops/options.rb b/Library/Homebrew/rubocops/options.rb index 77b8ee8d97581..a7eadd55bb6e3 100644 --- a/Library/Homebrew/rubocops/options.rb +++ b/Library/Homebrew/rubocops/options.rb @@ -26,7 +26,7 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node) option = string_content(option) problem UNI_DEPRECATION_MSG if option == "universal" - if option !~ /with(out)?-/ && + if !/with(out)?-/.match?(option) && option != "cxx11" && option != "universal" problem "Options should begin with with/without. " \ diff --git a/Library/Homebrew/sandbox.rb b/Library/Homebrew/sandbox.rb index 6eda0073c5df9..5b187e7bf676c 100644 --- a/Library/Homebrew/sandbox.rb +++ b/Library/Homebrew/sandbox.rb @@ -172,7 +172,7 @@ def exec(*args) logs = Utils.popen_read("syslog", *syslog_args) # These messages are confusing and non-fatal, so don't report them. - logs = logs.lines.reject { |l| l.match(/^.*Python\(\d+\) deny file-write.*pyc$/) }.join + logs = logs.lines.grep_v(/^.*Python\(\d+\) deny file-write.*pyc$/).join unless logs.empty? if @logfile diff --git a/Library/Homebrew/test/cask/audit_spec.rb b/Library/Homebrew/test/cask/audit_spec.rb index d579af0923a14..df5bc8f6b0fd2 100644 --- a/Library/Homebrew/test/cask/audit_spec.rb +++ b/Library/Homebrew/test/cask/audit_spec.rb @@ -5,7 +5,7 @@ describe Cask::Audit, :cask do def include_msg?(problems, msg) if msg.is_a?(Regexp) - Array(problems).any? { |problem| problem[:message] =~ msg } + Array(problems).any? { |problem| problem[:message]&.match?(msg) } else Array(problems).any? { |problem| problem[:message] == msg } end diff --git a/Library/Homebrew/utils/bottles.rb b/Library/Homebrew/utils/bottles.rb index 87a16b2487e88..273e9f85e82c3 100644 --- a/Library/Homebrew/utils/bottles.rb +++ b/Library/Homebrew/utils/bottles.rb @@ -51,7 +51,7 @@ def extname_tag_rebuild(filename) def receipt_path(bottle_file) bottle_file_list(bottle_file).find do |line| - line =~ %r{.+/.+/INSTALL_RECEIPT.json} + %r{.+/.+/INSTALL_RECEIPT.json}.match?(line) end end diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index 8973d43d3c4f3..efc9bcbf1e164 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -335,7 +335,7 @@ def curl_check_http_content(url, url_type, specs: {}, user_agents: [:default], r # Strategy: # If the `:homepage` 404s, it's a GitHub link, and we have a token then # check the API (which does use tokens) for the repository - repo_details = url.match(%r{https?://github\.com/(?[^/]+)/(?[^/]+)/?.*}) + repo_details = url.match?(%r{https?://github\.com/(?[^/]+)/(?[^/]+)/?.*}) check_github_api = url_type == SharedAudits::URL_TYPE_HOMEPAGE && details[:status_code] == "404" && repo_details && From caf8259ae62d5b9c54b5a59f134ed2cbd5ecbe42 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Wed, 27 Dec 2023 15:29:33 -0800 Subject: [PATCH 2/2] Code review changes --- Library/Homebrew/cask/audit.rb | 2 +- Library/Homebrew/dev-cmd/bottle.rb | 2 +- Library/Homebrew/formula_text_auditor.rb | 4 ++-- Library/Homebrew/keg_relocate.rb | 2 +- Library/Homebrew/livecheck/strategy/git.rb | 2 +- Library/Homebrew/rubocops/extend/formula_cop.rb | 2 +- Library/Homebrew/test/cask/audit_spec.rb | 2 +- Library/Homebrew/utils/curl.rb | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Library/Homebrew/cask/audit.rb b/Library/Homebrew/cask/audit.rb index 0974f12bf0ab7..67ae10e22c44d 100644 --- a/Library/Homebrew/cask/audit.rb +++ b/Library/Homebrew/cask/audit.rb @@ -843,7 +843,7 @@ def get_repo_data(regex) def bad_url_format?(regex, valid_formats_array) return false unless cask.url.to_s.match?(regex) - valid_formats_array.none? { |format| cask.url.to_s&.match?(format) } + valid_formats_array.none? { |format| cask.url.to_s.match?(format) } end sig { returns(T::Boolean) } diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 453b9df8ea7be..6a51a21e1e780 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -294,7 +294,7 @@ def self.formula_ignores(formula) # Ignore matches to go keg, because all go binaries are statically linked. any_go_deps = formula.deps.any? do |dep| - dep.name&.match?(Version.formula_optionally_versioned_regex(:go)) + Version.formula_optionally_versioned_regex(:go).match?(dep.name) end if any_go_deps go_regex = Version.formula_optionally_versioned_regex(:go, full: false) diff --git a/Library/Homebrew/formula_text_auditor.rb b/Library/Homebrew/formula_text_auditor.rb index 31569ff333a75..495fa870159ca 100644 --- a/Library/Homebrew/formula_text_auditor.rb +++ b/Library/Homebrew/formula_text_auditor.rb @@ -32,12 +32,12 @@ def to_s end def line_number(regex, skip = 0) - index = @lines.drop(skip).index { |line| line&.match?(regex) } + index = @lines.drop(skip).index { |line| line.match?(regex) } index ? index + 1 : nil end def reverse_line_number(regex) - index = @lines.reverse.index { |line| line&.match?(regex) } + index = @lines.reverse.index { |line| line.match?(regex) } index ? @lines.count - index : nil end end diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index dec59b5daf3e0..7173c27a74fff 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -328,7 +328,7 @@ def self.text_matches_in_file(file, string, ignores, linked_libraries, formula_a Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io| until io.eof? str = io.readline.chomp - next if ignores.any? { |i| i&.match?(str) } + next if ignores.any? { |i| str.match?(i) } next unless str.match? path_regex offset, match = str.split(" ", 2) diff --git a/Library/Homebrew/livecheck/strategy/git.rb b/Library/Homebrew/livecheck/strategy/git.rb index 393e600dbf017..ed669b39f0415 100644 --- a/Library/Homebrew/livecheck/strategy/git.rb +++ b/Library/Homebrew/livecheck/strategy/git.rb @@ -66,7 +66,7 @@ def self.tag_info(url, regex = nil) # Isolate tag strings and filter by regex tags = stdout.gsub(%r{^.*\trefs/tags/|\^{}$}, "").split("\n").uniq.sort - tags.select! { |t| t&.match?(regex) } if regex + tags.select! { |t| regex.match?(t) } if regex tags_data[:tags] = tags tags_data diff --git a/Library/Homebrew/rubocops/extend/formula_cop.rb b/Library/Homebrew/rubocops/extend/formula_cop.rb index 04e81333e7882..6cfaa7b9f601f 100644 --- a/Library/Homebrew/rubocops/extend/formula_cop.rb +++ b/Library/Homebrew/rubocops/extend/formula_cop.rb @@ -233,7 +233,7 @@ def file_path_allowed? paths_to_exclude = [%r{/Library/Homebrew/test/}] return true if @file_path.nil? # file_path is nil when source is directly passed to the cop, e.g. in specs - !@file_path&.match?(Regexp.union(paths_to_exclude)) + !@file_path.match?(Regexp.union(paths_to_exclude)) end def on_system_methods diff --git a/Library/Homebrew/test/cask/audit_spec.rb b/Library/Homebrew/test/cask/audit_spec.rb index df5bc8f6b0fd2..af6d944420eff 100644 --- a/Library/Homebrew/test/cask/audit_spec.rb +++ b/Library/Homebrew/test/cask/audit_spec.rb @@ -5,7 +5,7 @@ describe Cask::Audit, :cask do def include_msg?(problems, msg) if msg.is_a?(Regexp) - Array(problems).any? { |problem| problem[:message]&.match?(msg) } + Array(problems).any? { |problem| msg.match?(problem[:message]) } else Array(problems).any? { |problem| problem[:message] == msg } end diff --git a/Library/Homebrew/utils/curl.rb b/Library/Homebrew/utils/curl.rb index efc9bcbf1e164..8973d43d3c4f3 100644 --- a/Library/Homebrew/utils/curl.rb +++ b/Library/Homebrew/utils/curl.rb @@ -335,7 +335,7 @@ def curl_check_http_content(url, url_type, specs: {}, user_agents: [:default], r # Strategy: # If the `:homepage` 404s, it's a GitHub link, and we have a token then # check the API (which does use tokens) for the repository - repo_details = url.match?(%r{https?://github\.com/(?[^/]+)/(?[^/]+)/?.*}) + repo_details = url.match(%r{https?://github\.com/(?[^/]+)/(?[^/]+)/?.*}) check_github_api = url_type == SharedAudits::URL_TYPE_HOMEPAGE && details[:status_code] == "404" && repo_details &&