From f2cdaab8cf318acc4ff346307b67ac661f7fa23b Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 13 Dec 2001 08:19:09 +0000 Subject: * time.c (time_new_internal): avoid loop to calculate negative div, mod. * time.c (time_cmp): should handle Bignums. * array.c (rb_ary_pop): should ELTS_SHARED flag check before REALLOC. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@1909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 12 ++++++++++++ array.c | 4 +++- ext/gdbm/gdbm.c | 12 ++++++------ time.c | 41 ++++++++++++++++++++++++++++------------- version.h | 4 ++-- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index d1fc61e1c..d83526be3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Thu Dec 13 09:52:59 2001 Yukihiro Matsumoto + + * time.c (time_new_internal): avoid loop to calculate negative + div, mod. + + * time.c (time_cmp): should handle Bignums. + +Tue Dec 11 17:39:16 2001 K.Kosako + + * array.c (rb_ary_pop): should ELTS_SHARED flag check before + REALLOC. + Tue Dec 11 12:45:28 2001 Yukihiro Matsumoto * string.c (rb_str_match_m): should convert an argument into diff --git a/array.c b/array.c index c9b66eb2e..b58f484fc 100644 --- a/array.c +++ b/array.c @@ -336,7 +336,9 @@ rb_ary_pop(ary) { rb_ary_modify_check(ary); if (RARRAY(ary)->len == 0) return Qnil; - if (RARRAY(ary)->len * 10 < RARRAY(ary)->aux.capa && RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) { + if (!FL_TEST(ary, ELTS_SHARED) && + RARRAY(ary)->len * 10 < RARRAY(ary)->aux.capa && + RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) { RARRAY(ary)->aux.capa = RARRAY(ary)->len * 2; REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa); } diff --git a/ext/gdbm/gdbm.c b/ext/gdbm/gdbm.c index 0fa13a5e6..eddeb11f8 100644 --- a/ext/gdbm/gdbm.c +++ b/ext/gdbm/gdbm.c @@ -159,13 +159,13 @@ rb_gdbm_fetch(dbm, key) datum key; { datum val; - VALUE str = rb_obj_alloc(rb_cString); + VALUE str; val = gdbm_fetch(dbm, key); if (val.dptr == 0) return Qnil; - RSTRING(str)->ptr = 0; + str = rb_obj_alloc(rb_cString); RSTRING(str)->len = val.dsize; RSTRING(str)->aux.capa = val.dsize; RSTRING(str)->ptr = REALLOC_N(val.dptr,char,val.dsize+1); @@ -206,13 +206,13 @@ rb_gdbm_firstkey(dbm) GDBM_FILE dbm; { datum key; - VALUE str = rb_obj_alloc(rb_cString); + VALUE str; key = gdbm_firstkey(dbm); if (key.dptr == 0) return Qnil; - RSTRING(str)->ptr = 0; + str = rb_obj_alloc(rb_cString); RSTRING(str)->len = key.dsize; RSTRING(str)->aux.capa = key.dsize; RSTRING(str)->ptr = REALLOC_N(key.dptr,char,key.dsize+1); @@ -228,7 +228,7 @@ rb_gdbm_nextkey(dbm, keystr) VALUE keystr; { datum key, key2; - VALUE str = rb_obj_alloc(rb_cString); + VALUE str; key.dptr = RSTRING(keystr)->ptr; key.dsize = RSTRING(keystr)->len; @@ -236,7 +236,7 @@ rb_gdbm_nextkey(dbm, keystr) if (key2.dptr == 0) return Qnil; - RSTRING(str)->ptr = 0; + str = rb_obj_alloc(rb_cString); RSTRING(str)->len = key2.dsize; RSTRING(str)->aux.capa = key2.dsize; RSTRING(str)->ptr = REALLOC_N(key2.dptr,char,key2.dsize+1); diff --git a/time.c b/time.c index 655a49f1f..bffb985af 100644 --- a/time.c +++ b/time.c @@ -60,6 +60,9 @@ time_s_now(klass) return obj; } +#define NDIV(x,y) (-(-((x)+1)/(y))-1) +#define NMOD(x,y) ((y)-(-((x)+1)%(y))-1) + static VALUE time_new_internal(klass, sec, usec) VALUE klass; @@ -68,13 +71,13 @@ time_new_internal(klass, sec, usec) VALUE obj; struct time_object *tobj; - if (usec >= 1000000) { /* usec overflow */ + if (usec >= 1000000) { /* usec positive overflow */ sec += usec / 1000000; usec %= 1000000; } - while (usec < 0) { /* usec underflow */ - sec--; - usec += 1000000; + if (usec < 0) { /* usec negative overflow */ + sec += NDIV(usec,1000000); /* negative div */ + usec = NMOD(usec,1000000); /* negative mod */ } #ifndef NEGATIVE_TIME_T if (sec < 0 || (sec == 0 && usec < 0)) @@ -642,6 +645,14 @@ time_cmp(time1, time2) if (tobj1->tv.tv_sec > tobj2->tv.tv_sec) return INT2FIX(1); return INT2FIX(-1); } + if (TYPE(time2) == T_BIGNUM) { + double a = (double)tobj1->tv.tv_sec+(double)tobj1->tv.tv_usec/1e6; + double b = rb_big2dbl(time2); + + if (a == b) return INT2FIX(0); + if (a > b) return INT2FIX(1); + if (a < b) return INT2FIX(-1); + } i = NUM2LONG(time2); if (tobj1->tv.tv_sec == i) { if (tobj1->tv.tv_usec == 0) @@ -856,12 +867,15 @@ time_plus(time1, time2) } f = NUM2DBL(time2); sec = (time_t)f; + if (f != (double)sec) { + rb_raise(rb_eRangeError, "time + %f out of Time range", f); + } usec = tobj->tv.tv_usec + (time_t)((f - (double)sec)*1e6); sec = tobj->tv.tv_sec + sec; #ifdef NEGATIVE_TIME_T - if ((tobj->tv.tv_sec > 0 && f > 0 && sec < 0) || - (tobj->tv.tv_sec < 0 && f < 0 && sec > 0)) { + if ((tobj->tv.tv_sec >= 0 && f >= 0 && sec < 0) || + (tobj->tv.tv_sec <= 0 && f <= 0 && sec > 0)) { rb_raise(rb_eRangeError, "time + %f out of Time range", f); } #endif @@ -891,15 +905,16 @@ time_minus(time1, time2) return rb_float_new(f); } - else { - f = NUM2DBL(time2); - sec = (time_t)f; - usec = tobj->tv.tv_usec - (time_t)((f - (double)sec)*1e6); - sec = tobj->tv.tv_sec - sec; + f = NUM2DBL(time2); + sec = (time_t)f; + if (f != (double)sec) { + rb_raise(rb_eRangeError, "time - %f out of Time range", f); } + usec = tobj->tv.tv_usec - (time_t)((f - (double)sec)*1e6); + sec = tobj->tv.tv_sec - sec; #ifdef NEGATIVE_TIME_T - if ((tobj->tv.tv_sec < 0 && f > 0 && sec > 0) || - (tobj->tv.tv_sec > 0 && f < 0 && sec < 0)) { + if ((tobj->tv.tv_sec <= 0 && f >= 0 && sec > 0) || + (tobj->tv.tv_sec >= 0 && f <= 0 && sec < 0)) { rb_raise(rb_eRangeError, "time - %f out of Time range", f); } #endif diff --git a/version.h b/version.h index fb712da79..1826f8efc 100644 --- a/version.h +++ b/version.h @@ -1,4 +1,4 @@ #define RUBY_VERSION "1.7.2" -#define RUBY_RELEASE_DATE "2001-12-10" +#define RUBY_RELEASE_DATE "2001-12-13" #define RUBY_VERSION_CODE 172 -#define RUBY_RELEASE_CODE 20011210 +#define RUBY_RELEASE_CODE 20011213 -- cgit