From baf2ec2ca8127cd610a3681a1e84ebcb404fe8f2 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Wed, 13 Dec 2023 03:46:10 -0800 Subject: [PATCH] [rubygems/rubygems] Use match? when regexp match data is unused Improved performance / reduced allocations https://github.com/rubygems/rubygems/commit/b04726c9a7 --- lib/bundler/cli.rb | 4 ++-- lib/bundler/cli/common.rb | 2 +- lib/bundler/shared_helpers.rb | 2 +- lib/bundler/source/git/git_proxy.rb | 2 +- lib/bundler/templates/Executable.bundler | 2 +- lib/bundler/ui/shell.rb | 2 +- lib/rubygems/command.rb | 2 +- lib/rubygems/platform.rb | 4 ++-- lib/rubygems/source/git.rb | 2 +- lib/rubygems/util.rb | 2 +- test/rubygems/helper.rb | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index da5950917bbed9..bf6e0fbec9dba4 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -127,8 +127,8 @@ def help(cli = nil) if man_pages.include?(command) man_page = man_pages[command] - if Bundler.which("man") && man_path !~ %r{^file:/.+!/META-INF/jruby.home/.+} - Kernel.exec "man #{man_page}" + if Bundler.which("man") && !man_path.match?(%r{^file:/.+!/META-INF/jruby.home/.+}) + Kernel.exec("man", man_page) else puts File.read("#{man_path}/#{File.basename(man_page)}.ronn") end diff --git a/lib/bundler/cli/common.rb b/lib/bundler/cli/common.rb index 0cf85c1d8de327..dc2a889d2b455e 100644 --- a/lib/bundler/cli/common.rb +++ b/lib/bundler/cli/common.rb @@ -54,7 +54,7 @@ def self.select_spec(name, regex_match = nil) Bundler.definition.specs.each do |spec| return spec if spec.name == name - specs << spec if regexp && spec.name =~ regexp + specs << spec if regexp && spec.name.match?(regexp) end default_spec = default_gem_spec(name) diff --git a/lib/bundler/shared_helpers.rb b/lib/bundler/shared_helpers.rb index 23cbaae94005d9..a214641ae187d8 100644 --- a/lib/bundler/shared_helpers.rb +++ b/lib/bundler/shared_helpers.rb @@ -329,7 +329,7 @@ def set_path def set_rubyopt rubyopt = [ENV["RUBYOPT"]].compact setup_require = "-r#{File.expand_path("setup", __dir__)}" - return if !rubyopt.empty? && rubyopt.first =~ /#{Regexp.escape(setup_require)}/ + return if !rubyopt.empty? && rubyopt.first.include?(setup_require) rubyopt.unshift setup_require Bundler::SharedHelpers.set_env "RUBYOPT", rubyopt.join(" ") end diff --git a/lib/bundler/source/git/git_proxy.rb b/lib/bundler/source/git/git_proxy.rb index 484a03183514ba..8b6d4208846658 100644 --- a/lib/bundler/source/git/git_proxy.rb +++ b/lib/bundler/source/git/git_proxy.rb @@ -87,7 +87,7 @@ def current_branch def contains?(commit) allowed_with_path do result, status = git_null("branch", "--contains", commit, dir: path) - status.success? && result =~ /^\* (.*)$/ + status.success? && result.match?(/^\* (.*)$/) end end diff --git a/lib/bundler/templates/Executable.bundler b/lib/bundler/templates/Executable.bundler index e290fe91eba100..caa20217016240 100644 --- a/lib/bundler/templates/Executable.bundler +++ b/lib/bundler/templates/Executable.bundler @@ -27,7 +27,7 @@ m = Module.new do bundler_version = nil update_index = nil ARGV.each_with_index do |a, i| - if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN + if update_index && update_index.succ == i && a.match?(Gem::Version::ANCHORED_VERSION_PATTERN) bundler_version = a end next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ diff --git a/lib/bundler/ui/shell.rb b/lib/bundler/ui/shell.rb index a9053f2852fadc..4555612dbb639e 100644 --- a/lib/bundler/ui/shell.rb +++ b/lib/bundler/ui/shell.rb @@ -130,7 +130,7 @@ def tell_me(msg, color = nil, newline = nil) def tell_err(message, color = nil, newline = nil) return if @shell.send(:stderr).closed? - newline ||= message.to_s !~ /( |\t)\Z/ + newline ||= !message.to_s.match?(/( |\t)\Z/) message = word_wrap(message) if newline.is_a?(Hash) && newline[:wrap] color = nil if color && !$stderr.tty? diff --git a/lib/rubygems/command.rb b/lib/rubygems/command.rb index 1320d9f49f744c..06ec2a5bdda2ae 100644 --- a/lib/rubygems/command.rb +++ b/lib/rubygems/command.rb @@ -190,7 +190,7 @@ def get_all_gem_names "Please specify at least one gem name (e.g. gem build GEMNAME)" end - args.select {|arg| arg !~ /^-/ } + args.reject {|arg| arg.start_with?("-") } end ## diff --git a/lib/rubygems/platform.rb b/lib/rubygems/platform.rb index 3513db13f418e7..48b7344aee5d77 100644 --- a/lib/rubygems/platform.rb +++ b/lib/rubygems/platform.rb @@ -89,7 +89,7 @@ def initialize(arch) when String then arch = arch.split "-" - if arch.length > 2 && arch.last !~ /\d+(\.\d+)?$/ # reassemble x86-linux-{libc} + if arch.length > 2 && !arch.last.match?(/\d+(\.\d+)?$/) # reassemble x86-linux-{libc} extra = arch.pop arch.last << "-#{extra}" end @@ -101,7 +101,7 @@ def initialize(arch) else cpu end - if arch.length == 2 && arch.last =~ /^\d+(\.\d+)?$/ # for command-line + if arch.length == 2 && arch.last.match?(/^\d+(\.\d+)?$/) # for command-line @os, @version = arch return end diff --git a/lib/rubygems/source/git.rb b/lib/rubygems/source/git.rb index 70e8d1b06e104a..a0d03312b9681a 100644 --- a/lib/rubygems/source/git.rb +++ b/lib/rubygems/source/git.rb @@ -227,7 +227,7 @@ def uri_hash # :nodoc: require_relative "../openssl" normalized = - if @repository =~ %r{^\w+://(\w+@)?} + if @repository.match?(%r{^\w+://(\w+@)?}) uri = URI(@repository).normalize.to_s.sub %r{/$},"" uri.sub(/\A(\w+)/) { $1.downcase } else diff --git a/lib/rubygems/util.rb b/lib/rubygems/util.rb index eba89e64e3f1e0..1815f6af6fb8c7 100644 --- a/lib/rubygems/util.rb +++ b/lib/rubygems/util.rb @@ -109,7 +109,7 @@ def self.glob_files_in_dir(glob, base_path) # comes with a leading slash. def self.correct_for_windows_path(path) - if path[0].chr == "/" && path[1].chr =~ /[a-z]/i && path[2].chr == ":" + if path[0].chr == "/" && path[1].chr.match?(/[a-z]/i) && path[2].chr == ":" path[1..-1] else path diff --git a/test/rubygems/helper.rb b/test/rubygems/helper.rb index 1cdf7c44594abe..d259fbde1bc2fc 100644 --- a/test/rubygems/helper.rb +++ b/test/rubygems/helper.rb @@ -582,7 +582,7 @@ def have_git? end def in_path?(executable) # :nodoc: - return true if %r{\A([A-Z]:|/)} =~ executable && File.exist?(executable) + return true if %r{\A([A-Z]:|/)}.match?(executable) && File.exist?(executable) ENV["PATH"].split(File::PATH_SEPARATOR).any? do |directory| File.exist? File.join directory, executable