ruby-changes:21334
From: tenderlove <ko1@a...>
Date: Tue, 4 Oct 2011 06:21:50 +0900 (JST)
Subject: [ruby-changes:21334] tenderlove:r33383 (trunk): * ext/psych/lib/psych/scalar_scanner.rb: Match values against the
tenderlove 2011-10-04 06:21:31 +0900 (Tue, 04 Oct 2011) New Revision: 33383 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33383 Log: * 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. Added files: trunk/test/psych/test_numeric.rb Modified files: trunk/ChangeLog trunk/ext/psych/lib/psych/scalar_scanner.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 33382) +++ ChangeLog (revision 33383) @@ -1,3 +1,9 @@ +Tue Oct 4 06:20:19 2011 Aaron Patterson <aaron@t...> + + * 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. + Tue Oct 4 05:59:24 2011 Aaron Patterson <aaron@t...> * ext/psych/lib/psych/visitors/to_ruby.rb: ToRuby visitor can be Index: ext/psych/lib/psych/scalar_scanner.rb =================================================================== --- ext/psych/lib/psych/scalar_scanner.rb (revision 33382) +++ ext/psych/lib/psych/scalar_scanner.rb (revision 33383) @@ -7,6 +7,12 @@ # 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 @@ 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 Index: test/psych/test_numeric.rb =================================================================== --- test/psych/test_numeric.rb (revision 0) +++ test/psych/test_numeric.rb (revision 33383) @@ -0,0 +1,14 @@ +require 'psych/helper' + +module Psych + ### + # Test numerics from YAML spec: + # http://yaml.org/type/float.html + # http://yaml.org/type/int.html + class TestNumeric < TestCase + def test_non_float_with_0 + str = Psych.load('--- 090') + assert_equal '090', str + end + end +end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/