method
dump
v1_8_6_287 -
Show latest stable
- Class:
String
dump()public
Produces a version of str with all nonprinting characters replaced by \nnn notation and all special characters escaped.
/*
* call-seq:
* str.dump => new_str
*
* Produces a version of <i>str</i> with all nonprinting characters replaced by
* <code>\nnn</code> notation and all special characters escaped.
*/
VALUE
rb_str_dump(str)
VALUE str;
{
long len;
char *p, *pend;
char *q, *qend;
VALUE result;
len = 2; /* "" */
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
while (p < pend) {
char c = *p++;
switch (c) {
case '"': case '\\':
case '\n': case '\r':
case '\t': case '\f':
case '\013': case '\010': case '\007': case '\033':
len += 2;
break;
case '#':
len += IS_EVSTR(p, pend) ? 2 : 1;
break;
default:
if (ISPRINT(c)) {
len++;
}
else {
len += 4; /* \nnn */
}
break;
}
}
result = rb_str_new5(str, 0, len);
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
q = RSTRING(result)->ptr; qend = q + len;
*q++ = '"';
while (p < pend) {
char c = *p++;
if (c == '"' || c == '\\') {
*q++ = '\\';
*q++ = c;
}
else if (c == '#') {
if (IS_EVSTR(p, pend)) *q++ = '\\';
*q++ = '#';
}
else if (ISPRINT(c)) {
*q++ = c;
}
else if (c == '\n') {
*q++ = '\\';
*q++ = 'n';
}
else if (c == '\r') {
*q++ = '\\';
*q++ = 'r';
}
else if (c == '\t') {
*q++ = '\\';
*q++ = 't';
}
else if (c == '\f') {
*q++ = '\\';
*q++ = 'f';
}
else if (c == '\013') {
*q++ = '\\';
*q++ = 'v';
}
else if (c == '\010') {
*q++ = '\\';
*q++ = 'b';
}
else if (c == '\007') {
*q++ = '\\';
*q++ = 'a';
}
else if (c == '\033') {
*q++ = '\\';
*q++ = 'e';
}
else {
*q++ = '\\';
sprintf(q, "%03o", c&0xff);
q += 3;
}
}
*q++ = '"';
OBJ_INFECT(result, str);
return result;
} Related methods
- Instance methods
- %
- *
- +
- <<
- <=>
- ==
- =~
- []
- []=
- block_scanf
- capitalize
- capitalize!
- casecmp
- center
- chomp
- chomp!
- chop
- chop
- chop!
- chop!
- concat
- count
- crypt
- delete
- delete
- delete!
- delete!
- downcase
- downcase!
- dump
- each
- each_byte
- each_char
- each_line
- empty?
- end_regexp
- eql?
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data?
- is_complex_yaml?
- iseuc
- issjis
- isutf8
- jcount
- jlength
- jsize
- kconv
- length
- ljust
- lstrip
- lstrip!
- match
- mbchar?
- next
- next!
- oct
- quote
- replace
- reverse
- reverse!
- rindex
- rjust
- rstrip
- rstrip!
- scan
- scanf
- size
- slice
- slice!
- split
- squeeze
- squeeze
- squeeze!
- squeeze!
- strip
- strip!
- sub
- sub!
- succ
- succ
- succ!
- succ!
- sum
- swapcase
- swapcase!
- to_f
- to_i
- to_s
- to_str
- to_sym
- to_yaml
- toeuc
- tojis
- tosjis
- toutf16
- toutf8
- tr
- tr
- tr!
- tr!
- tr_s
- tr_s
- tr_s!
- tr_s!
- unpack
- upcase
- upcase!
- upto
- Class methods
- new
- yaml_new
- Private methods
-
_expand_ch -
_regex_quote -
expand_ch_hash -
original_succ -
original_succ!