summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-10-07 00:35:24 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-10-07 00:35:24 +0000
commitbe9766c37e614ca0a9286613f7b0a676f0c0adaf (patch)
treef7efd45303f80a3907ef26f6cd73cb2dc7cfb843 /string.c
parenta120569068621fce972506db53c3cce1cdac8939 (diff)
* string.c (rb_str_inspect): don't assign -1 to unsigned int.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25251 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c31
1 files changed, 12 insertions, 19 deletions
diff --git a/string.c b/string.c
index acb2a91ee8..caf4a5e522 100644
--- a/string.c
+++ b/string.c
@@ -4069,6 +4069,8 @@ rb_str_inspect(VALUE str)
{
rb_encoding *enc = STR_ENC_GET(str);
char *p, *pend;
+#define CHAR_ESC_LEN 12 /* sizeof(\x{ hex of 32bit unsigned int }) */
+ char buf[CHAR_ESC_LEN + 1];
VALUE result = rb_str_buf_new(0);
rb_encoding *resenc = rb_default_internal_encoding();
int unicode_p = rb_enc_unicode_p(enc);
@@ -4080,15 +4082,16 @@ rb_str_inspect(VALUE str)
p = RSTRING_PTR(str); pend = RSTRING_END(str);
while (p < pend) {
- unsigned int c = -1, cc;
+ unsigned int c, cc;
int n;
n = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(n)) {
+ snprintf(buf, CHAR_ESC_LEN, "\\x%02X", *p & 0377);
+ str_buf_cat(result, buf, strlen(buf));
p++;
- n = 1;
- goto escape_codepoint;
- }
+ continue;
+ }
n = MBCLEN_CHARFOUND_LEN(n);
c = rb_enc_mbc_to_codepoint(p, pend, enc);
p += n;
@@ -4130,31 +4133,21 @@ rb_str_inspect(VALUE str)
str_buf_cat(result, p-n, n);
}
else {
- char buf[11];
- escape_codepoint:
-
- 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 (unicode_p) {
if (c < 0x10000) {
- sprintf(buf, "\\u%04X", c);
+ snprintf(buf, CHAR_ESC_LEN, "\\u%04X", c);
}
else {
- sprintf(buf, "\\u{%X}", c);
+ snprintf(buf, CHAR_ESC_LEN, "\\u{%X}", c);
}
str_buf_cat(result, buf, strlen(buf));
}
else {
if (c < 0x100) {
- sprintf(buf, "\\x%02X", c);
+ snprintf(buf, CHAR_ESC_LEN, "\\x%02X", c);
}
else {
- sprintf(buf, "\\x{%X}", c);
+ snprintf(buf, CHAR_ESC_LEN, "\\x{%X}", c);
}
str_buf_cat(result, buf, strlen(buf));
}