summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-26 20:17:52 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-09-26 20:17:52 +0000
commit036c701bad168a86fd834c2b1a6ec102556963a0 (patch)
tree9966103e7f2ebf0a729468562094e40af3e6f37e
parent45dc99aea48338b59be903cf22376959886f8155 (diff)
downloadruby-036c701bad168a86fd834c2b1a6ec102556963a0.tar.gz
String#inspect's encoding should be fixed.
* string.c (rb_str_inspect): result's encoding should be fixed. If default_internal is not nil, the encoding is default_internal. Else if default_external is not nil, the encoding is default_external. But the encoding is not ASCII-compatible, the encoding is replaced by US-ASCII. Characters in ASCII-incompatible encoding or non ASCII characters in other than the encoding will be \xXX escaped. * string.c (str_buf_cat2): defined. * string.c (prefix_escape): removed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25113 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog14
-rw-r--r--string.c62
2 files changed, 45 insertions, 31 deletions
diff --git a/ChangeLog b/ChangeLog
index 429492c0f4..2b4b24850c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+Sun Sep 27 04:20:55 2009 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * string.c (rb_str_inspect): result's encoding should be fixed.
+ If default_internal is not nil, the encoding is default_internal.
+ Else if default_external is not nil, the encoding is default_external.
+ But the encoding is not ASCII-compatible, the encoding is replaced by
+ US-ASCII.
+ Characters in ASCII-incompatible encoding or non ASCII characters
+ in other than the encoding will be \xXX escaped.
+
+ * string.c (str_buf_cat2): defined.
+
+ * string.c (prefix_escape): removed.
+
Sun Sep 27 05:37:45 2009 Alexander Zavorine <alexandre.zavorine@nokia.com>
* symbian/missing-pips.c: Updated to work with the latest PIPS 1.6.
diff --git a/string.c b/string.c
index d1ee6b5578..fede22646a 100644
--- a/string.c
+++ b/string.c
@@ -1741,6 +1741,12 @@ str_buf_cat(VALUE str, const char *ptr, long len)
return str;
}
+static VALUE
+str_buf_cat2(VALUE str, const char *ptr)
+{
+ return str_buf_cat(str, ptr, strlen(ptr));
+}
+
VALUE
rb_str_buf_cat(VALUE str, const char *ptr, long len)
{
@@ -4034,13 +4040,6 @@ str_cat_char(VALUE str, unsigned int c, rb_encoding *enc)
rb_enc_str_buf_cat(str, s, n, enc);
}
-static void
-prefix_escape(VALUE str, unsigned int c, rb_encoding *enc)
-{
- str_cat_char(str, '\\', enc);
- str_cat_char(str, c, enc);
-}
-
/*
* call-seq:
* str.inspect => string
@@ -4059,10 +4058,13 @@ rb_str_inspect(VALUE str)
rb_encoding *enc = STR_ENC_GET(str);
char *p, *pend;
VALUE result = rb_str_buf_new(0);
+ rb_encoding *resenc = rb_default_internal_encoding();
+
+ if (resenc == NULL) resenc = rb_default_external_encoding();
+ if (!rb_enc_asciicompat(resenc)) resenc = rb_usascii_encoding();
+ rb_enc_associate(result, resenc);
+ str_buf_cat2(result, "\"");
- if (!rb_enc_asciicompat(enc)) enc = rb_usascii_encoding();
- rb_enc_associate(result, enc);
- str_cat_char(result, '"', enc);
p = RSTRING_PTR(str); pend = RSTRING_END(str);
while (p < pend) {
unsigned int c, cc;
@@ -4083,51 +4085,49 @@ rb_str_inspect(VALUE str)
MBCLEN_CHARFOUND_P(rb_enc_precise_mbclen(p,pend,enc)) &&
(cc = rb_enc_codepoint(p,pend,enc),
(cc == '$' || cc == '@' || cc == '{')))) {
- prefix_escape(result, c, enc);
+ str_buf_cat2(result, "\\");
+ str_buf_cat(result, p - n, n);
}
else if (c == '\n') {
- prefix_escape(result, 'n', enc);
+ str_buf_cat2(result, "\\n");
}
else if (c == '\r') {
- prefix_escape(result, 'r', enc);
+ str_buf_cat2(result, "\\r");
}
else if (c == '\t') {
- prefix_escape(result, 't', enc);
+ str_buf_cat2(result, "\\t");
}
else if (c == '\f') {
- prefix_escape(result, 'f', enc);
+ str_buf_cat2(result, "\\f");
}
else if (c == '\013') {
- prefix_escape(result, 'v', enc);
+ str_buf_cat2(result, "\\v");
}
else if (c == '\010') {
- prefix_escape(result, 'b', enc);
+ str_buf_cat2(result, "\\b");
}
else if (c == '\007') {
- prefix_escape(result, 'a', enc);
+ str_buf_cat2(result, "\\a");
}
else if (c == 033) {
- prefix_escape(result, 'e', enc);
+ str_buf_cat2(result, "\\e");
}
- else if (rb_enc_isprint(c, enc)) {
- rb_enc_str_buf_cat(result, p-n, n, enc);
+ else if ((enc == resenc && rb_enc_isprint(c, enc)) || rb_enc_isascii(c, enc)) {
+ str_buf_cat(result, p-n, n);
}
else {
- char buf[5];
- char *s;
char *q;
-
escape_codepoint:
for (q = p-n; q < p; q++) {
- s = buf;
- sprintf(buf, "\\x%02X", *q & 0377);
- while (*s) {
- str_cat_char(result, *s++, enc);
- }
- }
+#define BACKESC_BUFSIZE 5
+ char buf[BACKESC_BUFSIZE];
+ sprintf(buf, "\\x%02X", *q & 0377);
+ str_buf_cat(result, buf, BACKESC_BUFSIZE - 1);
+#undef BACKESC_BUFSIZE
+ }
}
}
- str_cat_char(result, '"', enc);
+ str_buf_cat2(result, "\"");
OBJ_INFECT(result, str);
return result;