From 732e40d9b07c786850f60cee022941a24d081f50 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 28 May 2010 09:39:41 +0000 Subject: [PATCH] * string.c (sym_inspect): escape ASCII-compatible strings. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 +++- string.c | 28 ++++++++++++++++++++-------- test/ruby/test_symbol.rb | 7 +++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/ChangeLog b/ChangeLog index d8c5187992..31ea049a55 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,6 @@ -Fri May 28 18:37:04 2010 Nobuyoshi Nakada +Fri May 28 18:39:38 2010 Nobuyoshi Nakada + + * string.c (sym_inspect): escape ASCII-compatible strings. * string.c (rb_str_inspect): escape ASCII-compatible strings. diff --git a/string.c b/string.c index 0962d0fba3..f2f49abaff 100644 --- a/string.c +++ b/string.c @@ -7063,17 +7063,29 @@ sym_inspect(VALUE sym) VALUE str; ID id = SYM2ID(sym); rb_encoding *enc; + const char *ptr; + long len; + char *dest; sym = rb_id2str(id); enc = STR_ENC_GET(sym); - str = rb_enc_str_new(0, RSTRING_LEN(sym)+1, enc); - RSTRING_PTR(str)[0] = ':'; - memcpy(RSTRING_PTR(str)+1, RSTRING_PTR(sym), RSTRING_LEN(sym)); - if (RSTRING_LEN(sym) != (long)strlen(RSTRING_PTR(sym)) || - !rb_enc_symname_p(RSTRING_PTR(sym), enc) || - !sym_printable(RSTRING_PTR(sym), RSTRING_END(sym), enc)) { - str = rb_str_inspect(str); - memcpy(RSTRING_PTR(str), ":\"", 2); + ptr = RSTRING_PTR(sym); + len = RSTRING_LEN(sym); + if (!rb_enc_asciicompat(enc) || len != (long)strlen(ptr) || + !rb_enc_symname_p(ptr, enc) || !sym_printable(ptr, ptr + len, enc)) { + str = rb_str_inspect(sym); + len = RSTRING_LEN(str); + rb_str_resize(str, len + 1); + dest = RSTRING_PTR(str); + memmove(dest + 1, dest, len); + dest[0] = ':'; + } + else { + char *dest; + str = rb_enc_str_new(0, len + 1, enc); + dest = RSTRING_PTR(str); + dest[0] = ':'; + memcpy(dest + 1, ptr, len); } return str; } diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index 0f09669293..07716821f4 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -136,4 +136,11 @@ class TestSymbol < Test::Unit::TestCase def test_symbol_poped assert_nothing_raised { eval('a = 1; :"#{ a }"; 1') } end + + def test_ascii_incomat_inspect + [Encoding::UTF_16LE, Encoding::UTF_16BE, + Encoding::UTF_32LE, Encoding::UTF_32BE].each do |e| + assert_equal(':"\\u0061\\u0062\\u0063"', "abc".encode(e).to_sym.inspect) + end + end end