1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Escape unprintable chars only, without surrounding quotes

This commit is contained in:
Nobuyoshi Nakada 2021-07-24 14:24:18 +09:00
parent 8cc18703cf
commit eec45a93ef
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2021-07-24 17:59:09 +09:00
4 changed files with 6 additions and 10 deletions

6
pack.c
View file

@ -141,7 +141,6 @@ str_associated(VALUE str)
static void static void
unknown_directive(const char *mode, char type, VALUE fmt) unknown_directive(const char *mode, char type, VALUE fmt)
{ {
VALUE f;
char unknown[5]; char unknown[5];
if (ISPRINT(type)) { if (ISPRINT(type)) {
@ -151,10 +150,7 @@ unknown_directive(const char *mode, char type, VALUE fmt)
else { else {
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff); snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
} }
f = rb_str_quote_unprintable(fmt); fmt = rb_str_quote_unprintable(fmt);
if (f != fmt) {
fmt = rb_str_subseq(f, 1, RSTRING_LEN(f) - 2);
}
rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'", rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'",
mode, unknown, fmt); mode, unknown, fmt);
} }

View file

@ -1121,7 +1121,7 @@ ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int s
else if (SYMBOL_P(value)) { else if (SYMBOL_P(value)) {
value = rb_sym2str(value); value = rb_sym2str(value);
if (sign == ' ' && !rb_str_symname_p(value)) { if (sign == ' ' && !rb_str_symname_p(value)) {
value = rb_str_inspect(value); value = rb_str_escape(value);
} }
} }
else { else {

View file

@ -11050,7 +11050,7 @@ rb_str_quote_unprintable(VALUE str)
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
if ((resenc != enc && !rb_str_is_ascii_only_p(str)) || if ((resenc != enc && !rb_str_is_ascii_only_p(str)) ||
!sym_printable(ptr, ptr + len, enc)) { !sym_printable(ptr, ptr + len, enc)) {
return rb_str_inspect(str); return rb_str_escape(str);
} }
return str; return str;
} }
@ -11060,7 +11060,7 @@ rb_id_quote_unprintable(ID id)
{ {
VALUE str = rb_id2str(id); VALUE str = rb_id2str(id);
if (!rb_str_symname_p(str)) { if (!rb_str_symname_p(str)) {
return rb_str_inspect(str); return rb_str_escape(str);
} }
return str; return str;
} }

View file

@ -21,11 +21,11 @@ class Test_SPrintf < Test::Unit::TestCase
end end
def test_quote 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('[aaa]', Bug::Printf.q('aaa'))
assert_equal('[a a]', Bug::Printf.q('a a')) assert_equal('[a a]', Bug::Printf.q('a a'))
assert_equal('[]', Bug::Printf.q('')) assert_equal('[]', Bug::Printf.q(''))
assert_equal('[""]', Bug::Printf.q(:'')) assert_equal('[]', Bug::Printf.q(:''))
end end
def test_encoding def test_encoding