Skip to content

Instantly share code, notes, and snippets.

@nobu
Created July 8, 2009 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobu/143286 to your computer and use it in GitHub Desktop.
Save nobu/143286 to your computer and use it in GitHub Desktop.
Index: bignum.c
===================================================================
--- bignum.c (revision 23992)
+++ bignum.c (working copy)
@@ -54,4 +54,10 @@ bigzero_p(VALUE x)
int
+rb_bigzero_p(VALUE x)
+{
+ return BIGZEROP(x);
+}
+
+int
rb_cmpint(VALUE val, VALUE a, VALUE b)
{
Index: ext/decimal/decimal.c
===================================================================
--- ext/decimal/decimal.c (revision 83)
+++ ext/decimal/decimal.c (working copy)
@@ -427,18 +427,14 @@ dec_to_s(VALUE self)
if (d == DEC_NINF) return rb_str_new2("-Infinity");
if (d->inum == DEC_PZERO || d->inum == DEC_NZERO) {
- const size_t HEAD_LEN = d->inum == DEC_PZERO ? 2U : 3U; /* "-0.".length */
- long len = HEAD_LEN + d->scale;
- char *buf;
-
/* FIXME: use "0eN" style when the scale is negative? */
if (d->scale <= 0) /* ignore the case of negative scale */
return d->inum == DEC_PZERO ? rb_str_new2("0") : rb_str_new2("-0");
- buf = xmalloc(len);
- if (d->inum == DEC_PZERO)
- memcpy(buf, "0.", HEAD_LEN);
- else
- memcpy(buf, "-0.", HEAD_LEN);
- memset(buf + HEAD_LEN, '0', d->scale);
- return rb_str_new(buf, len);
+#if SIZEOF_LONG > SIZEOF_INT
+ if (d->scale > INT_MAX) {
+ rb_raise(rb_eArgError, "too big Decimal");
+ }
+#endif
+ return rb_sprintf("%s0.%0*d", (d->inum == DEC_PZERO ? "" : "-"),
+ (int)d->scale, 0);
}
return finite_to_s(d);
@@ -827,4 +823,7 @@ do_round(Decimal *d, long scale, VALUE m
}
}
+ else {
+ rb_bug("do_round(): invalid round mode");
+ }
coda:
if (negative) inum = INUM_UMINUS(inum);
Index: ext/decimal/inum19.h
===================================================================
--- ext/decimal/inum19.h (revision 83)
+++ ext/decimal/inum19.h (working copy)
@@ -15,18 +15,7 @@
#define BDIGITS(x) (RBIGNUM_DIGITS(x))
-#define BIGZEROP(x) (RBIGNUM_LEN(x) == 0 || \
- (BDIGITS(x)[0] == 0 && \
- (RBIGNUM_LEN(x) == 1 || bigzero_p(x))))
-
-static int
-bigzero_p(VALUE x)
-{
- long i;
- for (i = RBIGNUM_LEN(x) - 1; 0 <= i; i--) {
- if (BDIGITS(x)[i]) return 0;
- }
- return 1;
-}
+#define BIGZEROP(x) (rb_bigzero_p(x))
+int rb_bigzero_p(VALUE x);
static VALUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment