aboutsummaryrefslogtreecommitdiffstats
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-21 02:17:29 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-21 02:17:29 +0000
commit5a44e653fd4fef6a972d0785be6e9fe0bd242914 (patch)
tree7f17514beb7d5782b39505d958e198a3b08db8e5 /numeric.c
parent5e8c9c51a52b3ebfdd62fd7df4f0b93aecc9c38f (diff)
downloadruby-5a44e653fd4fef6a972d0785be6e9fe0bd242914.tar.gz
numeric.c: refine error message
* numeric.c (rb_num_get_rounding_option): refine error message at invalid rounding mode. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c22
1 files changed, 14 insertions, 8 deletions
diff --git a/numeric.c b/numeric.c
index 6707ec39f7..e82f0bd518 100644
--- a/numeric.c
+++ b/numeric.c
@@ -189,28 +189,34 @@ rb_num_get_rounding_option(VALUE opts)
{
static ID round_kwds[1];
VALUE rounding;
+ VALUE str;
const char *s;
- long l;
if (!NIL_P(opts)) {
if (!round_kwds[0]) {
round_kwds[0] = rb_intern_const("half");
}
if (!rb_get_kwargs(opts, round_kwds, 0, 1, &rounding)) goto noopt;
- if (SYMBOL_P(rounding)) rounding = rb_sym2str(rounding);
- s = StringValueCStr(rounding);
- l = RSTRING_LEN(rounding);
- switch (l) {
+ if (SYMBOL_P(rounding)) {
+ str = rb_sym2str(rounding);
+ }
+ else if (!RB_TYPE_P(str = rounding, T_STRING)) {
+ str = rb_check_string_type(rounding);
+ if (NIL_P(str)) goto invalid;
+ }
+ s = RSTRING_PTR(str);
+ switch (RSTRING_LEN(str)) {
case 2:
- if (strncasecmp(s, "up", 2) == 0)
+ if (rb_memcicmp(s, "up", 2) == 0)
return RUBY_NUM_ROUND_HALF_UP;
break;
case 4:
- if (strncasecmp(s, "even", 4) == 0)
+ if (rb_memcicmp(s, "even", 4) == 0)
return RUBY_NUM_ROUND_HALF_EVEN;
break;
}
- rb_raise(rb_eArgError, "unknown rounding mode: %"PRIsVALUE, rounding);
+ invalid:
+ rb_raise(rb_eArgError, "invalid rounding mode: % "PRIsVALUE, rounding);
}
noopt:
return RUBY_NUM_ROUND_DEFAULT;