diff options
author | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-03 21:21:31 +0000 |
---|---|---|
committer | tenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-03 21:21:31 +0000 |
commit | bd3b0d470c12fbdb48efcdb310d00eaa0fea84e5 (patch) | |
tree | 7504076747c9c32e5c4305a282445b73bd2045dc /ext | |
parent | 1721fca3ada1c19211b01d7c8b18dac5e509fc0e (diff) | |
download | ruby-bd3b0d470c12fbdb48efcdb310d00eaa0fea84e5.tar.gz |
* ext/psych/lib/psych/scalar_scanner.rb: Match values against the
floating point spec defined in YAML to avoid erronious parses.
* test/psych/test_numeric.rb: corresponding test.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/psych/lib/psych/scalar_scanner.rb | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/ext/psych/lib/psych/scalar_scanner.rb b/ext/psych/lib/psych/scalar_scanner.rb index b92d3c075e..3e8acbb21c 100644 --- a/ext/psych/lib/psych/scalar_scanner.rb +++ b/ext/psych/lib/psych/scalar_scanner.rb @@ -7,6 +7,12 @@ module Psych # Taken from http://yaml.org/type/timestamp.html 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) + |[-+]?[0-9][0-9_,]*(:[0-5]?[0-9])+\.[0-9_]*(?# base 60) + |[-+]?\.(inf|Inf|INF)(?# infinity) + |\.(nan|NaN|NAN)(?# not a number))$/x + # Create a new scanner def initialize @string_cache = {} @@ -67,10 +73,14 @@ module Psych i += (n.to_f * 60 ** (e - 2).abs) end i + when FLOAT + return Float(string.gsub(/[,_]/, '')) rescue ArgumentError + + @string_cache[string] = true + string else if string.count('.') < 2 return Integer(string.gsub(/[,_]/, '')) rescue ArgumentError - return Float(string.gsub(/[,_]/, '')) rescue ArgumentError end @string_cache[string] = true |