Skip to content

Commit

Permalink
String#dump
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.ruby-lang.org/repos/ruby/branches/v1_1r@137 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Mar 26, 1998
1 parent 75a98e2 commit 117231d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Thu Mar 26 11:51:09 1998 Yukihiro Matsumoto <matz@netlab.co.jp>

* string.c (str_dump): new method.

* eval.c (block_pass): block argument can be nil, which means no
block is supplied for the method.

Wed Mar 25 08:12:07 1998 Yukihiro Matsumoto <matz@netlab.co.jp>

* numeric.c (flo_modulo): caused SEGV if left operand is not a
Expand Down
3 changes: 3 additions & 0 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4708,6 +4708,9 @@ block_pass(self, node)
volatile int orphan;
volatile int safe = safe_level;

if (NIL_P(block)) {
return rb_eval(self, node->nd_iter);
}
if (obj_is_kind_of(block, cMethod)) {
block = method_proc(block);
}
Expand Down
2 changes: 1 addition & 1 deletion ext/curses/curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* ext/curses/curses.c
*
* by MAEDA Shugo (ender@pic-internet.or.jp)
* modified by Yukihiro Matsumoto (matz@ruby.club.or.jp)
* modified by Yukihiro Matsumoto (matz@netlab.co.jp)
*/

#ifdef HAVE_NCURSES_H
Expand Down
2 changes: 1 addition & 1 deletion lib/delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def initialize(obj)
end
for method in obj.methods
next if preserved.include? method
eval "def self.#{method}(*args); __getobj__.__send__ :#{method}, *args; end"
eval "def self.#{method}(*args); __getobj__.__send__(:#{method}, *args){|x|yield}; end"
end
end

Expand Down
89 changes: 88 additions & 1 deletion string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ str_inspect(str)
*b++ = '\\';
*b++ = 'f';
}
else if (c == '\13') {
else if (c == '\013') {
CHECK(2);
*b++ = '\\';
*b++ = 'v';
Expand All @@ -1449,6 +1449,92 @@ str_inspect(str)
return str_new(buf, b - buf);
}

VALUE
str_dump(str)
VALUE str;
{
int len;
UCHAR *p, *pend;
UCHAR *q, *qend;
VALUE result;

len = 2; /* "" */
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
while (p < pend) {
UCHAR c = *p++;
switch (c) {
case '"': case '\'':
case '\n': case '\r':
case '\t': case '\f':
case '\013': case '\007': case '\033':
len += 2;
break;

default:
if (isascii(c) && isprint(c)) {
len++;
}
else {
len += 4; /* \nnn */
}
break;
}
}

result = str_new(0, len);
p = RSTRING(str)->ptr; pend = p + RSTRING(str)->len;
q = RSTRING(result)->ptr; qend = q + len;

*q++ = '"';
while (p < pend) {
UCHAR c = *p++;

if (c == '"' || c == '\\') {
*q++ = '\\';
*q++ = c;
}
else if (isascii(c) && 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 == '\13') {
*q++ = '\\';
*q++ = 'v';
}
else if (c == '\007') {
*q++ = '\\';
*q++ = 'a';
}
else if (c == 033) {
*q++ = '\\';
*q++ = 'e';
}
else {
*q++ = '\\';
sprintf(q, "%03o", c);
q += 3;
}
}
*q++ = '"';

return result;
}

static VALUE
str_upcase_bang(str)
VALUE str;
Expand Down Expand Up @@ -2544,6 +2630,7 @@ Init_String()
rb_define_method(cString, "to_f", str_to_f, 0);
rb_define_method(cString, "to_s", str_to_s, 0);
rb_define_method(cString, "inspect", str_inspect, 0);
rb_define_method(cString, "dump", str_dump, 0);

rb_define_method(cString, "upcase", str_upcase, 0);
rb_define_method(cString, "downcase", str_downcase, 0);
Expand Down

0 comments on commit 117231d

Please sign in to comment.