Some minor regexp match perf improvements

This commit is contained in:
Douglas Eichelberger 2023-12-27 13:00:48 -08:00
parent dbb800b7d3
commit 3abbf4447e
17 changed files with 20 additions and 20 deletions

View File

@ -20,7 +20,7 @@ module Cask
end end
def self.english_article def self.english_article
@english_article ||= (english_name =~ /^[aeiou]/i) ? "an" : "a" @english_article ||= /^[aeiou]/i.match?(english_name) ? "an" : "a"
end end
def self.dsl_key def self.dsl_key

View File

@ -843,7 +843,7 @@ module Cask
def bad_url_format?(regex, valid_formats_array) def bad_url_format?(regex, valid_formats_array)
return false unless cask.url.to_s.match?(regex) 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 end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }

View File

@ -294,7 +294,7 @@ module Homebrew
# Ignore matches to go keg, because all go binaries are statically linked. # Ignore matches to go keg, because all go binaries are statically linked.
any_go_deps = formula.deps.any? do |dep| 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 end
if any_go_deps if any_go_deps
go_regex = Version.formula_optionally_versioned_regex(:go, full: false) go_regex = Version.formula_optionally_versioned_regex(:go, full: false)

View File

@ -32,7 +32,7 @@ module FormulaCellarChecks
keg = Keg.new(formula.prefix) keg = Keg.new(formula.prefix)
system_openssl = keg.mach_o_files.select do |obj| system_openssl = keg.mach_o_files.select do |obj|
dlls = obj.dynamically_linked_libraries 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 end
return if system_openssl.empty? return if system_openssl.empty?

View File

@ -1328,7 +1328,7 @@ class Formula
self.class.link_overwrite_paths.any? do |p| self.class.link_overwrite_paths.any? do |p|
p == to_check || p == to_check ||
to_check.start_with?("#{p.chomp("/")}/") || to_check.start_with?("#{p.chomp("/")}/") ||
to_check =~ /^#{Regexp.escape(p).gsub('\*', ".*?")}$/ /^#{Regexp.escape(p).gsub('\*', ".*?")}$/.match?(to_check)
end end
end end

View File

@ -76,7 +76,7 @@ module Homebrew
!@versioned_formula && !@versioned_formula &&
(versioned_formulae = formula.versioned_formulae - [formula]) && (versioned_formulae = formula.versioned_formulae - [formula]) &&
versioned_formulae.present? 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("@") _, last_alias_version = versioned_formulae.map(&:name).last.split("@")
alias_name_major = "#{formula.name}@#{formula.version.major}" alias_name_major = "#{formula.name}@#{formula.version.major}"
@ -715,7 +715,7 @@ module Homebrew
return unless stable.url return unless stable.url
version = stable.version 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 stable_version_string = version.to_s
if stable_version_string.start_with?("HEAD") if stable_version_string.start_with?("HEAD")

View File

@ -16,7 +16,7 @@ module Homebrew
end end
def trailing_newline? def trailing_newline?
/\Z\n/ =~ @text /\Z\n/.match?(@text)
end end
def =~(other) def =~(other)
@ -32,12 +32,12 @@ module Homebrew
end end
def line_number(regex, skip = 0) 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 index ? index + 1 : nil
end end
def reverse_line_number(regex) 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 index ? @lines.count - index : nil
end end
end end

View File

@ -328,7 +328,7 @@ class Keg
Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io| Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io|
until io.eof? until io.eof?
str = io.readline.chomp 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 next unless str.match? path_regex
offset, match = str.split(" ", 2) offset, match = str.split(" ", 2)

View File

@ -66,7 +66,7 @@ module Homebrew
# Isolate tag strings and filter by regex # Isolate tag strings and filter by regex
tags = stdout.gsub(%r{^.*\trefs/tags/|\^{}$}, "").split("\n").uniq.sort 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[:tags] = tags
tags_data tags_data

View File

@ -78,7 +78,7 @@ class DATAPatch < EmbeddedPatch
path.open("rb") do |f| path.open("rb") do |f|
loop do loop do
line = f.gets line = f.gets
break if line.nil? || line =~ /^__END__$/ break if line.nil? || /^__END__$/.match?(line)
end end
while (line = f.gets) while (line = f.gets)
data << line data << line

View File

@ -233,7 +233,7 @@ module RuboCop
paths_to_exclude = [%r{/Library/Homebrew/test/}] 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 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 end
def on_system_methods def on_system_methods

View File

@ -34,7 +34,7 @@ module RuboCop
reason = string_content(reason).sub(name, "") reason = string_content(reason).sub(name, "")
first_word = reason.split.first 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| problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'." do |corrector|
reason[0] = reason[0].downcase reason[0] = reason[0].downcase
corrector.replace(@offensive_node.source_range, "\"#{reason}\"") corrector.replace(@offensive_node.source_range, "\"#{reason}\"")

View File

@ -26,7 +26,7 @@ module RuboCop
option = string_content(option) option = string_content(option)
problem UNI_DEPRECATION_MSG if option == "universal" problem UNI_DEPRECATION_MSG if option == "universal"
if option !~ /with(out)?-/ && if !/with(out)?-/.match?(option) &&
option != "cxx11" && option != "cxx11" &&
option != "universal" option != "universal"
problem "Options should begin with with/without. " \ problem "Options should begin with with/without. " \

View File

@ -172,7 +172,7 @@ class Sandbox
logs = Utils.popen_read("syslog", *syslog_args) logs = Utils.popen_read("syslog", *syslog_args)
# These messages are confusing and non-fatal, so don't report them. # 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? unless logs.empty?
if @logfile if @logfile

View File

@ -5,7 +5,7 @@ require "cask/audit"
describe Cask::Audit, :cask do describe Cask::Audit, :cask do
def include_msg?(problems, msg) def include_msg?(problems, msg)
if msg.is_a?(Regexp) if msg.is_a?(Regexp)
Array(problems).any? { |problem| problem[:message] =~ msg } Array(problems).any? { |problem| problem[:message]&.match?(msg) }
else else
Array(problems).any? { |problem| problem[:message] == msg } Array(problems).any? { |problem| problem[:message] == msg }
end end

View File

@ -51,7 +51,7 @@ module Utils
def receipt_path(bottle_file) def receipt_path(bottle_file)
bottle_file_list(bottle_file).find do |line| bottle_file_list(bottle_file).find do |line|
line =~ %r{.+/.+/INSTALL_RECEIPT.json} %r{.+/.+/INSTALL_RECEIPT.json}.match?(line)
end end
end end

View File

@ -335,7 +335,7 @@ module Utils
# Strategy: # Strategy:
# If the `:homepage` 404s, it's a GitHub link, and we have a token then # 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 # check the API (which does use tokens) for the repository
repo_details = url.match(%r{https?://github\.com/(?<user>[^/]+)/(?<repo>[^/]+)/?.*}) repo_details = url.match?(%r{https?://github\.com/(?<user>[^/]+)/(?<repo>[^/]+)/?.*})
check_github_api = url_type == SharedAudits::URL_TYPE_HOMEPAGE && check_github_api = url_type == SharedAudits::URL_TYPE_HOMEPAGE &&
details[:status_code] == "404" && details[:status_code] == "404" &&
repo_details && repo_details &&