ext/tuple.c in tuple-0.1.1 vs ext/tuple.c in tuple-0.1.2
- old
+ new
@@ -57,12 +57,19 @@
BDIGIT *digits;
if (TYPE(tuple) != T_ARRAY) tuple = rb_ary_new4(1, &tuple);
- for (i = 0; i < RARRAY(tuple)->len; i++) {
- item = RARRAY(tuple)->ptr[i];
+#if defined(RUBY_1_9_x)
+ for (i = 0; i < RARRAY_LEN(tuple); i++) {
+ item = RARRAY_PTR(tuple)[i];
+#elif defined(RUBY_1_8_x)
+ for (i = 0; i < RARRAY(tuple)->len; i++) {
+ item = RARRAY(tuple)->ptr[i];
+#else
+#error unsupported RUBY_VERSION
+#endif
header[0] = header[1] = header[2] = header[3] = 0;
if (FIXNUM_P(item)) {
fixnum = FIX2LONG(item);
sign = (fixnum >= 0);
if (!sign) fixnum = -fixnum;
@@ -78,17 +85,30 @@
}
digit = split64(fixnum, 0);
digit = htonl(sign ? digit : UINT_MAX - digit);
rb_str_cat(data, (char*)&digit, sizeof(digit));
} else if (TYPE(item) == T_BIGNUM) {
+#if defined(RUBY_1_9_x)
+ sign = RBIGNUM_SIGN(item);
+ len = RBIGNUM_LEN(item);
+#elif defined(RUBY_1_8_x)
sign = RBIGNUM(item)->sign;
len = RBIGNUM(item)->len;
+#else
+#error unsupported RUBY_VERSION
+#endif
header[2] = sign ? INTP_SORT : INTN_SORT;
header[3] = sign ? len : UCHAR_MAX - len;
rb_str_cat(data, (char*)&header, sizeof(header));
+#if defined(RUBY_1_9_x)
+ digits = RBIGNUM_DIGITS(item);
+#elif defined(RUBY_1_8_x)
digits = BDIGITS(item);
+#else
+#error unsupported RUBY_VERSION
+#endif
for (j = len-1; j >= 0; j--) {
digit = htonl(sign ? digits[j] : (UINT_MAX - digits[j]));
rb_str_cat(data, (char*)&digit, sizeof(digit));
}
} else if (SYMBOL_P(item) || TYPE(item) == T_STRING) {
@@ -136,18 +156,25 @@
}
return data;
}
static VALUE empty_bignum(int sign, int len) {
+#if defined(RUBY_1_9_x)
+ return rb_big_new(len, sign);
+#elif defined(RUBY_1_8_x)
/* Create an empty bignum with the right number of digits. */
NEWOBJ(num, struct RBignum);
OBJSETUP(num, rb_cBignum, T_BIGNUM);
+
num->sign = sign ? 1 : 0;
num->len = len;
num->digits = ALLOC_N(BDIGIT, len);
return (VALUE)num;
+#else
+#error unsupported RUBY_VERSION
+#endif
}
static VALUE tuple_parse(void **data, int data_len) {
VALUE tuple = rb_ary_new();
VALUE item;
@@ -170,10 +197,16 @@
case INTN_SORT:
sign = (header[2] == INTP_SORT);
len = sign ? header[3] : (UCHAR_MAX - header[3]);
item = empty_bignum(sign, len);
+#if defined(RUBY_1_9_x)
+ digits = RBIGNUM_DIGITS(item);
+#elif defined(RUBY_1_8_x)
digits = BDIGITS(item);
+#else
+#error unsupported RUBY_VERSION
+#endif
for (i = len-1; i >= 0; i--) {
digit = ntohl(*(u_int32_t*)ptr);
digits[i] = sign ? digit : UINT_MAX - digit;
ptr += 4;
}