summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-22 15:04:57 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-12-22 15:04:57 +0000
commit6f02547d83ec4651999b55f606672084cb0fe22b (patch)
tree33670755b21e35cdb937e083360465712d75b76a /string.c
parent1df33a4a5f4bb1b2393cf5597e7d1156983c7741 (diff)
internal.h: quote unprintable
* internal.h (QUOTE, QUOTE_ID): quote unprintable chars in strings and IDs. [Bug #7574] [ruby-dev:46749] * string.c (rb_str_quote_unprintable): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38558 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/string.c b/string.c
index 2033da2165..1e5dab37f1 100644
--- a/string.c
+++ b/string.c
@@ -4534,6 +4534,7 @@ rb_str_inspect(VALUE str)
}
}
switch (c) {
+ case '\0': cc = '0'; break;
case '\n': cc = 'n'; break;
case '\r': cc = 'r'; break;
case '\t': cc = 't'; break;
@@ -7784,6 +7785,31 @@ rb_str_symname_p(VALUE sym)
return TRUE;
}
+VALUE
+rb_str_quote_unprintable(VALUE str)
+{
+ rb_encoding *enc;
+ const char *ptr;
+ long len;
+ rb_encoding *resenc = rb_default_internal_encoding();
+
+ if (resenc == NULL) resenc = rb_default_external_encoding();
+ enc = STR_ENC_GET(str);
+ ptr = RSTRING_PTR(str);
+ len = RSTRING_LEN(str);
+ if ((resenc != enc && !rb_str_is_ascii_only_p(str)) ||
+ !sym_printable(ptr, ptr + len, enc)) {
+ return rb_str_inspect(str);
+ }
+ return str;
+}
+
+VALUE
+rb_id_quote_unprintable(ID id)
+{
+ return rb_str_quote_unprintable(rb_id2str(id));
+}
+
/*
* call-seq:
* sym.inspect -> string