summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/string.c b/string.c
index c34abb4375..8b16d5eb12 100644
--- a/string.c
+++ b/string.c
@@ -6826,6 +6826,19 @@ sym_equal(VALUE sym1, VALUE sym2)
}
+static int
+sym_printable(const char *s, rb_encoding *enc)
+{
+ const char *send = s + strlen(s);
+ while (s) {
+ int c = rb_enc_codepoint(s, send, enc);
+ int n = rb_enc_codelen(c, enc);
+ if (!rb_enc_isprint(c, enc)) return Qfalse;
+ s += n;
+ }
+ return Qtrue;
+}
+
/*
* call-seq:
* sym.inspect => string
@@ -6848,7 +6861,8 @@ sym_inspect(VALUE sym)
RSTRING_PTR(str)[0] = ':';
memcpy(RSTRING_PTR(str)+1, RSTRING_PTR(sym), RSTRING_LEN(sym));
if (RSTRING_LEN(sym) != strlen(RSTRING_PTR(sym)) ||
- !rb_enc_symname_p(RSTRING_PTR(sym), enc)) {
+ !rb_enc_symname_p(RSTRING_PTR(sym), enc) ||
+ !sym_printable(RSTRING_PTR(sym), enc)) {
str = rb_str_inspect(str);
strncpy(RSTRING_PTR(str), ":\"", 2);
}