1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Prefer no allocation start/end_with? over String#[] ==

This commit is contained in:
Ryuta Kamizono 2020-05-29 10:20:13 +09:00
parent 97c3a5605b
commit c65864cdca
11 changed files with 22 additions and 18 deletions

View file

@ -170,7 +170,7 @@ module ActionView
def explicit_dependencies def explicit_dependencies
dependencies = source.scan(EXPLICIT_DEPENDENCY).flatten.uniq 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 (explicits + resolve_directories(wildcards)).uniq
end end

View file

@ -46,7 +46,7 @@ module ActionView
as.to_sym as.to_sym
else else
begin begin
base = path[-1] == "/" ? "" : File.basename(path) base = path.end_with?("/") ? "" : File.basename(path)
raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/ raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/
$1.to_sym $1.to_sym
end end

View file

@ -138,7 +138,7 @@ module ActionView
@virtual_path = virtual_path @virtual_path = virtual_path
@variable = if @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/ base =~ /\A_?(.*?)(?:\.\w+)*\z/
$1.to_sym $1.to_sym
end end

View file

@ -200,7 +200,7 @@ behavior out of the box:
attr_accessor :first_name, :last_name attr_accessor :first_name, :last_name
validates_each :first_name, :last_name do |record, attr, value| 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
end end

View file

@ -15,7 +15,7 @@ module ActiveModel
# attr_accessor :first_name, :last_name # attr_accessor :first_name, :last_name
# #
# validates_each :first_name, :last_name do |record, attr, value| # 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
# end # end
# #
@ -61,7 +61,7 @@ module ActiveModel
# attr_accessor :first_name, :last_name # attr_accessor :first_name, :last_name
# #
# validates_each :first_name, :last_name, allow_blank: true do |record, attr, value| # 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
# end # end
# #

View file

@ -14,7 +14,7 @@ module ActiveRecord
def cast(value) def cast(value)
case value case value
when ::String when ::String
if value[0] == "(" && value[-1] == ")" if value.start_with?("(") && value.end_with?(")")
value = value[1...-1] value = value[1...-1]
end end
cast(value.split(",")) cast(value.split(","))

View file

@ -18,7 +18,7 @@ module ActiveRecord
when ::String when ::String
return if value.blank? return if value.blank?
if value[0] == "(" && value[-1] == ")" if value.start_with?("(") && value.end_with?(")")
value = value[1...-1] value = value[1...-1]
end end
x, y = value.split(",") x, y = value.split(",")

View file

@ -67,12 +67,12 @@ module ActiveRecord
end end
def extract_bounds(value) 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, from: (from == "" || from == "-infinity") ? infinity(negative: true) : from,
to: (value[-2] == "," || to == "infinity") ? infinity : to, to: (to == "" || to == "infinity") ? infinity : to,
exclude_start: (value[0] == "("), exclude_start: value.start_with?("("),
exclude_end: (value[-1] == ")") exclude_end: value.end_with?(")")
} }
end end

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"
module ActiveSupport module ActiveSupport
# Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check # Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check
# its string-like contents: # its string-like contents:
@ -34,11 +36,11 @@ module ActiveSupport
private private
def respond_to_missing?(name, include_private = false) def respond_to_missing?(name, include_private = false)
(name[-1] == "?") || super name.end_with?("?") || super
end end
def method_missing(name, *args) def method_missing(name, *args)
if name[-1] == "?" if name.end_with?("?")
any?(name[0..-2]) any?(name[0..-2])
else else
super super

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
require "active_support/core_ext/symbol/starts_ends_with"
module ActiveSupport module ActiveSupport
# Wrapping a string in this class gives you a prettier way to test # 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 # for equality. The value returned by <tt>Rails.env</tt> is wrapped
@ -19,11 +21,11 @@ module ActiveSupport
class StringInquirer < String class StringInquirer < String
private private
def respond_to_missing?(method_name, include_private = false) def respond_to_missing?(method_name, include_private = false)
(method_name[-1] == "?") || super method_name.end_with?("?") || super
end end
def method_missing(method_name, *arguments) def method_missing(method_name, *arguments)
if method_name[-1] == "?" if method_name.end_with?("?")
self == method_name[0..-2] self == method_name[0..-2]
else else
super super

View file

@ -109,7 +109,7 @@ module Rails
results = {} results = {}
Dir.glob("#{dir}/*") do |item| Dir.glob("#{dir}/*") do |item|
next if File.basename(item)[0] == ?. next if File.basename(item).start_with?(".")
if File.directory?(item) if File.directory?(item)
results.update(find_in(item)) results.update(find_in(item))