1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* array.c (rb_values_at): extract common procedure from

rb_ary_values_at.  follow DRY principle.

* re.c (match_values_at): values_at should understand ranges.

* struct.c (rb_struct_values_at): ditto.

* struct.c (inspect_struct): inspect format changed; add "struct "
  at the top.

* sprintf.c (rb_f_sprintf): "%p" specifier for inspect output.
  (RCR#68)

* eval.c (rb_mod_undef_method): allow "undef_method" to accept
  multiple arguments. (RCR#146)

* lib/timeout.rb: put timeout in Timeout module. (RCR#121)
  [ruby-talk:61028]

* re.c (match_groups): new method added. (RCR#139)

* variable.c (rb_mod_const_of): should exclude constant defined
  in Object, unless retrieving constants of Object.

* string.c (rb_str_new4): do not allocate new string if original
  is frozen or already have copy-on-write entry. [ruby-talk:74940]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4031 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2003-07-03 11:02:53 +00:00
parent a11ab83884
commit 9d22a06ea0
13 changed files with 157 additions and 96 deletions

View file

@ -1,10 +1,43 @@
Thu Jul 3 14:22:46 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* array.c (rb_values_at): extract common procedure from
rb_ary_values_at. follow DRY principle.
* re.c (match_values_at): values_at should understand ranges.
* struct.c (rb_struct_values_at): ditto.
* struct.c (inspect_struct): inspect format changed; add "struct "
at the top.
* sprintf.c (rb_f_sprintf): "%p" specifier for inspect output.
(RCR#68)
* eval.c (rb_mod_undef_method): allow "undef_method" to accept
multiple arguments. (RCR#146)
* lib/timeout.rb: put timeout in Timeout module. (RCR#121)
[ruby-talk:61028]
* re.c (match_groups): new method added. (RCR#139)
* variable.c (rb_mod_const_of): should exclude constant defined
in Object, unless retrieving constants of Object.
Thu Jul 3 12:13:05 2003 WATANABE Hirofumi <eban@ruby-lang.org> Thu Jul 3 12:13:05 2003 WATANABE Hirofumi <eban@ruby-lang.org>
* lib/mkmf.rb (VPATH): convert from Windows form to Unix form on * lib/mkmf.rb (VPATH): convert from Windows form to Unix form on
MinGW. This fixes the build with GNU make 3.80-1 for Cygwin. MinGW. This fixes the build with GNU make 3.80-1 for Cygwin.
Wed Jul 2 23:27:34 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_new4): do not allocate new string if original
is frozen or already have copy-on-write entry. [ruby-talk:74940]
Wed Jul 2 13:22:39 2003 Yukihiro Matsumoto <matz@ruby-lang.org> Wed Jul 2 13:22:39 2003 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_new4):
* string.c (rb_str_shared_replace): clear flags before copy. * string.c (rb_str_shared_replace): clear flags before copy.
* string.c (rb_str_replace): ditto. * string.c (rb_str_replace): ditto.

42
array.c
View file

@ -1218,29 +1218,37 @@ rb_ary_collect_bang(ary)
return ary; return ary;
} }
static void VALUE
push_values_at(result, ary, arg) rb_values_at(obj, olen, argc, argv, func)
VALUE result, ary, arg; VALUE obj;
long olen;
int argc;
VALUE *argv;
VALUE (*func) _((VALUE,long));
{ {
long beg, len, i; VALUE result = rb_ary_new2(argc);
long beg, len, i, j;
if (FIXNUM_P(arg)) { for (i=0; i<argc; i++) {
rb_ary_push(result, rb_ary_entry(ary, FIX2LONG(arg))); if (FIXNUM_P(argv[i])) {
return; rb_ary_push(result, (*func)(obj, FIX2LONG(argv[i])));
continue;
} }
/* check if idx is Range */ /* check if idx is Range */
switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) { switch (rb_range_beg_len(argv[i], &beg, &len, olen, 0)) {
case Qfalse: case Qfalse:
break; break;
case Qnil: case Qnil:
return; continue;
default: default:
for (i=0; i<len; i++) { for (j=0; j<len; j++) {
rb_ary_push(result, rb_ary_entry(ary, i+beg)); rb_ary_push(result, (*func)(obj, j+beg));
} }
return; continue;
} }
rb_ary_push(result, rb_ary_entry(ary, NUM2LONG(arg))); rb_ary_push(result, (*func)(obj, NUM2LONG(argv[i])));
}
return result;
} }
static VALUE static VALUE
@ -1249,13 +1257,7 @@ rb_ary_values_at(argc, argv, ary)
VALUE *argv; VALUE *argv;
VALUE ary; VALUE ary;
{ {
VALUE result = rb_ary_new2(argc); return rb_values_at(ary, RARRAY(ary)->len, argc, argv, rb_ary_entry);
long i;
for (i=0; i<argc; i++) {
push_values_at(result, ary, argv[i]);
}
return result;
} }
static VALUE static VALUE

15
eval.c
View file

@ -362,6 +362,7 @@ rb_get_method_body(klassp, idp, noexp)
body = ent->method = body->nd_head; body = ent->method = body->nd_head;
} }
else { else {
if (BUILTIN_TYPE(origin) == T_ICLASS) origin = RBASIC(origin)->klass;
*klassp = origin; *klassp = origin;
ent->origin = origin; ent->origin = origin;
ent->mid = ent->mid0 = id; ent->mid = ent->mid0 = id;
@ -1824,10 +1825,16 @@ rb_undef(klass, id)
} }
static VALUE static VALUE
rb_mod_undef_method(mod, name) rb_mod_undef_method(argc, argv, mod)
VALUE mod, name; int argc;
VALUE *argv;
VALUE mod;
{ {
rb_undef(mod, rb_to_id(name)); int i;
for (i=0; i<argc; i++) {
rb_undef(mod, rb_to_id(argv[i]));
}
return mod; return mod;
} }
@ -6521,7 +6528,7 @@ Init_eval()
rb_undef_method(rb_cClass, "module_function"); rb_undef_method(rb_cClass, "module_function");
rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, 1); rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, 1);
rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, 1); rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);
rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2); rb_define_private_method(rb_cModule, "alias_method", rb_mod_alias_method, 2);
rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1); rb_define_private_method(rb_cModule, "define_method", rb_mod_define_method, -1);

