File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -278,7 +278,7 @@ def visit_String o
278
278
style = Nodes ::Scalar ::FOLDED
279
279
elsif o =~ /^[^[:word:]][^"]*$/
280
280
style = Nodes ::Scalar ::DOUBLE_QUOTED
281
- elsif not String === @ss . tokenize ( o ) or / \A 0[0-7]*[89]/ =~ o
281
+ elsif not String === @ss . tokenize ( o ) or may_be_confused_as_yaml12? ( o )
282
282
style = Nodes ::Scalar ::SINGLE_QUOTED
283
283
end
284
284
@@ -392,6 +392,19 @@ def binary? string
392
392
string . encoding == Encoding ::ASCII_8BIT && !string . ascii_only?
393
393
end
394
394
395
+ def may_be_confused_as_yaml12? string
396
+ case string
397
+ when /\A 0[0-7]*[89]\z / # YAML 1.1 int (Base 8)
398
+ true
399
+ when /\A 0o[0-7]+\z / # YAML 1.2 int (Base 8)
400
+ true
401
+ when /\A [-+]?(\. [0-9]+|[0-9]+(\. [0-9]*)?)([eE][-+]?[0-9]+)?\z / # YAML 1.2 float (Number)
402
+ true
403
+ else
404
+ false
405
+ end
406
+ end
407
+
395
408
def visit_array_subclass o
396
409
tag = "!ruby/array:#{ o . class } "
397
410
ivars = o . instance_variables
Original file line number Diff line number Diff line change @@ -167,9 +167,20 @@ def test_float
167
167
end
168
168
169
169
def test_string
170
+ # YAML 1.1 int Base 8
170
171
assert_match ( /'017'/ , Psych . dump ( { 'a' => '017' } ) )
171
172
assert_match ( /'019'/ , Psych . dump ( { 'a' => '019' } ) )
172
173
assert_match ( /'01818'/ , Psych . dump ( { 'a' => '01818' } ) )
174
+
175
+ # YAML 1.2 int Base 8
176
+ assert_match ( /'0o17'/ , Psych . dump ( { 'a' => '0o17' } ) )
177
+ assert_match ( /'0o19'/ , Psych . dump ( { 'a' => '0o19' } ) )
178
+ assert_match ( /'0o111'/ , Psych . dump ( { 'a' => '0o111' } ) )
179
+
180
+ # YAML 1.2 float Number
181
+ assert_match ( /'12e03'/ , Psych . dump ( { 'a' => '12e03' } ) )
182
+ assert_match ( /'1.2e3'/ , Psych . dump ( { 'a' => '1.2e3' } ) )
183
+ assert_match ( /'.2e3'/ , Psych . dump ( { 'a' => '.2e3' } ) )
173
184
end
174
185
175
186
# http://yaml.org/type/null.html
You can’t perform that action at this time.
0 commit comments