summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-31 08:37:47 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-31 08:37:47 +0000
commitc90b1ecaf81868ab64b014401ea75eb45da2c5d0 (patch)
tree8aa0738f21319dc82e0403a4f065f831a907ebcc
parent5f4d324d3b7f9f50c7c1eb2ec6d2b546e4466f0b (diff)
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1023 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog25
-rw-r--r--array.c2
-rw-r--r--bignum.c189
-rw-r--r--configure.in1
-rw-r--r--gc.c4
-rw-r--r--marshal.c75
-rw-r--r--parse.y3
-rw-r--r--sample/test.rb14
-rw-r--r--win32/win32.c12
9 files changed, 224 insertions, 101 deletions
diff --git a/ChangeLog b/ChangeLog
index 04239bc54d..21a1252f27 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,28 @@
+Tue Oct 31 17:27:17 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * bignum.c: change digit size to `long|int' if long long is
+ available.
+
+ * marshal.c (w_object): support `long|int' digits.
+
+ * marshal.c (r_object): ditto.
+
+Sat Oct 28 23:54:22 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * parse.y (yylex): allow =end at the end of file (without a
+ newline at the end).
+
+Fri Oct 27 10:00:27 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * bignum.c (rb_cstr2inum): should ignore trailing white spaces.
+
+ * bignum.c (rb_str2inum): string may not have sentinel NUL.
+
+Fri Oct 27 02:37:22 2000 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * bignum.c (rb_cstr2inum): wrongly assigned base to c before
+ badcheck check.
+
Thu Oct 26 02:42:50 2000 Minero Aoki <aamine@dp.u-netsurf.ne.jp>
* lib/net/protocol.rb: Command#critical_ok
diff --git a/array.c b/array.c
index 48fb0a6429..617f6e22ac 100644
--- a/array.c
+++ b/array.c
@@ -1586,7 +1586,7 @@ rb_ary_flatten_bang(ary)
}
id = rb_obj_id(ary2);
if (rb_ary_includes(flattening, id)) {
- rb_raise(rb_eArgError, "tryed to flatten recursive array");
+ rb_raise(rb_eArgError, "tried to flatten recursive array");
}
rb_ary_push(flattening, id);
}
diff --git a/bignum.c b/bignum.c
index 3a5fed571c..8d8b92ddbe 100644
--- a/bignum.c
+++ b/bignum.c
@@ -20,15 +20,27 @@ VALUE rb_cBignum;
#define USHORT _USHORT
#endif
-typedef unsigned short USHORT;
+#if SIZEOF_LONG*2 <= SIZEOF_LONG_LONG
+typedef unsigned long BDIGIT;
+typedef unsigned long long BDIGIT2;
+typedef long long BDIGIT2_SIGNED;
+#elif SIZEOF_INT*2 <= SIZEOF_LONG_LONG
+typedef unsigned int BDIGIT;
+typedef unsigned long long BDIGIT2;
+typedef long long BDIGIT2_SIGNED;
+#else
+typedef unsigned short BDIGIT;
+typedef unsigned long BDIGIT2;
+typedef long BDIGIT2_SIGNED;
+#endif
-#define BDIGITS(x) ((USHORT*)RBIGNUM(x)->digits)
-#define BITSPERDIG (sizeof(short)*CHAR_BIT)
-#define BIGRAD (1L << BITSPERDIG)
-#define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(short)))
-#define BIGUP(x) ((unsigned long)(x) << BITSPERDIG)
+#define BDIGITS(x) ((BDIGIT*)RBIGNUM(x)->digits)
+#define BITSPERDIG (sizeof(BDIGIT)*CHAR_BIT)
+#define BIGRAD ((BDIGIT2)1 << BITSPERDIG)
+#define DIGSPERLONG ((unsigned int)(sizeof(long)/sizeof(BDIGIT)))
+#define BIGUP(x) ((BDIGIT2)(x) << BITSPERDIG)
#define BIGDN(x) RSHIFT(x,BITSPERDIG)
-#define BIGLO(x) ((USHORT)((x) & (BIGRAD-1)))
+#define BIGLO(x) ((BDIGIT)((x) & (BIGRAD-1)))
static VALUE
bignew_1(klass, len, sign)
@@ -40,7 +52,7 @@ bignew_1(klass, len, sign)
OBJSETUP(big, klass, T_BIGNUM);
big->sign = sign;
big->len = len;
- big->digits = ALLOC_N(USHORT, len);
+ big->digits = ALLOC_N(BDIGIT, len);
return (VALUE)big;
}
@@ -53,7 +65,7 @@ rb_big_clone(x)
{
VALUE z = bignew_1(CLASS_OF(x), RBIGNUM(x)->len, RBIGNUM(x)->sign);
- MEMCPY(BDIGITS(z), BDIGITS(x), USHORT, RBIGNUM(x)->len);
+ MEMCPY(BDIGITS(z), BDIGITS(x), BDIGIT, RBIGNUM(x)->len);
return z;
}
@@ -62,8 +74,8 @@ rb_big_2comp(x) /* get 2's complement */
VALUE x;
{
long i = RBIGNUM(x)->len;
- USHORT *ds = BDIGITS(x);
- long num;
+ BDIGIT *ds = BDIGITS(x);
+ BDIGIT2 num;
while (i--) ds[i] = ~ds[i];
i = 0; num = 1;
@@ -76,7 +88,7 @@ rb_big_2comp(x) /* get 2's complement */
for (i=1; i<RBIGNUM(x)->len; i++) {
if (ds[i] != 0) return;
}
- REALLOC_N(RBIGNUM(x)->digits, USHORT, RBIGNUM(x)->len++);
+ REALLOC_N(RBIGNUM(x)->digits, BDIGIT, RBIGNUM(x)->len++);
ds = BDIGITS(x);
ds[RBIGNUM(x)->len-1] = 1;
}
@@ -88,12 +100,12 @@ bignorm(x)
{
if (!FIXNUM_P(x)) {
long len = RBIGNUM(x)->len;
- USHORT *ds = BDIGITS(x);
+ BDIGIT *ds = BDIGITS(x);
while (len-- && !ds[len]) ;
RBIGNUM(x)->len = ++len;
- if (len*sizeof(USHORT) <= sizeof(VALUE)) {
+ if (len*sizeof(BDIGIT) <= sizeof(VALUE)) {
long num = 0;
while (len--) {
num = BIGUP(num) + ds[len];
@@ -120,16 +132,17 @@ VALUE
rb_uint2big(n)
unsigned long n;
{
- unsigned int i = 0;
- USHORT *digits;
+ BDIGIT2 num = n;
+ long i = 0;
+ BDIGIT *digits;
VALUE big;
i = 0;
big = bignew(DIGSPERLONG, 1);
digits = BDIGITS(big);
while (i < DIGSPERLONG) {
- digits[i++] = BIGLO(n);
- n = BIGDN(n);
+ digits[i++] = BIGLO(num);
+ num = BIGDN(num);
}
i = DIGSPERLONG;
@@ -181,11 +194,11 @@ rb_cstr2inum(str, base)
char *end;
int badcheck = (base==0)?1:0;
char sign = 1, c;
- unsigned long num;
+ BDIGIT2 num;
long len, blen = 1;
long i;
VALUE z;
- USHORT *zds;
+ BDIGIT *zds;
while (*str && ISSPACE(*str)) str++;
@@ -237,10 +250,9 @@ rb_cstr2inum(str, base)
unsigned long val = strtoul((char*)str, &end, base);
if (badcheck) {
- if (end == str || *end)
- goto bad;
+ if (end == str) goto bad; /* no number */
while (*end && ISSPACE(*end)) end++;
- if (*end) {
+ if (*end) { /* trailing garbage */
bad:
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
}
@@ -279,16 +291,14 @@ rb_cstr2inum(str, base)
c = c - 'A' + 10;
break;
default:
- c = base;
if (badcheck) {
if (ISSPACE(c)) {
while (*str && ISSPACE(*str)) str++;
- if (*str) {
- break;
- }
+ if (!*str) break;
}
rb_raise(rb_eArgError, "invalid literal for Integer: %s", s);
}
+ c = base;
break;
}
if (c >= base) break;
@@ -296,7 +306,7 @@ rb_cstr2inum(str, base)
num = c;
for (;;) {
while (i<blen) {
- num += zds[i]*base;
+ num += (BDIGIT2)zds[i]*base;
zds[i++] = BIGLO(num);
num = BIGDN(num);
}
@@ -320,6 +330,13 @@ rb_str2inum(str, base)
int len;
s = rb_str2cstr(str, &len);
+ if (s[len]) { /* no sentinel somehow */
+ char *p = ALLOCA_N(char, len+1);
+
+ MEMCPY(p, s, char, len);
+ p[len] = '\0';
+ s = p;
+ }
if (len != strlen(s)) {
rb_raise(rb_eArgError, "string for Integer contains null byte");
}
@@ -333,8 +350,8 @@ rb_big2str(x, base)
int base;
{
VALUE t;
- USHORT *ds;
- unsigned long i, j, hbase;
+ BDIGIT *ds;
+ long i, j, hbase;
VALUE ss;
char *s, c;
@@ -344,19 +361,19 @@ rb_big2str(x, base)
i = RBIGNUM(x)->len;
if (i == 0) return rb_str_new2("0");
if (base == 10) {
- j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i*241L)/800+2;
+ j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i*241L)/800+2;
hbase = 10000;
}
else if (base == 16) {
- j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)/4+2;
+ j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i)/4+2;
hbase = 0x10000;
}
else if (base == 8) {
- j = (sizeof(USHORT)/sizeof(char)*CHAR_BIT*i)+2;
+ j = (sizeof(BDIGIT)/sizeof(char)*CHAR_BIT*i)+2;
hbase = 010000;
}
else if (base == 2) {
- j = (sizeof(USHORT)*CHAR_BIT*i)+2;
+ j = (sizeof(BDIGIT)*CHAR_BIT*i)+2;
hbase = 020;
}
else {
@@ -373,10 +390,11 @@ rb_big2str(x, base)
s[0] = RBIGNUM(x)->sign ? '+' : '-';
while (i && j) {
long k = i;
- unsigned long num = 0;
+ BDIGIT2 num = 0;
+
while (k--) {
num = BIGUP(num) + ds[k];
- ds[k] = (USHORT)(num / hbase);
+ ds[k] = (BDIGIT)(num / hbase);
num %= hbase;
}
if (ds[i-1] == 0) i--;
@@ -408,11 +426,11 @@ big2ulong(x, type)
VALUE x;
char *type;
{
- unsigned long num;
long len = RBIGNUM(x)->len;
- USHORT *ds;
+ BDIGIT2 num;
+ BDIGIT *ds;
- if (len > sizeof(long)/sizeof(USHORT))
+ if (len > sizeof(long)/sizeof(BDIGIT))
rb_raise(rb_eRangeError, "bignum too big to convert into `%s'", type);
ds = BDIGITS(x);
num = 0;
@@ -450,9 +468,9 @@ static VALUE
dbl2big(d)
double d;
{
- unsigned long i = 0;
+ long i = 0;
long c;
- USHORT *digits;
+ BDIGIT *digits;
VALUE z;
double u = (d < 0)?-d:d;
@@ -473,7 +491,7 @@ dbl2big(d)
u *= BIGRAD;
c = (long)u;
u -= c;
- digits[i] = (USHORT)c;
+ digits[i] = (BDIGIT)c;
}
return z;
@@ -492,7 +510,7 @@ rb_big2dbl(x)
{
double d = 0.0;
long i = RBIGNUM(x)->len;
- USHORT *ds = BDIGITS(x);
+ BDIGIT *ds = BDIGITS(x);
while (i--) {
d = ds[i] + BIGRAD*d;
@@ -562,7 +580,7 @@ rb_big_eq(x, y)
}
if (RBIGNUM(x)->sign != RBIGNUM(y)->sign) return Qfalse;
if (RBIGNUM(x)->len != RBIGNUM(y)->len) return Qfalse;
- if (MEMCMP(BDIGITS(x),BDIGITS(y),USHORT,RBIGNUM(y)->len) != 0) return Qfalse;
+ if (MEMCMP(BDIGITS(x),BDIGITS(y),BDIGIT,RBIGNUM(y)->len) != 0) return Qfalse;
return Qtrue;
}
@@ -583,7 +601,7 @@ rb_big_neg(x)
{
VALUE z = rb_big_clone(x);
long i = RBIGNUM(x)->len;
- USHORT *ds = BDIGITS(z);
+ BDIGIT *ds = BDIGITS(z);
if (!RBIGNUM(x)->sign) rb_big_2comp(z);
while (i--) ds[i] = ~ds[i];
@@ -598,8 +616,8 @@ bigsub(x, y)
VALUE x, y;
{
VALUE z = 0;
- USHORT *zds;
- long num;
+ BDIGIT *zds;
+ BDIGIT2_SIGNED num;
long i;
i = RBIGNUM(x)->len;
@@ -624,7 +642,7 @@ bigsub(x, y)
zds = BDIGITS(z);
for (i = 0, num = 0; i < RBIGNUM(y)->len; i++) {
- num += (long)BDIGITS(x)[i] - BDIGITS(y)[i];
+ num += (BDIGIT2_SIGNED)BDIGITS(x)[i] - BDIGITS(y)[i];
zds[i] = BIGLO(num);
num = BIGDN(num);
}
@@ -647,7 +665,7 @@ bigadd(x, y, sign)
char sign;
{
VALUE z;
- long num;
+ BDIGIT2 num;
long i, len;
sign = (sign == RBIGNUM(y)->sign);
@@ -681,7 +699,7 @@ bigadd(x, y, sign)
BDIGITS(z)[i] = BDIGITS(y)[i];
i++;
}
- BDIGITS(z)[i] = (USHORT)num;
+ BDIGITS(z)[i] = (BDIGIT)num;
return z;
}
@@ -729,9 +747,9 @@ rb_big_mul(x, y)
VALUE x, y;
{
long i, j;
- unsigned long n = 0;
+ BDIGIT2 n = 0;
VALUE z;
- USHORT *zds;
+ BDIGIT *zds;
if (FIXNUM_P(x)) x = rb_int2big(FIX2LONG(x));
switch (TYPE(y)) {
@@ -754,11 +772,11 @@ rb_big_mul(x, y)
zds = BDIGITS(z);
while (j--) zds[j] = 0;
for (i = 0; i < RBIGNUM(x)->len; i++) {
- unsigned long dd = BDIGITS(x)[i];
+ BDIGIT2 dd = BDIGITS(x)[i];
if (dd == 0) continue;
n = 0;
for (j = 0; j < RBIGNUM(y)->len; j++) {
- int ee = n + dd * BDIGITS(y)[j];
+ BDIGIT2 ee = n + (BDIGIT2)dd * BDIGITS(y)[j];
n = zds[i + j] + ee;
if (ee) zds[i + j] = BIGLO(n);
n = BIGDN(n);
@@ -779,10 +797,10 @@ bigdivrem(x, y, divp, modp)
long nx = RBIGNUM(x)->len, ny = RBIGNUM(y)->len;
long i, j;
VALUE yy, z;
- USHORT *xds, *yds, *zds, *tds;
- unsigned long t2;
- long num;
- USHORT dd, q;
+ BDIGIT *xds, *yds, *zds, *tds;
+ BDIGIT2 t2;
+ BDIGIT2_SIGNED num;
+ BDIGIT dd, q;
yds = BDIGITS(y);
if (ny == 0 && yds[0] == 0) rb_num_zerodiv();
@@ -799,7 +817,7 @@ bigdivrem(x, y, divp, modp)
t2 = 0; i = nx;
while (i--) {
t2 = BIGUP(t2) + zds[i];
- zds[i] = (USHORT)(t2 / dd);
+ zds[i] = (BDIGIT)(t2 / dd);
t2 %= dd;
}
RBIGNUM(z)->sign = RBIGNUM(x)->sign==RBIGNUM(y)->sign;
@@ -812,13 +830,13 @@ bigdivrem(x, y, divp, modp)
zds = BDIGITS(z);
if (nx==ny) zds[nx+1] = 0;
while (!yds[ny-1]) ny--;
- if ((dd = BIGRAD/(int)(yds[ny-1]+1)) != 1) {
+ if ((dd = BIGRAD/(BDIGIT2_SIGNED)(yds[ny-1]+1)) != 1) {
yy = rb_big_clone(y);
tds = BDIGITS(yy);
j = 0;
num = 0;
while (j<ny) {
- num += (long)yds[j]*dd;
+ num += (BDIGIT2)yds[j]*dd;
tds[j++] = BIGLO(num);
num = BIGDN(num);
}
@@ -826,11 +844,11 @@ bigdivrem(x, y, divp, modp)
j = 0;
num = 0;
while (j<nx) {
- num += (long)xds[j]*dd;
+ num += (BDIGIT2)xds[j]*dd;
zds[j++] = BIGLO(num);
num = BIGDN(num);
}
- zds[j] = (USHORT)num;
+ zds[j] = (BDIGIT)num;
}
else {
zds[nx] = 0;
@@ -840,12 +858,12 @@ bigdivrem(x, y, divp, modp)
j = nx==ny?nx+1:nx;
do {
if (zds[j] == yds[ny-1]) q = BIGRAD-1;
- else q = (USHORT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
+ else q = (BDIGIT)((BIGUP(zds[j]) + zds[j-1])/yds[ny-1]);
if (q) {
i = 0; num = 0; t2 = 0;
do { /* multiply and subtract */
- int ee;
- t2 += (long)yds[i] * q;
+ BDIGIT2 ee;
+ t2 += (BDIGIT2)yds[i] * q;
ee = num - BIGLO(t2);
num = zds[j - ny + i] + ee;
if (ee) zds[j - ny + i] = BIGLO(num);
@@ -856,8 +874,8 @@ bigdivrem(x, y, divp, modp)
while (num) { /* "add back" required */
i = 0; num = 0; q--;
do {
- int ee = num + yds[i];
- num = (long) zds[j - ny + i] + ee;
+ BDIGIT2 ee = num + yds[i];
+ num = (BDIGIT2)zds[j - ny + i] + ee;
if (ee) zds[j - ny + i] = BIGLO(num);
num = BIGDN(num);
} while (++i < ny);
@@ -880,7 +898,7 @@ bigdivrem(x, y, divp, modp)
t2 = 0; i = ny;
while(i--) {
t2 = BIGUP(t2) + zds[i];
- zds[i] = (USHORT)(t2 / dd);
+ zds[i] = (BDIGIT)(t2 / dd);
t2 %= dd;
}
}
@@ -1049,7 +1067,7 @@ rb_big_and(x, y)
VALUE x, y;
{
VALUE z;
- USHORT *ds1, *ds2, *zds;
+ BDIGIT *ds1, *ds2, *zds;
long i, l1, l2;
char sign;
@@ -1100,8 +1118,8 @@ rb_big_or(x, y)
VALUE x, y;
{
VALUE z;
- USHORT *ds1, *ds2, *zds;
- unsigned long i, l1, l2;
+ BDIGIT *ds1, *ds2, *zds;
+ long i, l1, l2;
char sign;
if (FIXNUM_P(y)) {
@@ -1152,8 +1170,8 @@ rb_big_xor(x, y)
VALUE x, y;
{
VALUE z;
- USHORT *ds1, *ds2, *zds;
- unsigned int i, l1, l2;
+ BDIGIT *ds1, *ds2, *zds;
+ long i, l1, l2;
char sign;
if (FIXNUM_P(y)) {
@@ -1207,12 +1225,12 @@ VALUE
rb_big_lshift(x, y)
VALUE x, y;
{
- USHORT *xds, *zds;
+ BDIGIT *xds, *zds;
int shift = NUM2INT(y);
int s1 = shift/BITSPERDIG;
int s2 = shift%BITSPERDIG;
VALUE z;
- unsigned long num = 0;
+ BDIGIT2 num = 0;
long len, i;
if (shift < 0) return rb_big_rshift(x, INT2FIX(-shift));
@@ -1236,12 +1254,12 @@ static VALUE
rb_big_rshift(x, y)
VALUE x, y;
{
- USHORT *xds, *zds;
+ BDIGIT *xds, *zds;
int shift = NUM2INT(y);
int s1 = shift/BITSPERDIG;
int s2 = shift%BITSPERDIG;
VALUE z;
- unsigned long num = 0;
+ BDIGIT2 num = 0;
long i = RBIGNUM(x)->len;
long j;
@@ -1268,7 +1286,7 @@ static VALUE
rb_big_aref(x, y)
VALUE x, y;
{
- USHORT *xds;
+ BDIGIT *xds;
int shift = NUM2INT(y);
int s1, s2;
@@ -1294,12 +1312,11 @@ static VALUE
rb_big_hash(x)
VALUE x;
{
- long i, len;
- int key;
- USHORT *digits;
+ long i, len, key;
+ BDIGIT *digits;
- key = 0; digits = BDIGITS(x);
- for (i=0,len=RBIGNUM(x)->len; i<RBIGNUM(x)->len; i++) {
+ key = 0; digits = BDIGITS(x); len = RBIGNUM(x)->len;
+ for (i=0; i<len; i++) {
key ^= *digits++;
}
return INT2FIX(key);
@@ -1346,7 +1363,7 @@ rb_big_rand(max, rand)
len = RBIGNUM(max)->len;
v = bignew(len,1);
while (len--) {
- BDIGITS(v)[len] = ((USHORT)~0) * rand;
+ BDIGITS(v)[len] = ((BDIGIT)~0) * rand;
}
return rb_big_modulo((VALUE)v, max);
@@ -1356,7 +1373,7 @@ static VALUE
rb_big_size(big)
VALUE big;
{
- return INT2FIX(RBIGNUM(big)->len*sizeof(USHORT));
+ return INT2FIX(RBIGNUM(big)->len*sizeof(BDIGIT));
}
static VALUE
diff --git a/configure.in b/configure.in
index 806e0869cf..a87e098e43 100644
--- a/configure.in
+++ b/configure.in
@@ -112,6 +112,7 @@ AC_OBJEXT
AC_CHECK_SIZEOF(int, 4)
AC_CHECK_SIZEOF(short, 2)
AC_CHECK_SIZEOF(long, 4)
+AC_CHECK_SIZEOF(long long, 0)
AC_CHECK_SIZEOF(void*, 4)
AC_CHECK_SIZEOF(float, 4)
AC_CHECK_SIZEOF(double, 8)
diff --git a/gc.c b/gc.c
index 9c33634f92..1444a960a2 100644
--- a/gc.c
+++ b/gc.c
@@ -80,7 +80,7 @@ ruby_xmalloc(size)
mem = malloc(size);
if (!mem) {
if (size >= 10 * 1024 * 1024) {
- rb_raise(rb_eNoMemError, "tryed to allocate too big memory");
+ rb_raise(rb_eNoMemError, "tried to allocate too big memory");
}
mem_error("failed to allocate memory");
}
@@ -120,7 +120,7 @@ ruby_xrealloc(ptr, size)
mem = realloc(ptr, size);
if (!mem)
if (size >= 50 * 1024 * 1024) {
- rb_raise(rb_eNoMemError, "tryed to re-allocate too big memory");
+ rb_raise(rb_eNoMemError, "tried to re-allocate too big memory");
}
mem_error("failed to allocate memory(realloc)");
}
diff --git a/marshal.c b/marshal.c
index f0ae1f5d05..7ae88485a8 100644
--- a/marshal.c
+++ b/marshal.c
@@ -18,6 +18,42 @@
double strtod();
#endif
+#if SIZEOF_LONG*2 <= SIZEOF_LONG_LONG
+typedef unsigned long BDIGIT;
+#define SIZEOF_BDIGITS SIZEOF_LONG
+#elif SIZEOF_INT*2 <= SIZEOF_LONG_LONG
+typedef unsigned int BDIGIT;
+#define SIZEOF_BDIGITS SIZEOF_INT
+#else
+typedef unsigned short BDIGIT;
+#define SIZEOF_BDIGITS SIZEOF_SHORT
+#endif
+
+#define BITSPERSHORT (sizeof(short)*CHAR_BIT)
+#define SHORTMASK ((1<<BITSPERSHORT)-1)
+#define SHORTDN(x) RSHIFT(x,BITSPERSHORT)
+
+#if SIZEOF_SHORT == SIZEOF_BDIGITS
+#define SHORTLEN(x) (x)
+#else
+static int
+shortlen(len, ds)
+ long len;
+ BDIGIT *ds;
+{
+ BDIGIT num;
+ int offset = 0;
+
+ num = ds[len-1];
+ while (num) {
+ num = SHORTDN(num);
+ offset++;
+ }
+ return len*sizeof(BDIGIT)/sizeof(short) - offset;
+}
+#define SHORTLEN(x) shortlen((x),d)
+#endif
+
#define MARSHAL_MAJOR 4
#define MARSHAL_MINOR 4
@@ -318,12 +354,23 @@ w_object(obj, arg, limit)
{
char sign = RBIGNUM(obj)->sign?'+':'-';
int len = RBIGNUM(obj)->len;
- unsigned short *d = RBIGNUM(obj)->digits;
+ BDIGIT *d = RBIGNUM(obj)->digits;
w_byte(sign, arg);
- w_long(len, arg);
+ w_long(SHORTLEN(len), arg);
while (len--) {
+#if SIZEOF_BDIGITS > SIZEOF_SHORT
+ BDIGIT num = *d;
+ int i;
+
+ for (i=0; i<SIZEOF_BDIGITS; i+=sizeof(short)) {
+ w_short(num & SHORTMASK, arg);
+ num = SHORTDN(num);
+ if (num == 0) break;
+ }
+#else
w_short(*d, arg);
+#endif
d++;
}
}
@@ -752,15 +799,31 @@ r_object(arg)
case TYPE_BIGNUM:
{
int len;
- unsigned short *digits;
+ BDIGIT *digits;
NEWOBJ(big, struct RBignum);
OBJSETUP(big, rb_cBignum, T_BIGNUM);
big->sign = (r_byte(arg) == '+');
- big->len = len = r_long(arg);
- big->digits = digits = ALLOC_N(unsigned short, len);
- while (len--) {
+ len = r_long(arg);
+ big->len = (len + 1) * sizeof(short) / sizeof(BDIGIT);
+ big->digits = digits = ALLOC_N(BDIGIT, big->len);
+ while (len > 0) {
+#if SIZEOF_BDIGITS > SIZEOF_SHORT
+ BDIGIT num = 0;
+ int shift = 0;
+ int i;
+
+ for (i=0; i<SIZEOF_BDIGITS; i+=sizeof(short)) {
+ int j = r_short(arg);
+ num |= j << shift;
+ shift += BITSPERSHORT;
+ if (--len == 0) break;
+ }
+ *digits++ = num;
+#else
*digits++ = r_short(arg);
+ len--;
+#endif
}
big = RBIGNUM(rb_big_norm((VALUE)big));
if (TYPE(big) == T_BIGNUM) {
diff --git a/parse.y b/parse.y
index ae55beee32..2e1882626b 100644
--- a/parse.y
+++ b/parse.y
@@ -2863,7 +2863,8 @@ yylex()
return 0;
}
if (c != '=') continue;
- if (strncmp(lex_p, "end", 3) == 0 && ISSPACE(lex_p[3])) {
+ if (strncmp(lex_p, "end", 3) == 0 &&
+ (lex_p + 3 == lex_pend || ISSPACE(lex_p[3]))) {
break;
}
}
diff --git a/sample/test.rb b/sample/test.rb
index 1106053c10..8f9805f60a 100644
--- a/sample/test.rb
+++ b/sample/test.rb
@@ -385,6 +385,18 @@ $z = [1,2]
$y[$z] = 256
test_ok($y[$z] == 256)
+$x = [1,2,3]
+$x[1,0] = $x
+test_ok($x == [1,1,2,3,2,3])
+
+$x = [1,2,3]
+$x[-1,0] = $x
+test_ok($x == [1,2,1,2,3,3])
+
+$x = [1,2,3]
+$x.concat($x)
+test_ok($x == [1,2,3,1,2,3])
+
test_check "iterator"
test_ok(!iterator?)
@@ -571,7 +583,6 @@ def fact(n)
end
return f
end
-fact(3)
$x = fact(40)
test_ok($x == $x)
test_ok($x == fact(40))
@@ -613,6 +624,7 @@ $good = true;
for i in 4000..4096
n1 = 1 << i;
if (n1**2-1) / (n1+1) != (n1-1)
+ p i
$good = false
end
end
diff --git a/win32/win32.c b/win32/win32.c
index 9142c4d94a..a2b256d74d 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -1724,26 +1724,30 @@ myfdclose(FILE *fp)
#undef strerror
-char *
-mystrerror(int e)
+char *
+mystrerror(int e)
{
static char buffer[512];
#if !defined __MINGW32__
extern int sys_nerr;
#endif
DWORD source = 0;
+ char *p;
if (e < 0 || e > sys_nerr) {
if (e < 0)
e = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, &source, e, 0,
buffer, 512, NULL) == 0) {
- strcpy (buffer, "Unknown Error");
+ strcpy(buffer, "Unknown Error");
+ }
+ for (p = buffer + strlen(buffer) - 1; buffer <= p; p--) {
+ if (*p != '\r' && *p != '\n') break;
+ *p = 0;
}
return buffer;
}
return strerror(e);
-
}
//