View file

@ -56,6 +56,7 @@ VALUE rb_ary_cmp _((VALUE, VALUE));
VALUE rb_protect_inspect _((VALUE(*)(ANYARGS),VALUE,VALUE)); VALUE rb_protect_inspect _((VALUE(*)(ANYARGS),VALUE,VALUE));
VALUE rb_inspecting_p _((VALUE)); VALUE rb_inspecting_p _((VALUE));
VALUE rb_check_array_value _((VALUE)); VALUE rb_check_array_value _((VALUE));
VALUE rb_values_at _((VALUE, long, int, VALUE*, VALUE(*) _((VALUE,long))));
/* bignum.c */ /* bignum.c */
VALUE rb_big_clone _((VALUE)); VALUE rb_big_clone _((VALUE));
void rb_big_2comp _((VALUE)); void rb_big_2comp _((VALUE));

View file

@ -700,9 +700,11 @@ EOHELP
@line = line @line = line
case event case event
when 'line' when 'line'
p [line, @stop_next]
frame_set_pos(file, line) frame_set_pos(file, line)
if !@no_step or @frames.size == @no_step if !@no_step or @frames.size == @no_step
@stop_next -= 1 @stop_next -= 1
@stop_next = 0 if @stop_next < 0
elsif @frames.size < @no_step elsif @frames.size < @no_step
@stop_next = 0 # break here before leaving... @stop_next = 0 # break here before leaving...
else else

View file

@ -177,6 +177,7 @@ module IRB
@inspect_mode @inspect_mode
end end
undef use_readline=
def use_readline=(opt) def use_readline=(opt)
@use_readline = opt @use_readline = opt
print "use readline module\n" if @use_readline print "use readline module\n" if @use_readline

View file

@ -8,6 +8,7 @@ CONFIG = Config::MAKEFILE_CONFIG
ORIG_LIBPATH = ENV['LIB'] ORIG_LIBPATH = ENV['LIB']
SRC_EXT = ["c", "cc", "m", "cxx", "cpp", "C"] SRC_EXT = ["c", "cc", "m", "cxx", "cpp", "C"]
$static = $config_h = nil
unless defined? $configure_args unless defined? $configure_args
$configure_args = {} $configure_args = {}

View file

