summaryrefslogtreecommitdiff
path: root/ext/psych/lib
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-17 03:11:22 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-11-17 03:11:22 +0000
commit24c6a7c3680b7a4b40526e28391fabe5e5c36ce3 (patch)
tree6cc9b7380091af1daf784c7cabcaec450389f220 /ext/psych/lib
parent262cb379d6d2fa6500faa90cfdfa54ae044d2296 (diff)
* ext/psych/lib/psych/scalar_scanner.rb: avoid raising exceptions when
parsing Floats and Integers. Thanks riffraff [ruby-core:44426] * test/psych/test_numeric.rb: associated test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37697 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/psych/lib')
-rw-r--r--ext/psych/lib/psych/scalar_scanner.rb34
1 files changed, 21 insertions, 13 deletions
diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb
index 41e889a528..d0beee3626 100644
--- a/ext/psych/lib/psych/scalar_scanner.rb
+++ b/ext/psych/lib/psych/scalar_scanner.rb
@@ -8,11 +8,17 @@ module Psych
TIME = /^\d{4}-\d{1,2}-\d{1,2}([Tt]|\s+)\d{1,2}:\d\d:\d\d(\.\d*)?(\s*Z|[-+]\d{1,2}(:\d\d)?)?/
# Taken from http://yaml.org/type/float.html
- FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9.]*([eE][-+][0-9]+)?(?# base 10)
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10)
|[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60)
|[-+]?\.(inf|Inf|INF)(?# infinity)
|\.(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
+
# Create a new scanner
def initialize
@string_cache = {}
@@ -86,20 +92,15 @@ module Psych
end
i
when FLOAT
- begin
- return Float(string.gsub(/[,_]/, ''))
- rescue ArgumentError
+ if string == '.'
+ @string_cache[string] = true
+ string
+ else
+ Float(string.gsub(/[,_]/, ''))
end
-
- @string_cache[string] = true
- string
else
- if string.count('.') < 2
- begin
- return Integer(string.gsub(/[,_]/, ''))
- rescue ArgumentError
- end
- end
+ int = parse_int string.gsub(/[,_]/, '')
+ return int if int
@string_cache[string] = true
string
@@ -107,6 +108,13 @@ module Psych
end
###
+ # Parse and return an int from +string+
+ def parse_int string
+ return unless INTEGER === string
+ Integer(string)
+ end
+
+ ###
# Parse and return a Time from +string+
def parse_time string
date, time = *(string.split(/[ tT]/, 2))