mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Do not allocate a string to check if a scalar is an integer
This commit is contained in:
parent
5a4d2b9f2f
commit
746812ee96
2 changed files with 24 additions and 9 deletions
|
@ -14,10 +14,10 @@ module Psych
|
|||
|\.(nan|NaN|NAN)(?# not a number))$/x
|
||||
|
||||
# Taken from http://yaml.org/type/int.html
|
||||
INTEGER = /^(?:[-+]?0b[0-1_]+ (?# base 2)
|
||||
|[-+]?0[0-7_]+ (?# base 8)
|
||||
|[-+]?(?:0|[1-9][0-9_]*) (?# base 10)
|
||||
|[-+]?0x[0-9a-fA-F_]+ (?# base 16))$/x
|
||||
INTEGER = /^(?:[-+]?0b[0-1_,]+ (?# base 2)
|
||||
|[-+]?0[0-7_,]+ (?# base 8)
|
||||
|[-+]?(?:0|[1-9][0-9_,]*) (?# base 10)
|
||||
|[-+]?0x[0-9a-fA-F_,]+ (?# base 16))$/x
|
||||
|
||||
attr_reader :class_loader
|
||||
|
||||
|
@ -91,10 +91,9 @@ module Psych
|
|||
else
|
||||
Float(string.gsub(/[,_]|\.([Ee]|$)/, '\1'))
|
||||
end
|
||||
elsif string.match?(INTEGER)
|
||||
parse_int string
|
||||
else
|
||||
int = parse_int string.gsub(/[,_]/, '')
|
||||
return int if int
|
||||
|
||||
string
|
||||
end
|
||||
end
|
||||
|
@ -102,8 +101,7 @@ module Psych
|
|||
###
|
||||
# Parse and return an int from +string+
|
||||
def parse_int string
|
||||
return unless INTEGER === string
|
||||
Integer(string)
|
||||
Integer(string.gsub(/[,]/, ''))
|
||||
end
|
||||
|
||||
###
|
||||
|
|
|
@ -113,5 +113,22 @@ module Psych
|
|||
def test_scan_strings_starting_with_underscores
|
||||
assert_equal "_100", ss.tokenize('_100')
|
||||
end
|
||||
|
||||
def test_scan_int_commas_and_underscores
|
||||
# NB: This test is to ensure backward compatibility with prior Psych versions,
|
||||
# not to test against any actual YAML specification.
|
||||
assert_equal 123_456_789, ss.tokenize('123_456_789')
|
||||
assert_equal 123_456_789, ss.tokenize('123,456,789')
|
||||
assert_equal 123_456_789, ss.tokenize('1_2,3,4_5,6_789')
|
||||
|
||||
assert_equal 0b010101010, ss.tokenize('0b010101010')
|
||||
assert_equal 0b010101010, ss.tokenize('0b0,1_0,1_,0,1_01,0')
|
||||
|
||||
assert_equal 01234567, ss.tokenize('01234567')
|
||||
assert_equal 01234567, ss.tokenize('0_,,,1_2,_34567')
|
||||
|
||||
assert_equal 0x123456789abcdef, ss.tokenize('0x123456789abcdef')
|
||||
assert_equal 0x123456789abcdef, ss.tokenize('0x12_,34,_56,_789abcdef')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue