Project

General

Profile

Bug #15718 ยป yamldumputf32encodingerror.patch

rubenochiavone (Ruben Chiavone), 03/21/2019 02:11 PM

View differences:

ext/psych/lib/psych/visitors/yaml_tree.rb (working copy)
288 288
        style = Nodes::Scalar::PLAIN
289 289
        tag   = nil
290 290

  
291
        # regexps
292
        literal_regexp = /\n(?!\Z)/
293
        double_quoted_regexp = /^[^[:word:]][^"]*$/
294
        single_quoted_regexp = /\A0[0-7]*[89]/
295

  
291 296
        if binary?(o)
292 297
          o     = [o].pack('m0')
293 298
          tag   = '!binary' # FIXME: change to below when syck is removed
......
295 300
          style = Nodes::Scalar::LITERAL
296 301
          plain = false
297 302
          quote = false
298
        elsif o =~ /\n(?!\Z)/  # match \n except blank line at the end of string
303
        # match \n except blank line at the end of string
304
        elsif string_match? o, literal_regexp
299 305
          style = Nodes::Scalar::LITERAL
300 306
        elsif o == '<<'
301 307
          style = Nodes::Scalar::SINGLE_QUOTED
......
304 310
          quote = false
305 311
        elsif @line_width && o.length > @line_width
306 312
          style = Nodes::Scalar::FOLDED
307
        elsif o =~ /^[^[:word:]][^"]*$/
313
        elsif string_match? o, double_quoted_regexp
308 314
          style = Nodes::Scalar::DOUBLE_QUOTED
309
        elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/ =~ o
315
        elsif not String === @ss.tokenize(o) or string_match? o, single_quoted_regexp
310 316
          style = Nodes::Scalar::SINGLE_QUOTED
311 317
        end
312 318

  
......
420 426
        string.encoding == Encoding::ASCII_8BIT && !string.ascii_only?
421 427
      end
422 428

  
429
      def string_match? string, regexp
430
        (string.encoding.ascii_compatible? || string.encoding == regexp.encoding) && string =~ regexp
431
      end
432

  
423 433
      def visit_array_subclass o
424 434
        tag = "!ruby/array:#{o.class}"
425 435
        ivars = o.instance_variables
test/yaml/test_dump.rb (working copy)
1
# frozen_string_literal: true
2
require 'test/unit'
3
require 'yaml'
4
require 'tmpdir'
5

  
6
class YAMLDumpTest < Test::Unit::TestCase
7
  def test_dump_with_empty_utf32le_string
8
    bug15718 = '[ruby-dev:91903]'
9
    assert_nothing_raised bug15718 do
10
      YAML.dump(''.dup.force_encoding('UTF-32LE'))
11
    end
12
  end
13
end