/*
* call-seq:
* str.inspect => string
*
* Returns a printable version of _str_, with special characters
* escaped.
*
* str = "hello"
* str[3] = 8
* str.inspect #=> "hel\010o"
*/
VALUE
rb_str_inspect(str)
VALUE str;
{
char *p, *pend;
VALUE result = rb_str_buf_new2("\"");
char s[5];
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
while (p < pend) {
char c = *p++;
if (ismbchar(c) && p < pend) {
int len = mbclen(c);
rb_str_buf_cat(result, p - 1, len);
p += len - 1;
}
else if (c == '"'|| c == '\\' || (c == '#' && IS_EVSTR(p, pend))) {
s[0] = '\\'; s[1] = c;
rb_str_buf_cat(result, s, 2);
}
else if (ISPRINT(c)) {
s[0] = c;
rb_str_buf_cat(result, s, 1);
}
else if (c == '\n') {
s[0] = '\\'; s[1] = 'n';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\r') {
s[0] = '\\'; s[1] = 'r';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\t') {
s[0] = '\\'; s[1] = 't';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\f') {
s[0] = '\\'; s[1] = 'f';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\013') {
s[0] = '\\'; s[1] = 'v';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\010') {
s[0] = '\\'; s[1] = 'b';
rb_str_buf_cat(result, s, 2);
}
else if (c == '\007') {
s[0] = '\\'; s[1] = 'a';
rb_str_buf_cat(result, s, 2);
}
else if (c == 033) {
s[0] = '\\'; s[1] = 'e';
rb_str_buf_cat(result, s, 2);
}
else {
sprintf(s, "\\%03o", c & 0377);
rb_str_buf_cat2(result, s);
}
}
rb_str_buf_cat2(result, "\"");
OBJ_INFECT(result, str);
return result;
}