Add String#delete_prefix
backport.
This commit is contained in:
parent
dfb6ada805
commit
3a0a9f9073
@ -382,7 +382,7 @@ module Homebrew
|
||||
f.full_name => {
|
||||
"formula" => {
|
||||
"pkg_version" => f.pkg_version.to_s,
|
||||
"path" => f.path.to_s.strip_prefix("#{HOMEBREW_REPOSITORY}/"),
|
||||
"path" => f.path.to_s.delete_prefix("#{HOMEBREW_REPOSITORY}/"),
|
||||
},
|
||||
"bottle" => {
|
||||
"root_url" => bottle.root_url,
|
||||
|
@ -127,7 +127,7 @@ module Homebrew
|
||||
if (testing_match = arg.match %r{/job/Homebrew.*Testing/(\d+)/})
|
||||
tap = ARGV.value("tap")
|
||||
tap = if tap&.start_with?("homebrew/")
|
||||
Tap.fetch("homebrew", tap.strip_prefix("homebrew/"))
|
||||
Tap.fetch("homebrew", tap.delete_prefix("homebrew/"))
|
||||
elsif tap
|
||||
odie "Tap option did not start with \"homebrew/\": #{tap}"
|
||||
else
|
||||
|
@ -118,7 +118,7 @@ module HomebrewArgvExtension
|
||||
def value(name)
|
||||
arg_prefix = "--#{name}="
|
||||
flag_with_value = find { |arg| arg.start_with?(arg_prefix) }
|
||||
flag_with_value&.strip_prefix(arg_prefix)
|
||||
flag_with_value&.delete_prefix(arg_prefix)
|
||||
end
|
||||
|
||||
# Returns an array of values that were given as a comma-separated list.
|
||||
|
@ -1,5 +1,6 @@
|
||||
# Contains backports from newer versions of Ruby
|
||||
require "backports/2.4.0/string/match"
|
||||
require "backports/2.5.0/string/delete_prefix"
|
||||
|
||||
class String
|
||||
# String.chomp, but if result is empty: returns nil instead.
|
||||
@ -8,10 +9,6 @@ class String
|
||||
s = chomp
|
||||
s unless s.empty?
|
||||
end
|
||||
|
||||
def strip_prefix(prefix)
|
||||
start_with?(prefix) ? self[prefix.length..-1] : self
|
||||
end
|
||||
end
|
||||
|
||||
class NilClass
|
||||
|
@ -43,7 +43,7 @@ class LinkageChecker
|
||||
sorted.each do |dylib, files|
|
||||
puts dylib
|
||||
files.each do |f|
|
||||
unprefixed = f.to_s.strip_prefix "#{keg}/"
|
||||
unprefixed = f.to_s.delete_prefix "#{keg}/"
|
||||
puts " #{unprefixed}"
|
||||
end
|
||||
puts if dylib != sorted.last.first
|
||||
|
@ -27,7 +27,7 @@ class Tap
|
||||
|
||||
# We special case homebrew and linuxbrew so that users don't have to shift in a terminal.
|
||||
user = user.capitalize if ["homebrew", "linuxbrew"].include? user
|
||||
repo = repo.strip_prefix "homebrew-"
|
||||
repo = repo.delete_prefix "homebrew-"
|
||||
|
||||
if ["Homebrew", "Linuxbrew"].include?(user) && ["core", "homebrew"].include?(repo)
|
||||
return CoreTap.instance
|
||||
@ -119,7 +119,7 @@ class Tap
|
||||
|
||||
def repo_var
|
||||
@repo_var ||= path.to_s
|
||||
.strip_prefix(TAP_DIRECTORY.to_s)
|
||||
.delete_prefix(TAP_DIRECTORY.to_s)
|
||||
.tr("^A-Za-z0-9", "_")
|
||||
.upcase
|
||||
end
|
||||
|
@ -0,0 +1,32 @@
|
||||
unless String.method_defined? :delete_prefix
|
||||
require 'backports/tools/arguments'
|
||||
|
||||
class String
|
||||
def delete_prefix(prefix)
|
||||
prefix = Backports.coerce_to_str(prefix)
|
||||
if rindex(prefix, 0)
|
||||
self[prefix.length..-1]
|
||||
else
|
||||
dup
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
unless String.method_defined? :delete_prefix!
|
||||
require 'backports/tools/arguments'
|
||||
|
||||
class String
|
||||
def delete_prefix!(prefix)
|
||||
prefix = Backports.coerce_to_str(prefix)
|
||||
chomp! if frozen?
|
||||
len = prefix.length
|
||||
if len > 0 && rindex(prefix, 0)
|
||||
self[0...prefix.length] = ''
|
||||
self
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,69 @@
|
||||
module Backports
|
||||
# Helper method to coerce a value into a specific class.
|
||||
# Raises a TypeError if the coercion fails or the returned value
|
||||
# is not of the right class.
|
||||
# (from Rubinius)
|
||||
def self.coerce_to(obj, cls, meth)
|
||||
return obj if obj.kind_of?(cls)
|
||||
|
||||
begin
|
||||
ret = obj.__send__(meth)
|
||||
rescue Exception => e
|
||||
raise TypeError, "Coercion error: #{obj.inspect}.#{meth} => #{cls} failed:\n" \
|
||||
"(#{e.message})"
|
||||
end
|
||||
raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{ret.class})" unless ret.kind_of? cls
|
||||
ret
|
||||
end
|
||||
|
||||
def self.coerce_to_int(obj)
|
||||
coerce_to(obj, Integer, :to_int)
|
||||
end
|
||||
|
||||
def self.coerce_to_ary(obj)
|
||||
coerce_to(obj, Array, :to_ary)
|
||||
end
|
||||
|
||||
def self.coerce_to_str(obj)
|
||||
coerce_to(obj, String, :to_str)
|
||||
end
|
||||
|
||||
def self.coerce_to_hash(obj)
|
||||
coerce_to(obj, Hash, :to_hash)
|
||||
end
|
||||
|
||||
def self.coerce_to_options(obj, *options)
|
||||
hash = coerce_to_hash(obj)
|
||||
hash.values_at(*options)
|
||||
end
|
||||
|
||||
def self.coerce_to_option(obj, option)
|
||||
coerce_to_options(obj, option)[0]
|
||||
end
|
||||
|
||||
def self.is_array?(obj)
|
||||
coerce_to(obj, Array, :to_ary) if obj.respond_to? :to_ary
|
||||
end
|
||||
|
||||
def self.try_convert(obj, cls, meth)
|
||||
return obj if obj.kind_of?(cls)
|
||||
return nil unless obj.respond_to?(meth)
|
||||
ret = obj.__send__(meth)
|
||||
raise TypeError, "Coercion error: obj.#{meth} did NOT return a #{cls} (was #{ret.class})" unless ret.nil? || ret.kind_of?(cls)
|
||||
ret
|
||||
end
|
||||
|
||||
# Checks for a failed comparison (in which case it throws an ArgumentError)
|
||||
# Additionally, it maps any negative value to -1 and any positive value to +1
|
||||
# (from Rubinius)
|
||||
def self.coerce_to_comparison(a, b, cmp = (a <=> b))
|
||||
raise ArgumentError, "comparison of #{a} with #{b} failed" if cmp.nil?
|
||||
return 1 if cmp > 0
|
||||
return -1 if cmp < 0
|
||||
0
|
||||
end
|
||||
|
||||
# Used internally to make it easy to deal with optional arguments
|
||||
# (from Rubinius)
|
||||
Undefined = Object.new
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user