зеркало из https://github.com/github/ruby.git
Reduce string allocations in scalar_scanner
This commit is contained in:
Родитель
00d0ede845
Коммит
63f78bbaae
|
@ -34,68 +34,66 @@ module Psych
|
|||
return string if @string_cache.key?(string)
|
||||
return @symbol_cache[string] if @symbol_cache.key?(string)
|
||||
|
||||
case string
|
||||
# Check for a String type, being careful not to get caught by hash keys, hex values, and
|
||||
# special floats (e.g., -.inf).
|
||||
when /^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/, /\n/
|
||||
if string.match?(/^[^\d\.:-]?[A-Za-z_\s!@#\$%\^&\*\(\)\{\}\<\>\|\/\\~;=]+/) || string.match?(/\n/)
|
||||
if string.length > 5
|
||||
@string_cache[string] = true
|
||||
return string
|
||||
end
|
||||
|
||||
case string
|
||||
when /^[^ytonf~]/i
|
||||
if string.match?(/^[^ytonf~]/i)
|
||||
@string_cache[string] = true
|
||||
string
|
||||
when '~', /^null$/i
|
||||
elsif string == '~' || string.match?(/^null$/i)
|
||||
nil
|
||||
when /^(yes|true|on)$/i
|
||||
elsif string.match?(/^(yes|true|on)$/i)
|
||||
true
|
||||
when /^(no|false|off)$/i
|
||||
elsif string.match?(/^(no|false|off)$/i)
|
||||
false
|
||||
else
|
||||
@string_cache[string] = true
|
||||
string
|
||||
end
|
||||
when TIME
|
||||
elsif string.match?(TIME)
|
||||
begin
|
||||
parse_time string
|
||||
rescue ArgumentError
|
||||
string
|
||||
end
|
||||
when /^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/
|
||||
elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
|
||||
require 'date'
|
||||
begin
|
||||
class_loader.date.strptime(string, '%Y-%m-%d')
|
||||
rescue ArgumentError
|
||||
string
|
||||
end
|
||||
when /^\.inf$/i
|
||||
elsif string.match?(/^\.inf$/i)
|
||||
Float::INFINITY
|
||||
when /^-\.inf$/i
|
||||
elsif string.match?(/^-\.inf$/i)
|
||||
-Float::INFINITY
|
||||
when /^\.nan$/i
|
||||
elsif string.match?(/^\.nan$/i)
|
||||
Float::NAN
|
||||
when /^:./
|
||||
elsif string.match?(/^:./)
|
||||
if string =~ /^:(["'])(.*)\1/
|
||||
@symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
|
||||
else
|
||||
@symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
|
||||
end
|
||||
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/
|
||||
elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
|
||||
i = 0
|
||||
string.split(':').each_with_index do |n,e|
|
||||
i += (n.to_i * 60 ** (e - 2).abs)
|
||||
end
|
||||
i
|
||||
when /^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/
|
||||
elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
|
||||
i = 0
|
||||
string.split(':').each_with_index do |n,e|
|
||||
i += (n.to_f * 60 ** (e - 2).abs)
|
||||
end
|
||||
i
|
||||
when FLOAT
|
||||
if string =~ /\A[-+]?\.\Z/
|
||||
elsif string.match?(FLOAT)
|
||||
if string.match?(/\A[-+]?\.\Z/)
|
||||
@string_cache[string] = true
|
||||
string
|
||||
else
|
||||
|
|
Загрузка…
Ссылка в новой задаче