@ -23,12 +23,17 @@
# #
# The time in seconds to wait for block teminatation. # The time in seconds to wait for block teminatation.
# #
# : [exception]
#
# The exception classs to be raised on timeout.
#
#=end #=end
class TimeoutError<Interrupt module Timeout
end class Error<Interrupt
end
def timeout(sec, exception=TimeoutError) def timeout(sec, exception=Error)
return yield if sec == nil return yield if sec == nil
begin begin
x = Thread.current x = Thread.current
@ -37,12 +42,20 @@ def timeout(sec, exception=TimeoutError)
x.raise exception, "execution expired" if x.alive? x.raise exception, "execution expired" if x.alive?
} }
yield sec yield sec
# return true # return true
ensure ensure
y.kill if y and y.alive? y.kill if y and y.alive?
end end
end
module_function :timeout
end end
# compatible
def timeout(n, &block)
Timeout::timeout(n, &block)
end
TimeoutError = Timeout::Error
if __FILE__ == $0 if __FILE__ == $0
p timeout(5) { p timeout(5) {
45 45

49
re.c
View file

@ -914,8 +914,9 @@ last_paren_match_getter()
} }
static VALUE static VALUE
match_to_a(match) match_array(match, start)
VALUE match; VALUE match;
int start;
{ {
struct re_registers *regs = RMATCH(match)->regs; struct re_registers *regs = RMATCH(match)->regs;
VALUE ary = rb_ary_new2(regs->num_regs); VALUE ary = rb_ary_new2(regs->num_regs);
@ -923,7 +924,7 @@ match_to_a(match)
int i; int i;
int taint = OBJ_TAINTED(match); int taint = OBJ_TAINTED(match);
for (i=0; i<regs->num_regs; i++) { for (i=start; i<regs->num_regs; i++) {
if (regs->beg[i] == -1) { if (regs->beg[i] == -1) {
rb_ary_push(ary, Qnil); rb_ary_push(ary, Qnil);
} }
@ -936,6 +937,20 @@ match_to_a(match)
return ary; return ary;
} }
static VALUE
match_to_a(match)
VALUE match;
{
return match_array(match, 0);
}
static VALUE
match_groups(match)
VALUE match;
{
return match_array(match, 1);
}
static VALUE static VALUE
match_aref(argc, argv, match) match_aref(argc, argv, match)
int argc; int argc;
@ -952,32 +967,21 @@ match_aref(argc, argv, match)
return rb_reg_nth_match(FIX2INT(idx), match); return rb_reg_nth_match(FIX2INT(idx), match);
} }
static VALUE
match_entry(match, n)
VALUE match;
long n;
{
return rb_reg_nth_match(n, match);
}
static VALUE static VALUE
match_values_at(argc, argv, match) match_values_at(argc, argv, match)
int argc; int argc;
VALUE *argv; VALUE *argv;
VALUE match; VALUE match;
{ {
struct re_registers *regs = RMATCH(match)->regs; return rb_values_at(match, RMATCH(match)->regs->num_regs, argc, argv, match_entry);
VALUE target = RMATCH(match)->str;
VALUE result = rb_ary_new();
int i;
long idx;
int taint = OBJ_TAINTED(match);
for (i=0; i<argc; i++) {
idx = NUM2LONG(argv[i]);
if (idx < 0) idx += regs->num_regs;
if (idx < 0 || regs->num_regs <= idx) {
rb_ary_push(result, Qnil);
}
else {
VALUE str = rb_str_substr(target, regs->beg[idx], regs->end[idx]-regs->beg[idx]);
if (taint) OBJ_TAINT(str);
rb_ary_push(result, str);
}
}
return result;
} }
static VALUE static VALUE
@ -1766,6 +1770,7 @@ Init_Regexp()
rb_define_method(rb_cMatch, "end", match_end, 1); rb_define_method(rb_cMatch, "end", match_end, 1);
rb_define_method(rb_cMatch, "to_a", match_to_a, 0); rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
rb_define_method(rb_cMatch, "[]", match_aref, -1); rb_define_method(rb_cMatch, "[]", match_aref, -1);
rb_define_method(rb_cMatch, "groups", match_groups, 0);
rb_define_method(rb_cMatch, "select", match_select, -1); rb_define_method(rb_cMatch, "select", match_select, -1);
rb_define_method(rb_cMatch, "values_at", match_values_at, -1); rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0); rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);

View file

