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

* 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
This commit is contained in:
nobu 2010-05-28 09:39:41 +00:00
parent 2138c773cc
commit 732e40d9b0
3 changed files with 30 additions and 9 deletions

View file

@ -1,4 +1,6 @@
Fri May 28 18:37:04 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
Fri May 28 18:39:38 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (sym_inspect): escape ASCII-compatible strings.
* string.c (rb_str_inspect): escape ASCII-compatible strings.

View file

@ -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;
}

View file

@ -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