ruby-changes:66878
From: Nobuyoshi <ko1@a...>
Date: Sat, 24 Jul 2021 17:58:56 +0900 (JST)
Subject: [ruby-changes:66878] eec45a93ef (master): Escape unprintable chars only, without surrounding quotes
https://git.ruby-lang.org/ruby.git/commit/?id=eec45a93ef From eec45a93effe69a821960839760b9e39be1ebc63 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Sat, 24 Jul 2021 14:24:18 +0900 Subject: Escape unprintable chars only, without surrounding quotes --- pack.c | 6 +----- sprintf.c | 2 +- string.c | 4 ++-- test/-ext-/test_printf.rb | 4 ++-- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pack.c b/pack.c index 9fecc31..4403465 100644 --- a/pack.c +++ b/pack.c @@ -141,7 +141,6 @@ str_associated(VALUE str) https://github.com/ruby/ruby/blob/trunk/pack.c#L141 static void unknown_directive(const char *mode, char type, VALUE fmt) { - VALUE f; char unknown[5]; if (ISPRINT(type)) { @@ -151,10 +150,7 @@ unknown_directive(const char *mode, char type, VALUE fmt) https://github.com/ruby/ruby/blob/trunk/pack.c#L150 else { snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff); } - f = rb_str_quote_unprintable(fmt); - if (f != fmt) { - fmt = rb_str_subseq(f, 1, RSTRING_LEN(f) - 2); - } + fmt = rb_str_quote_unprintable(fmt); rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'", mode, unknown, fmt); } diff --git a/sprintf.c b/sprintf.c index f54336f..693b30b 100644 --- a/sprintf.c +++ b/sprintf.c @@ -1121,7 +1121,7 @@ ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int s https://github.com/ruby/ruby/blob/trunk/sprintf.c#L1121 else if (SYMBOL_P(value)) { value = rb_sym2str(value); if (sign == ' ' && !rb_str_symname_p(value)) { - value = rb_str_inspect(value); + value = rb_str_escape(value); } } else { diff --git a/string.c b/string.c index 864fcc8..0d6b02b 100644 --- a/string.c +++ b/string.c @@ -11050,7 +11050,7 @@ rb_str_quote_unprintable(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L11050 len = RSTRING_LEN(str); if ((resenc != enc && !rb_str_is_ascii_only_p(str)) || !sym_printable(ptr, ptr + len, enc)) { - return rb_str_inspect(str); + return rb_str_escape(str); } return str; } @@ -11060,7 +11060,7 @@ rb_id_quote_unprintable(ID id) https://github.com/ruby/ruby/blob/trunk/string.c#L11060 { VALUE str = rb_id2str(id); if (!rb_str_symname_p(str)) { - return rb_str_inspect(str); + return rb_str_escape(str); } return str; } diff --git a/test/-ext-/test_printf.rb b/test/-ext-/test_printf.rb index cfec388..c2b5028 100644 --- a/test/-ext-/test_printf.rb +++ b/test/-ext-/test_printf.rb @@ -21,11 +21,11 @@ class Test_SPrintf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/test_printf.rb#L21 end def test_quote - assert_equal('["\n"]', Bug::Printf.q("\n")) + assert_equal('[\n]', Bug::Printf.q("\n")) assert_equal('[aaa]', Bug::Printf.q('aaa')) assert_equal('[a a]', Bug::Printf.q('a a')) assert_equal('[]', Bug::Printf.q('')) - assert_equal('[""]', Bug::Printf.q(:'')) + assert_equal('[]', Bug::Printf.q(:'')) end def test_encoding -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/