Add exception keyword in BigDecimal()
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 32beb8f..470a116 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c
@@ -646,7 +646,7 @@ VpNewRbClass(size_t mx, const char *str, VALUE klass) { VALUE obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, 0); - Real *pv = VpAlloc(mx, str, 1); + Real *pv = VpAlloc(mx, str, 1, 1); RTYPEDDATA_DATA(obj) = pv; pv->obj = obj; RB_OBJ_FREEZE(obj); @@ -2560,19 +2560,60 @@ return self; } +static int +opts_exception_p(VALUE opts) +{ + static ID kwds[1]; + VALUE exception; + if (!kwds[0]) { + kwds[0] = rb_intern_const("exception"); + } + rb_get_kwargs(opts, kwds, 0, 1, &exception); + return exception != Qfalse; +} + static Real * BigDecimal_new(int argc, VALUE *argv) { size_t mf; + VALUE opts = Qnil; VALUE nFig; VALUE iniValue; double d; + int exc; - if (rb_scan_args(argc, argv, "11", &iniValue, &nFig) == 1) { + argc = rb_scan_args(argc, argv, "11:", &iniValue, &nFig, &opts); + exc = opts_exception_p(opts); + + if (argc == 1) { mf = 0; } else { - mf = GetPrecisionInt(nFig); + /* expand GetPrecisionInt for exception suppression */ + ssize_t n = NUM2INT(nFig); + if (n < 0) { + if (!exc) { + return NULL; + } + rb_raise(rb_eArgError, "negative precision"); + } + mf = (size_t)n; + } + + if (SPECIAL_CONST_P(iniValue)) { + switch (iniValue) { + case Qnil: + if (!exc) return NULL; + rb_raise(rb_eTypeError, "can't convert nil into BigDecimal"); + case Qtrue: + if (!exc) return NULL; + rb_raise(rb_eTypeError, "can't convert true into BigDecimal"); + case Qfalse: + if (!exc) return NULL; + rb_raise(rb_eTypeError, "can't convert false into BigDecimal"); + default: + break; + } } switch (TYPE(iniValue)) { @@ -2595,11 +2636,17 @@ return pv; } if (mf > DBL_DIG+1) { + if (!exc) { + return NULL; + } rb_raise(rb_eArgError, "precision too large."); } /* fall through */ case T_RATIONAL: if (NIL_P(nFig)) { + if (!exc) { + return NULL; + } rb_raise(rb_eArgError, "can't omit precision for a %"PRIsVALUE".", RB_OBJ_CLASSNAME(iniValue)); @@ -2611,8 +2658,13 @@ default: break; } + /* TODO: support to_d */ + if (!exc) { + iniValue = rb_check_convert_type(iniValue, T_STRING, "String", "to_str"); + if (NIL_P(iniValue)) return NULL; + } StringValueCStr(iniValue); - return VpAlloc(mf, RSTRING_PTR(iniValue), 1); + return VpAlloc(mf, RSTRING_PTR(iniValue), 1, exc); } /* call-seq: @@ -2654,7 +2706,9 @@ VALUE obj; obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, 0); - GUARD_OBJ(pv, BigDecimal_new(argc, argv)); + pv = BigDecimal_new(argc, argv); + if (pv == NULL) return Qnil; + SAVE(pv); if (ToValue(pv)) pv = VpCopy(NULL, pv); RTYPEDDATA_DATA(obj) = pv; RB_OBJ_FREEZE(obj); @@ -3090,7 +3144,7 @@ VALUE obj; c_str = StringValueCStr(str); - GUARD_OBJ(pv, VpAlloc(0, c_str, 0)); + GUARD_OBJ(pv, VpAlloc(0, c_str, 0, 1)); obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, pv); RB_OBJ_FREEZE(obj); return obj; @@ -3908,8 +3962,8 @@ VpGetDoubleNegZero(); /* Allocates Vp constants. */ - VpConstOne = VpAlloc(1UL, "1", 1); - VpPt5 = VpAlloc(1UL, ".5", 1); + VpConstOne = VpAlloc(1UL, "1", 1, 1); + VpPt5 = VpAlloc(1UL, ".5", 1, 1); #ifdef BIGDECIMAL_DEBUG gnAlloc = 0; @@ -4033,7 +4087,7 @@ * NULL be returned if memory allocation is failed,or any error. */ VP_EXPORT Real * -VpAlloc(size_t mx, const char *szVal, int strict_p) +VpAlloc(size_t mx, const char *szVal, int strict_p, int exc) { const char *orig_szVal = szVal; size_t i, j, ni, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc; @@ -4232,7 +4286,9 @@ if (!strict_p) { goto return_zero; } - + if (!exc) { + return NULL; + } str = rb_str_new2(orig_szVal); rb_raise(rb_eArgError, "invalid value for BigDecimal(): \"%"PRIsVALUE"\"", str); } @@ -4809,7 +4865,7 @@ if (MxIndC < MxIndAB) { /* The Max. prec. of c < Prec(a)+Prec(b) */ w = c; - c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0", 1); + c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0", 1, 1); MxIndC = MxIndAB; } @@ -5977,8 +6033,8 @@ if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec; /* allocate temporally variables */ - f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1", 1); - r = VpAlloc((n + n) * (BASE_FIG + 2), "#1", 1); + f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1", 1, 1); + r = VpAlloc((n + n) * (BASE_FIG + 2), "#1", 1, 1); nr = 0; y_prec = y->MaxPrec; @@ -6430,8 +6486,8 @@ /* Allocate working variables */ - w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0", 1); - w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0", 1); + w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0", 1, 1); + w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0", 1, 1); /* calculation start */ VpAsgn(y, x, 1);
diff --git a/ext/bigdecimal/bigdecimal.h b/ext/bigdecimal/bigdecimal.h index b7b0abe..e3eae06 100644 --- a/ext/bigdecimal/bigdecimal.h +++ b/ext/bigdecimal/bigdecimal.h
@@ -308,7 +308,7 @@ VP_EXPORT void *VpMemAlloc(size_t mb); VP_EXPORT void *VpMemRealloc(void *ptr, size_t mb); VP_EXPORT void VpFree(Real *pv); -VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p); +VP_EXPORT Real *VpAlloc(size_t mx, const char *szVal, int strict_p, int exc); VP_EXPORT size_t VpAsgn(Real *c, Real *a, int isw); VP_EXPORT size_t VpAddSub(Real *c,Real *a,Real *b,int operation); VP_EXPORT size_t VpMult(Real *c,Real *a,Real *b);