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

* string.c (rb_str_inspect): copy by chunks.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25259 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-10-07 17:46:34 +00:00
parent 0f408e10ba
commit ec0e370eb5
2 changed files with 35 additions and 28 deletions

View file

@ -1,3 +1,7 @@
Thu Oct 8 02:46:24 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_inspect): copy by chunks.
Thu Oct 8 01:23:18 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Thu Oct 8 01:23:18 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* iseq.c (iseq_s_disasm): accept proc objects. [ruby-core:18762] * iseq.c (iseq_s_disasm): accept proc objects. [ruby-core:18762]

View file

@ -4068,7 +4068,7 @@ VALUE
rb_str_inspect(VALUE str) rb_str_inspect(VALUE str)
{ {
rb_encoding *enc = STR_ENC_GET(str); rb_encoding *enc = STR_ENC_GET(str);
char *p, *pend; const char *p, *pend, *prev;
#define CHAR_ESC_LEN 12 /* sizeof(\x{ hex of 32bit unsigned int }) */ #define CHAR_ESC_LEN 12 /* sizeof(\x{ hex of 32bit unsigned int }) */
char buf[CHAR_ESC_LEN + 1]; char buf[CHAR_ESC_LEN + 1];
VALUE result = rb_str_buf_new(0); VALUE result = rb_str_buf_new(0);
@ -4081,15 +4081,17 @@ rb_str_inspect(VALUE str)
str_buf_cat2(result, "\""); str_buf_cat2(result, "\"");
p = RSTRING_PTR(str); pend = RSTRING_END(str); p = RSTRING_PTR(str); pend = RSTRING_END(str);
prev = p;
while (p < pend) { while (p < pend) {
unsigned int c, cc; unsigned int c, cc;
int n; int n;
n = rb_enc_precise_mbclen(p, pend, enc); n = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(n)) { if (!MBCLEN_CHARFOUND_P(n)) {
if (p > prev) str_buf_cat(result, prev, p - prev);
snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377); snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
str_buf_cat(result, buf, strlen(buf)); str_buf_cat(result, buf, strlen(buf));
p++; prev = ++p;
continue; continue;
} }
n = MBCLEN_CHARFOUND_LEN(n); n = MBCLEN_CHARFOUND_LEN(n);
@ -4101,38 +4103,36 @@ rb_str_inspect(VALUE str)
MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) && MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
(cc = rb_enc_codepoint(p,pend,enc), (cc = rb_enc_codepoint(p,pend,enc),
(cc == '$' || cc == '@' || cc == '{')))) { (cc == '$' || cc == '@' || cc == '{')))) {
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
str_buf_cat2(result, "\\"); str_buf_cat2(result, "\\");
str_buf_cat(result, p - n, n); prev = p - n;
continue;
} }
else if (c == '\n') { switch (c) {
str_buf_cat2(result, "\\n"); case '\n': cc = 'n'; break;
case '\r': cc = 'r'; break;
case '\t': cc = 't'; break;
case '\f': cc = 'f'; break;
case '\013': cc = 'v'; break;
case '\010': cc = 'b'; break;
case '\007': cc = 'a'; break;
case 033: cc = 'e'; break;
default: cc = 0; break;
} }
else if (c == '\r') { if (cc) {
str_buf_cat2(result, "\\r"); if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
buf[0] = '\\';
buf[1] = (char)cc;
str_buf_cat(result, buf, 2);
prev = p;
continue;
} }
else if (c == '\t') { if ((enc == resenc && rb_enc_isprint(c, enc)) ||
str_buf_cat2(result, "\\t");
}
else if (c == '\f') {
str_buf_cat2(result, "\\f");
}
else if (c == '\013') {
str_buf_cat2(result, "\\v");
}
else if (c == '\010') {
str_buf_cat2(result, "\\b");
}
else if (c == '\007') {
str_buf_cat2(result, "\\a");
}
else if (c == 033) {
str_buf_cat2(result, "\\e");
}
else if ((enc == resenc && rb_enc_isprint(c, enc)) ||
(rb_enc_isascii(c, enc) && ISPRINT(c))) { (rb_enc_isascii(c, enc) && ISPRINT(c))) {
str_buf_cat(result, p-n, n); continue;
} }
else { else {
if (p - n > prev) str_buf_cat(result, prev, p - n - prev);
if (unicode_p) { if (unicode_p) {
if (c < 0x10000) { if (c < 0x10000) {
snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c); snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
@ -4151,8 +4151,11 @@ rb_str_inspect(VALUE str)
} }
str_buf_cat(result, buf, strlen(buf)); str_buf_cat(result, buf, strlen(buf));
} }
prev = p;
continue;
} }
} }
if (p > prev) str_buf_cat(result, prev, p - prev);
str_buf_cat2(result, "\""); str_buf_cat2(result, "\"");
OBJ_INFECT(result, str); OBJ_INFECT(result, str);