Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into embed

* master:
  * 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
  * ext/psych/lib/psych/core_ext.rb: move Kernel#y so that it can   manually be required as 'psych/y'.
  Use literal style when emitting multiline strings, fixes #64
  • Loading branch information...
commit 249f9080826509deb39283a39e2fc4512e1316c3 2 parents 4cd1c16 + eb029f0
@tenderlove authored
View
13 CHANGELOG.rdoc
@@ -1,3 +1,16 @@
+Sat Nov 17 12:03:41 2012 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * 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
+
+Sat Nov 17 11:26:36 2012 Aaron Patterson <aaron@tenderlovemaking.com>
+
+ * ext/psych/lib/psych/core_ext.rb: move Kernel#y so that it can
+ manually be required as 'psych/y'.
+
+ * ext/psych/lib/psych/y.rb: ditto
+
Tue Nov 6 09:37:57 2012 NARUSE, Yui <naruse@ruby-lang.org>
* ruby.c (load_file_internal): set default source encoding as
View
9 lib/psych/core_ext.rb
@@ -31,12 +31,5 @@ def psych_yaml_as url
end
if defined?(::IRB)
-module Kernel
- def psych_y *objects
- puts Psych.dump_stream(*objects)
- end
- remove_method :y rescue nil
- alias y psych_y
- private :y
-end
+ require 'psych/y'
end
View
34 lib/psych/scalar_scanner.rb
@@ -8,11 +8,17 @@ class ScalarScanner
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 @@ def tokenize string
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 @@ def tokenize string
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))
View
7 lib/psych/visitors/yaml_tree.rb
@@ -231,15 +231,18 @@ def visit_String o
plain = false
quote = false
style = Nodes::Scalar::ANY
+ tag = nil
+ str = o
if binary?(o)
str = [o].pack('m').chomp
tag = '!binary' # FIXME: change to below when syck is removed
#tag = 'tag:yaml.org,2002:binary'
style = Nodes::Scalar::LITERAL
+ elsif o =~ /\n/
+ quote = true
+ style = Nodes::Scalar::LITERAL
else
- str = o
- tag = nil
quote = !(String === @ss.tokenize(o))
plain = !quote
end
View
7 lib/psych/y.rb
@@ -0,0 +1,7 @@
+module Kernel
+ def y *objects
+ puts Psych.dump_stream(*objects)
+ end
+ private :y
+end
+
View
16 test/psych/test_numeric.rb
@@ -7,6 +7,15 @@ module Psych
# http://yaml.org/type/float.html
# http://yaml.org/type/int.html
class TestNumeric < TestCase
+ def setup
+ @old_debug = $DEBUG
+ $DEBUG = true
+ end
+
+ def teardown
+ $DEBUG = @old_debug
+ end
+
def test_non_float_with_0
str = Psych.load('--- 090')
assert_equal '090', str
@@ -21,5 +30,12 @@ def test_big_decimal_round_trip
decimal = BigDecimal("12.34")
assert_cycle decimal
end
+
+ def test_does_not_attempt_numeric
+ str = Psych.load('--- 4 roses')
+ assert_equal '4 roses', str
+ str = Psych.load('--- 1.1.1')
+ assert_equal '1.1.1', str
+ end
end
end
View
5 test/psych/test_yaml.rb
@@ -1266,4 +1266,9 @@ def test_normal_exit
Psych.load("2000-01-01 00:00:00.#{"0"*1000} +00:00\n")
# '[ruby-core:13735]'
end
+
+ def test_multiline_string_uses_literal_style
+ yaml = Psych.dump("multi\nline\nstring")
+ assert_match("|", yaml)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.