Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
tenderlove committed Nov 17, 2012
2 parents 4cd1c16 + eb029f0 commit 249f908
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
13 changes: 13 additions & 0 deletions 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
Expand Down
9 changes: 1 addition & 8 deletions lib/psych/core_ext.rb
Expand Up @@ -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
34 changes: 21 additions & 13 deletions lib/psych/scalar_scanner.rb
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -86,26 +92,28 @@ 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
end
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
Expand Down
7 changes: 5 additions & 2 deletions lib/psych/visitors/yaml_tree.rb
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/psych/y.rb
@@ -0,0 +1,7 @@
module Kernel
def y *objects
puts Psych.dump_stream(*objects)
end
private :y
end

16 changes: 16 additions & 0 deletions test/psych/test_numeric.rb
Expand Up @@ -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
Expand All @@ -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
5 changes: 5 additions & 0 deletions test/psych/test_yaml.rb
Expand Up @@ -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

0 comments on commit 249f908

Please sign in to comment.