ruby-changes:32530
From: nobu <ko1@a...>
Date: Wed, 15 Jan 2014 17:16:53 +0900 (JST)
Subject: [ruby-changes:32530] nobu:r44611 (trunk): numeric.c: FLOAT_OUT_OF_RANGE
nobu 2014-01-15 17:16:38 +0900 (Wed, 15 Jan 2014) New Revision: 44611 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44611 Log: numeric.c: FLOAT_OUT_OF_RANGE * numeric.c (FLOAT_OUT_OF_RANGE): extract a macro and a helper to raise RangeError of float. Modified files: trunk/numeric.c Index: numeric.c =================================================================== --- numeric.c (revision 44610) +++ numeric.c (revision 44611) @@ -1981,6 +1981,23 @@ num_step(int argc, VALUE *argv, VALUE fr https://github.com/ruby/ruby/blob/trunk/numeric.c#L1981 return from; } +static char * +out_of_range_float(char (*pbuf)[24], VALUE val) +{ + char *const buf = *pbuf; + char *s; + + snprintf(buf, sizeof(*pbuf), "%-.10g", RFLOAT_VALUE(val)); + if ((s = strchr(buf, ' ')) != 0) *s = '\0'; + return buf; +} + +#define FLOAT_OUT_OF_RANGE(val, type) do { \ + char buf[24]; \ + rb_raise(rb_eRangeError, "float %s out of range of "type, \ + out_of_range_float(&buf, (val))); \ +} while (0) + #define LONG_MIN_MINUS_ONE ((double)LONG_MIN-1) #define LONG_MAX_PLUS_ONE (2*(double)(LONG_MAX/2+1)) #define ULONG_MAX_PLUS_ONE (2*(double)(ULONG_MAX/2+1)) @@ -2005,12 +2022,7 @@ rb_num2long(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2022 return (long)RFLOAT_VALUE(val); } else { - char buf[24]; - char *s; - - snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); - if ((s = strchr(buf, ' ')) != 0) *s = '\0'; - rb_raise(rb_eRangeError, "float %s out of range of integer", buf); + FLOAT_OUT_OF_RANGE(val, "integer"); } } else if (RB_TYPE_P(val, T_BIGNUM)) { @@ -2047,12 +2059,7 @@ rb_num2ulong_internal(VALUE val, int *wr https://github.com/ruby/ruby/blob/trunk/numeric.c#L2059 return (unsigned long)(long)d; } else { - char buf[24]; - char *s; - - snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); - if ((s = strchr(buf, ' ')) != 0) *s = '\0'; - rb_raise(rb_eRangeError, "float %s out of range of integer", buf); + FLOAT_OUT_OF_RANGE(val, "integer"); } } else if (RB_TYPE_P(val, T_BIGNUM)) { @@ -2274,12 +2281,7 @@ rb_num2ll(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2281 return (LONG_LONG)(RFLOAT_VALUE(val)); } else { - char buf[24]; - char *s; - - snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); - if ((s = strchr(buf, ' ')) != 0) *s = '\0'; - rb_raise(rb_eRangeError, "float %s out of range of long long", buf); + FLOAT_OUT_OF_RANGE(val, "long long"); } } else if (RB_TYPE_P(val, T_BIGNUM)) { @@ -2313,12 +2315,7 @@ rb_num2ull(VALUE val) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2315 return (unsigned LONG_LONG)(LONG_LONG)(RFLOAT_VALUE(val)); } else { - char buf[24]; - char *s; - - snprintf(buf, sizeof(buf), "%-.10g", RFLOAT_VALUE(val)); - if ((s = strchr(buf, ' ')) != 0) *s = '\0'; - rb_raise(rb_eRangeError, "float %s out of range of unsigned long long", buf); + FLOAT_OUT_OF_RANGE(val, "unsigned long long"); } } else if (RB_TYPE_P(val, T_BIGNUM)) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/