From eb9031b0206f45486eb6cccafff970547673eed5 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 19 Apr 2013 15:16:30 +0900 Subject: [PATCH] * string.c (rb_str_inspect): NUL should not be represented as "\0" when octal digits may follow. --- ChangeLog | 5 +++++ string.c | 6 +++++- test/ruby/test_string.rb | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index cccecb0..72980a6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Apr 19 15:15:14 2013 Akinori MUSHA + + * string.c (rb_str_inspect): NUL should not be represented as + "\0" when octal digits may follow. + Fri Apr 19 13:03:14 2013 Nobuyoshi Nakada * marshal.c (w_object): do not dump encoding which is dumped with diff --git a/string.c b/string.c index 0724326..d42a20d 100644 --- a/string.c +++ b/string.c @@ -4572,7 +4572,11 @@ rb_str_inspect(VALUE str) } } switch (c) { - case '\0': cc = '0'; break; + case '\0': + if (p - n > prev) str_buf_cat(result, prev, p - n - prev); + str_buf_cat(result, "\\000", 4); + prev = p; + continue; case '\n': cc = 'n'; break; case '\r': cc = 'r'; break; case '\t': cc = 't'; break; diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 4d9f7d9..f1ed82a 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1985,6 +1985,12 @@ class TestString < Test::Unit::TestCase assert_instance_of(String, s.to_s) end + def test_inspect_nul + s = "\0" + "12" + assert_not_equal '"\\012"', eval(s.inspect) + assert_equal s, eval(s.inspect) + end + def test_partition assert_equal(%w(he l lo), "hello".partition(/l/)) assert_equal(%w(he l lo), "hello".partition("l")) -- 1.8.2.1