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

Escape as \x{XXXX} other than Unicode chars.

* string.c (rb_str_inspect): escape as \x{XXXX} when the encoding is
  other than Unicode. [ruby-dev:39388]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25163 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2009-09-29 15:02:59 +00:00
parent e8f007e6b8
commit 5b2d54be66
3 changed files with 26 additions and 12 deletions

View file

@ -1,3 +1,8 @@
Wed Sep 30 00:00:25 2009 NARUSE, Yui <naruse@ruby-lang.org>
* string.c (rb_str_inspect): escape as \x{XXXX} when the encoding is
other than Unicode. [ruby-dev:39388]
Wed Sep 30 00:00:30 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in (THREAD_MODEL): modified message when no thread

View file

@ -4123,22 +4123,31 @@ rb_str_inspect(VALUE str)
char buf[11];
escape_codepoint:
if (unicode_p && c != -1) {
if (c > 0xFFFF) {
sprintf(buf, "\\u{%X}", c);
}
else {
sprintf(buf, "\\u%04X", c);
}
str_buf_cat(result, buf, strlen(buf));
}
else {
if (c == -1) {
char *q;
for (q = p-n; q < p; q++) {
sprintf(buf, "\\x%02X", *q & 0377);
str_buf_cat(result, buf, strlen(buf));
}
}
else if (unicode_p) {
if (c < 0x10000) {
sprintf(buf, "\\u%04X", c);
}
else {
sprintf(buf, "\\u{%X}", c);
}
str_buf_cat(result, buf, strlen(buf));
}
else {
if (c < 0x100) {
sprintf(buf, "\\x%02X", c);
}
else {
sprintf(buf, "\\x{%X}", c);
}
str_buf_cat(result, buf, strlen(buf));
}
}
}
str_buf_cat2(result, "\"");

View file

@ -201,10 +201,10 @@ class TestM17N < Test::Unit::TestCase
assert_equal('"\xFC\x80\x80\x80\x80 "', u("\xfc\x80\x80\x80\x80 ").inspect)
assert_equal("\"\\xA1\\x8F\\xA1\\xA1\"", e("\xa1\x8f\xa1\xa1").inspect)
assert_equal("\"\\xA1\\x{8FA1A1}\"", e("\xa1\x8f\xa1\xa1").inspect)
assert_equal('"\x81."', s("\x81.").inspect)
assert_equal(s('"\x81\x40"'), s("\x81@").inspect)
assert_equal(s('"\x{8140}"'), s("\x81@").inspect)
assert_equal('"\xFC"', u("\xfc").inspect)
end