@ -291,10 +291,12 @@ rb_f_sprintf(argc, argv)
break; break;
case 's': case 's':
case 'p':
{ {
VALUE arg = GETARG(); VALUE arg = GETARG();
long len; long len;
if (*p == 'p') arg = rb_inspect(arg);
str = rb_obj_as_string(arg); str = rb_obj_as_string(arg);
if (OBJ_TAINTED(str)) tainted = 1; if (OBJ_TAINTED(str)) tainted = 1;
len = RSTRING(str)->len; len = RSTRING(str)->len;

View file

@ -157,15 +157,18 @@ rb_str_new4(orig)
{ {
VALUE klass, str; VALUE klass, str;
if (OBJ_FROZEN(orig)) return orig;
klass = rb_obj_class(orig); klass = rb_obj_class(orig);
if (FL_TEST(orig, ELTS_SHARED) && RSTRING(orig)->aux.shared) { if (FL_TEST(orig, ELTS_SHARED) && RSTRING(orig)->aux.shared) {
long ofs; long ofs;
str = RSTRING(orig)->aux.shared; str = RSTRING(orig)->aux.shared;
ofs = RSTRING(str)->len - RSTRING(orig)->len; ofs = RSTRING(str)->len - RSTRING(orig)->len;
if (ofs > 0) {
str = str_new3(klass, str); str = str_new3(klass, str);
RSTRING(str)->ptr += ofs; RSTRING(str)->ptr += ofs;
RSTRING(str)->len -= ofs; RSTRING(str)->len -= ofs;
} }
}
else if (FL_TEST(orig, STR_ASSOC)) { else if (FL_TEST(orig, STR_ASSOC)) {
str = str_new(klass, RSTRING(orig)->ptr, RSTRING(orig)->len); str = str_new(klass, RSTRING(orig)->ptr, RSTRING(orig)->len);
OBJ_INFECT(str, orig); OBJ_INFECT(str, orig);

View file

@ -351,18 +351,6 @@ rb_struct_each_pair(s)
return s; return s;
} }
static VALUE
rb_struct_to_s(s)
VALUE s;
{
char *cname = rb_class2name(rb_obj_class(s));
VALUE str = rb_str_new(0, strlen(cname) + 4);
sprintf(RSTRING(str)->ptr, "#<%s>", cname);
RSTRING(str)->len = strlen(RSTRING(str)->ptr);
return str;
}
static VALUE static VALUE
inspect_struct(s) inspect_struct(s)
VALUE s; VALUE s;
@ -376,7 +364,7 @@ inspect_struct(s)
rb_bug("non-initialized struct"); rb_bug("non-initialized struct");
} }
str = rb_str_buf_new2("#<"); str = rb_str_buf_new2("#<struct ");
rb_str_cat2(str, cname); rb_str_cat2(str, cname);
rb_str_cat2(str, " "); rb_str_cat2(str, " ");
for (i=0; i<RSTRUCT(s)->len; i++) { for (i=0; i<RSTRUCT(s)->len; i++) {
@ -405,9 +393,9 @@ rb_struct_inspect(s)
{ {
if (rb_inspecting_p(s)) { if (rb_inspecting_p(s)) {
char *cname = rb_class2name(rb_obj_class(s)); char *cname = rb_class2name(rb_obj_class(s));
VALUE str = rb_str_new(0, strlen(cname) + 8); VALUE str = rb_str_new(0, strlen(cname) + 15);
sprintf(RSTRING(str)->ptr, "#<%s:...>", cname); sprintf(RSTRING(str)->ptr, "#<struct %s:...>", cname);
RSTRING(str)->len = strlen(RSTRING(str)->ptr); RSTRING(str)->len = strlen(RSTRING(str)->ptr);
return str; return str;
} }
@ -529,20 +517,21 @@ rb_struct_aset(s, idx, val)
return RSTRUCT(s)->ptr[i] = val; return RSTRUCT(s)->ptr[i] = val;
} }
static VALUE
struct_entry(s, n)
VALUE s;
long n;
{
return rb_struct_aref(s, LONG2NUM(n));
}
static VALUE static VALUE
rb_struct_values_at(argc, argv, s) rb_struct_values_at(argc, argv, s)
int argc; int argc;
VALUE *argv; VALUE *argv;
VALUE s; VALUE s;
{ {
VALUE result = rb_ary_new(); return rb_values_at(s, RSTRUCT(s)->len, argc, argv, struct_entry);
long i;
for (i=0; i<argc; i++) {
rb_ary_push(result, rb_struct_aref(s, argv[i]));
}
return result;
} }
static VALUE static VALUE
@ -648,7 +637,7 @@ Init_Struct()
rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1); rb_define_method(rb_cStruct, "eql?", rb_struct_eql, 1);
rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0); rb_define_method(rb_cStruct, "hash", rb_struct_hash, 0);
rb_define_method(rb_cStruct, "to_s", rb_struct_to_s, 0); rb_define_method(rb_cStruct, "to_s", rb_struct_inspect, 0);
rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0); rb_define_method(rb_cStruct, "inspect", rb_struct_inspect, 0);
rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0); rb_define_method(rb_cStruct, "to_a", rb_struct_to_a, 0);
rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0); rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0);

View file

@ -1387,10 +1387,12 @@ rb_mod_const_of(mod, data)
VALUE mod; VALUE mod;
void *data; void *data;
{ {
VALUE tmp = mod;
for (;;) { for (;;) {
data = rb_mod_const_at(mod, data); data = rb_mod_const_at(tmp, data);
mod = RCLASS(mod)->super; tmp = RCLASS(tmp)->super;
if (!mod) break; if (!tmp) break;
if (tmp == rb_cObject && mod != rb_cObject) break;
} }
return data; return data;
} }