summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-07 07:36:40 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-01-07 07:36:40 +0000
commit6d481a4ae440532dcf674adf910c5cfa802f4679 (patch)
tree9eb994643aa02d75494ab17b10f33eee31bf8a9c /array.c
parent954826f7a84722628b5386868dfdf7da2f4bd58a (diff)
* hash.c (env_clear): new Hash compatible method.
* hash.c (env_shift): ditto. * hash.c (env_invert): ditto. * hash.c (env_replace): ditto. * hash.c (env_update): ditto. * array.c (rb_ary_join): dispatch based on "to_str". * array.c (rb_ary_times): ditto. * array.c (rb_ary_equal): ditto. * process.c (rb_f_exec): dispatch based on "to_ary". * eval.c (umethod_bind): exact class match is not required. relax the restriction to subclasses. * eval.c (rb_eval): call "inherited" before executing class body. * class.c (rb_define_class): call "inherited" after defining the constant. * class.c (rb_define_class_under): ditto. * eval.c (massign): expand first element if RHS is an array and its size is 1, and LHS has concrete assignment target (i.e. LHS has target(s) other than *var). * eval.c (massign): avoid unnecessary avalue/svalue conversion. * eval.c (rb_yield_0): ditto * array.c (rb_ary_update): do not allocate unused array if rpl is nil (i.e. merely removing elements). * io.c (io_read): should resize supplied string if it's shorter than expected. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3307 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'array.c')
-rw-r--r--array.c45
1 files changed, 29 insertions, 16 deletions
diff --git a/array.c b/array.c
index d3142c9..934fb36 100644
--- a/array.c
+++ b/array.c
@@ -648,9 +648,6 @@ rb_ary_update(ary, beg, len, rpl)
{
long rlen;
- rpl = (NIL_P(rpl)) ? rb_ary_new2(0) : rb_ary_to_ary(rpl);
- rlen = RARRAY(rpl)->len;
-
if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
if (beg < 0) {
beg += RARRAY(ary)->len;
@@ -664,6 +661,14 @@ rb_ary_update(ary, beg, len, rpl)
}
rb_ary_modify(ary);
+ if (NIL_P(rpl)) {
+ rlen = 0;
+ }
+ else {
+ rpl = rb_ary_to_ary(rpl);
+ rlen = RARRAY(rpl)->len;
+ }
+
if (beg >= RARRAY(ary)->len) {
len = beg + rlen;
if (len >= RARRAY(ary)->aux.capa) {
@@ -671,7 +676,9 @@ rb_ary_update(ary, beg, len, rpl)
RARRAY(ary)->aux.capa = len;
}
rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, beg - RARRAY(ary)->len);
- MEMCPY(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
+ if (rlen > 0) {
+ MEMCPY(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
+ }
RARRAY(ary)->len = len;
}
else {
@@ -692,7 +699,9 @@ rb_ary_update(ary, beg, len, rpl)
VALUE, RARRAY(ary)->len - (beg + len));
RARRAY(ary)->len = alen;
}
- MEMMOVE(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
+ if (rlen > 0) {
+ MEMMOVE(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
+ }
}
}
@@ -836,12 +845,8 @@ rb_ary_join(ary, sep)
if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = Qtrue;
for (i=0; i<RARRAY(ary)->len; i++) {
- if (TYPE(RARRAY(ary)->ptr[i]) == T_STRING) {
- len += RSTRING(RARRAY(ary)->ptr[i])->len;
- }
- else {
- len += 10;
- }
+ tmp = rb_check_string_type(RARRAY(ary)->ptr[i]);
+ len += NIL_P(tmp) ? 10 : RSTRING(tmp)->len;
}
if (!NIL_P(sep)) {
StringValue(sep);
@@ -1488,11 +1493,12 @@ static VALUE
rb_ary_times(ary, times)
VALUE ary, times;
{
- VALUE ary2;
+ VALUE ary2, tmp;
long i, len;
- if (TYPE(times) == T_STRING) {
- return rb_ary_join(ary, times);
+ tmp = rb_check_string_type(times);
+ if (!NIL_P(tmp)) {
+ return rb_ary_join(ary, tmp);
}
len = NUM2LONG(times);
@@ -1557,7 +1563,10 @@ rb_ary_equal(ary1, ary2)
long i;
if (ary1 == ary2) return Qtrue;
- if (TYPE(ary2) != T_ARRAY) return Qfalse;
+ if (TYPE(ary2) != T_ARRAY) {
+ ary2 = rb_check_array_type(ary2);
+ if (NIL_P(ary2)) return Qfalse;
+ }
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
@@ -1572,7 +1581,11 @@ rb_ary_eql(ary1, ary2)
{
long i;
- if (TYPE(ary2) != T_ARRAY) return Qfalse;
+ if (ary1 == ary2) return Qtrue;
+ if (TYPE(ary2) != T_ARRAY) {
+ ary2 = rb_check_array_type(ary2);
+ if (NIL_P(ary2)) return Qfalse;
+ }
if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
for (i=0; i<RARRAY(ary1)->len; i++) {
if (!rb_eql(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))