diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-03 11:02:53 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-03 11:02:53 +0000 |
commit | 9d22a06ea06e9bec5030fd6e2c8481958fcd30d9 (patch) | |
tree | 58b15b4dc85acca1c806388ff3848a3c85fd0ebf | |
parent | a11ab83884c47f729a8630ab5ef21c7522b48386 (diff) | |
download | ruby-9d22a06ea06e9bec5030fd6e2c8481958fcd30d9.tar.gz |
* 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
-rw-r--r-- | ChangeLog | 33 | ||||
-rw-r--r-- | array.c | 54 | ||||
-rw-r--r-- | eval.c | 15 | ||||
-rw-r--r-- | intern.h | 1 | ||||
-rw-r--r-- | lib/debug.rb | 2 | ||||
-rw-r--r-- | lib/irb/context.rb | 1 | ||||
-rw-r--r-- | lib/mkmf.rb | 1 | ||||
-rw-r--r-- | lib/timeout.rb | 41 | ||||
-rw-r--r-- | re.c | 49 | ||||
-rw-r--r-- | sprintf.c | 2 | ||||
-rw-r--r-- | string.c | 9 | ||||
-rw-r--r-- | struct.c | 37 | ||||
-rw-r--r-- | variable.c | 8 |
13 files changed, 157 insertions, 96 deletions
@@ -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> * 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. +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> + * string.c (rb_str_new4): + * string.c (rb_str_shared_replace): clear flags before copy. * string.c (rb_str_replace): ditto. @@ -1218,29 +1218,37 @@ rb_ary_collect_bang(ary) return ary; } -static void -push_values_at(result, ary, arg) - VALUE result, ary, arg; +VALUE +rb_values_at(obj, olen, argc, argv, func) + 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)) { - rb_ary_push(result, rb_ary_entry(ary, FIX2LONG(arg))); - return; - } - /* check if idx is Range */ - switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) { - case Qfalse: - break; - case Qnil: - return; - default: - for (i=0; i<len; i++) { - rb_ary_push(result, rb_ary_entry(ary, i+beg)); + for (i=0; i<argc; i++) { + if (FIXNUM_P(argv[i])) { + rb_ary_push(result, (*func)(obj, FIX2LONG(argv[i]))); + continue; + } + /* check if idx is Range */ + switch (rb_range_beg_len(argv[i], &beg, &len, olen, 0)) { + case Qfalse: + break; + case Qnil: + continue; + default: + for (j=0; j<len; j++) { + rb_ary_push(result, (*func)(obj, j+beg)); + } + continue; } - return; + rb_ary_push(result, (*func)(obj, NUM2LONG(argv[i]))); } - rb_ary_push(result, rb_ary_entry(ary, NUM2LONG(arg))); + return result; } static VALUE @@ -1249,13 +1257,7 @@ rb_ary_values_at(argc, argv, ary) VALUE *argv; VALUE ary; { - VALUE result = rb_ary_new2(argc); - long i; - - for (i=0; i<argc; i++) { - push_values_at(result, ary, argv[i]); - } - return result; + return rb_values_at(ary, RARRAY(ary)->len, argc, argv, rb_ary_entry); } static VALUE @@ -362,6 +362,7 @@ rb_get_method_body(klassp, idp, noexp) body = ent->method = body->nd_head; } else { + if (BUILTIN_TYPE(origin) == T_ICLASS) origin = RBASIC(origin)->klass; *klassp = origin; ent->origin = origin; ent->mid = ent->mid0 = id; @@ -1824,10 +1825,16 @@ rb_undef(klass, id) } static VALUE -rb_mod_undef_method(mod, name) - VALUE mod, name; +rb_mod_undef_method(argc, argv, mod) + 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; } @@ -6521,7 +6528,7 @@ Init_eval() 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, "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, "define_method", rb_mod_define_method, -1); @@ -56,6 +56,7 @@ VALUE rb_ary_cmp _((VALUE, VALUE)); VALUE rb_protect_inspect _((VALUE(*)(ANYARGS),VALUE,VALUE)); VALUE rb_inspecting_p _((VALUE)); VALUE rb_check_array_value _((VALUE)); +VALUE rb_values_at _((VALUE, long, int, VALUE*, VALUE(*) _((VALUE,long)))); /* bignum.c */ VALUE rb_big_clone _((VALUE)); void rb_big_2comp _((VALUE)); diff --git a/lib/debug.rb b/lib/debug.rb index 0caeecb00c..9275b47cd9 100644 --- a/lib/debug.rb +++ b/lib/debug.rb @@ -700,9 +700,11 @@ EOHELP @line = line case event when 'line' + p [line, @stop_next] frame_set_pos(file, line) if !@no_step or @frames.size == @no_step @stop_next -= 1 + @stop_next = 0 if @stop_next < 0 elsif @frames.size < @no_step @stop_next = 0 # break here before leaving... else diff --git a/lib/irb/context.rb b/lib/irb/context.rb index 9022e8138c..f1194f8db9 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -177,6 +177,7 @@ module IRB @inspect_mode end + undef use_readline= def use_readline=(opt) @use_readline = opt print "use readline module\n" if @use_readline diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 7ae2349046..9b01d67c4c 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -8,6 +8,7 @@ CONFIG = Config::MAKEFILE_CONFIG ORIG_LIBPATH = ENV['LIB'] SRC_EXT = ["c", "cc", "m", "cxx", "cpp", "C"] +$static = $config_h = nil unless defined? $configure_args $configure_args = {} diff --git a/lib/timeout.rb b/lib/timeout.rb index 8c6ccb7128..00e60aff8c 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -23,25 +23,38 @@ # # The time in seconds to wait for block teminatation. # +# : [exception] +# +# The exception classs to be raised on timeout. +# #=end -class TimeoutError<Interrupt -end +module Timeout + class Error<Interrupt + end -def timeout(sec, exception=TimeoutError) - return yield if sec == nil - begin - x = Thread.current - y = Thread.start { - sleep sec - x.raise exception, "execution expired" if x.alive? - } - yield sec -# return true - ensure - y.kill if y and y.alive? + def timeout(sec, exception=Error) + return yield if sec == nil + begin + x = Thread.current + y = Thread.start { + sleep sec + x.raise exception, "execution expired" if x.alive? + } + yield sec + # return true + ensure + y.kill if y and y.alive? + end end + module_function :timeout +end + +# compatible +def timeout(n, &block) + Timeout::timeout(n, &block) end +TimeoutError = Timeout::Error if __FILE__ == $0 p timeout(5) { @@ -914,8 +914,9 @@ last_paren_match_getter() } static VALUE -match_to_a(match) +match_array(match, start) VALUE match; + int start; { struct re_registers *regs = RMATCH(match)->regs; VALUE ary = rb_ary_new2(regs->num_regs); @@ -923,7 +924,7 @@ match_to_a(match) int i; 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) { rb_ary_push(ary, Qnil); } @@ -937,6 +938,20 @@ match_to_a(match) } 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 match_aref(argc, argv, match) int argc; VALUE *argv; @@ -953,31 +968,20 @@ match_aref(argc, argv, match) } static VALUE +match_entry(match, n) + VALUE match; + long n; +{ + return rb_reg_nth_match(n, match); +} + +static VALUE match_values_at(argc, argv, match) int argc; VALUE *argv; VALUE match; { - struct re_registers *regs = RMATCH(match)->regs; - 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; + return rb_values_at(match, RMATCH(match)->regs->num_regs, argc, argv, match_entry); } static VALUE @@ -1766,6 +1770,7 @@ Init_Regexp() 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, "[]", 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, "values_at", match_values_at, -1); rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0); @@ -291,10 +291,12 @@ rb_f_sprintf(argc, argv) break; case 's': + case 'p': { VALUE arg = GETARG(); long len; + if (*p == 'p') arg = rb_inspect(arg); str = rb_obj_as_string(arg); if (OBJ_TAINTED(str)) tainted = 1; len = RSTRING(str)->len; @@ -157,14 +157,17 @@ rb_str_new4(orig) { VALUE klass, str; + if (OBJ_FROZEN(orig)) return orig; klass = rb_obj_class(orig); if (FL_TEST(orig, ELTS_SHARED) && RSTRING(orig)->aux.shared) { long ofs; str = RSTRING(orig)->aux.shared; ofs = RSTRING(str)->len - RSTRING(orig)->len; - str = str_new3(klass, str); - RSTRING(str)->ptr += ofs; - RSTRING(str)->len -= ofs; + if (ofs > 0) { + str = str_new3(klass, str); + RSTRING(str)->ptr += ofs; + RSTRING(str)->len -= ofs; + } } else if (FL_TEST(orig, STR_ASSOC)) { str = str_new(klass, RSTRING(orig)->ptr, RSTRING(orig)->len); @@ -352,18 +352,6 @@ rb_struct_each_pair(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 inspect_struct(s) VALUE s; { @@ -376,7 +364,7 @@ inspect_struct(s) 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, " "); for (i=0; i<RSTRUCT(s)->len; i++) { @@ -405,9 +393,9 @@ rb_struct_inspect(s) { if (rb_inspecting_p(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); return str; } @@ -530,19 +518,20 @@ rb_struct_aset(s, idx, val) } static VALUE +struct_entry(s, n) + VALUE s; + long n; +{ + return rb_struct_aref(s, LONG2NUM(n)); +} + +static VALUE rb_struct_values_at(argc, argv, s) int argc; VALUE *argv; VALUE s; { - VALUE result = rb_ary_new(); - long i; - - for (i=0; i<argc; i++) { - rb_ary_push(result, rb_struct_aref(s, argv[i])); - } - - return result; + return rb_values_at(s, RSTRUCT(s)->len, argc, argv, struct_entry); } static VALUE @@ -648,7 +637,7 @@ Init_Struct() 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, "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, "to_a", rb_struct_to_a, 0); rb_define_method(rb_cStruct, "values", rb_struct_to_a, 0); diff --git a/variable.c b/variable.c index fcbc8cc745..8b62aad92a 100644 --- a/variable.c +++ b/variable.c @@ -1387,10 +1387,12 @@ rb_mod_const_of(mod, data) VALUE mod; void *data; { + VALUE tmp = mod; for (;;) { - data = rb_mod_const_at(mod, data); - mod = RCLASS(mod)->super; - if (!mod) break; + data = rb_mod_const_at(tmp, data); + tmp = RCLASS(tmp)->super; + if (!tmp) break; + if (tmp == rb_cObject && mod != rb_cObject) break; } return data; } |