Prefer no allocation `start/end_with?` over `String#[] ==`
This commit is contained in:
parent
97c3a5605b
commit
c65864cdca
|
@ -170,7 +170,7 @@ module ActionView
|
|||
def explicit_dependencies
|
||||
dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq
|
||||
|
||||
wildcards, explicits = dependencies.partition { |dependency| dependency[-1] == "*" }
|
||||
wildcards, explicits = dependencies.partition { |dependency| dependency.end_with?("*") }
|
||||
|
||||
(explicits + resolve_directories(wildcards)).uniq
|
||||
end
|
||||
|
|
|
@ -46,7 +46,7 @@ module ActionView
|
|||
as.to_sym
|
||||
else
|
||||
begin
|
||||
base = path[-1] == "/" ? "" : File.basename(path)
|
||||
base = path.end_with?("/") ? "" : File.basename(path)
|
||||
raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
|
||||
$1.to_sym
|
||||
end
|
||||
|
|
|
@ -138,7 +138,7 @@ module ActionView
|
|||
@virtual_path = virtual_path
|
||||
|
||||
@variable = if @virtual_path
|
||||
base = @virtual_path[-1] == "/" ? "" : ::File.basename(@virtual_path)
|
||||
base = @virtual_path.end_with?("/") ? "" : ::File.basename(@virtual_path)
|
||||
base =~ /\A_?(.*?)(?:\.\w+)*\z/
|
||||
$1.to_sym
|
||||
end
|
||||
|
|
|
@ -200,7 +200,7 @@ behavior out of the box:
|
|||
attr_accessor :first_name, :last_name
|
||||
|
||||
validates_each :first_name, :last_name do |record, attr, value|
|
||||
record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
||||
record.errors.add attr, "starts with z." if value.start_with?("z")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ module ActiveModel
|
|||
# attr_accessor :first_name, :last_name
|
||||
#
|
||||
# validates_each :first_name, :last_name do |record, attr, value|
|
||||
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
||||
# record.errors.add attr, "starts with z." if value.start_with?("z")
|
||||
# end
|
||||
# end
|
||||
#
|
||||
|
@ -61,7 +61,7 @@ module ActiveModel
|
|||
# attr_accessor :first_name, :last_name
|
||||
#
|
||||
# validates_each :first_name, :last_name, allow_blank: true do |record, attr, value|
|
||||
# record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
|
||||
# record.errors.add attr, "starts with z." if value.start_with?("z")
|
||||
# end
|
||||
# end
|
||||
#
|
||||
|
|
|
@ -14,7 +14,7 @@ module ActiveRecord
|
|||
def cast(value)
|
||||
case value
|
||||
when ::String
|
||||
if value[0] == "(" && value[-1] == ")"
|
||||
if value.start_with?("(") && value.end_with?(")")
|
||||
value = value[1...-1]
|
||||
end
|
||||
cast(value.split(","))
|
||||
|
|
|
@ -18,7 +18,7 @@ module ActiveRecord
|
|||
when ::String
|
||||
return if value.blank?
|
||||
|
||||
if value[0] == "(" && value[-1] == ")"
|
||||
if value.start_with?("(") && value.end_with?(")")
|
||||
value = value[1...-1]
|
||||
end
|
||||
x, y = value.split(",")
|
||||
|
|
|
@ -67,12 +67,12 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def extract_bounds(value)
|
||||
from, to = value[1..-2].split(",")
|
||||
from, to = value[1..-2].split(",", 2)
|
||||
{
|
||||
from: (value[1] == "," || from == "-infinity") ? infinity(negative: true) : from,
|
||||
to: (value[-2] == "," || to == "infinity") ? infinity : to,
|
||||
exclude_start: (value[0] == "("),
|
||||
exclude_end: (value[-1] == ")")
|
||||
from: (from == "" || from == "-infinity") ? infinity(negative: true) : from,
|
||||
to: (to == "" || to == "infinity") ? infinity : to,
|
||||
exclude_start: value.start_with?("("),
|
||||
exclude_end: value.end_with?(")")
|
||||
}
|
||||
end
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/symbol/starts_ends_with"
|
||||
|
||||
module ActiveSupport
|
||||
# Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check
|
||||
# its string-like contents:
|
||||
|
@ -34,11 +36,11 @@ module ActiveSupport
|
|||
|
||||
private
|
||||
def respond_to_missing?(name, include_private = false)
|
||||
(name[-1] == "?") || super
|
||||
name.end_with?("?") || super
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
if name[-1] == "?"
|
||||
if name.end_with?("?")
|
||||
any?(name[0..-2])
|
||||
else
|
||||
super
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "active_support/core_ext/symbol/starts_ends_with"
|
||||
|
||||
module ActiveSupport
|
||||
# Wrapping a string in this class gives you a prettier way to test
|
||||
# for equality. The value returned by <tt>Rails.env</tt> is wrapped
|
||||
|
@ -19,11 +21,11 @@ module ActiveSupport
|
|||
class StringInquirer < String
|
||||
private
|
||||
def respond_to_missing?(method_name, include_private = false)
|
||||
(method_name[-1] == "?") || super
|
||||
method_name.end_with?("?") || super
|
||||
end
|
||||
|
||||
def method_missing(method_name, *arguments)
|
||||
if method_name[-1] == "?"
|
||||
if method_name.end_with?("?")
|
||||
self == method_name[0..-2]
|
||||
else
|
||||
super
|
||||
|
|
|
@ -109,7 +109,7 @@ module Rails
|
|||
results = {}
|
||||
|
||||
Dir.glob("#{dir}/*") do |item|
|
||||
next if File.basename(item)[0] == ?.
|
||||
next if File.basename(item).start_with?(".")
|
||||
|
||||
if File.directory?(item)
|
||||
results.update(find_in(item))
|
||||
|
|
Loading…
Reference in New Issue