mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Add exception:
keyword in Kernel#Integer()
Support `exception:` keyword argument in Kernel#Integer(). If `exception:` is `false`, `Kernel#Integer()` returns `nil` if the given value cannot be interpreted as an integer value. The default value of `exception:` is `true`. This is part of [Feature #12732]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62757 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4691daccc1
commit
2cfc5b03da
4 changed files with 104 additions and 31 deletions
13
bignum.c
13
bignum.c
|
@ -4230,7 +4230,7 @@ rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base)
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_str_to_inum(VALUE str, int base, int badcheck)
|
rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception)
|
||||||
{
|
{
|
||||||
VALUE ret;
|
VALUE ret;
|
||||||
const char *s;
|
const char *s;
|
||||||
|
@ -4242,12 +4242,21 @@ rb_str_to_inum(VALUE str, int base, int badcheck)
|
||||||
RSTRING_GETMEM(str, s, len);
|
RSTRING_GETMEM(str, s, len);
|
||||||
ret = rb_cstr_parse_inum(s, len, (badcheck ? NULL : &end), base);
|
ret = rb_cstr_parse_inum(s, len, (badcheck ? NULL : &end), base);
|
||||||
if (NIL_P(ret)) {
|
if (NIL_P(ret)) {
|
||||||
if (badcheck) invalid_integer(str);
|
if (badcheck) {
|
||||||
|
if (!raise_exception) return Qnil;
|
||||||
|
invalid_integer(str);
|
||||||
|
}
|
||||||
ret = INT2FIX(0);
|
ret = INT2FIX(0);
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_str_to_inum(VALUE str, int base, int badcheck)
|
||||||
|
{
|
||||||
|
return rb_str_convert_to_inum(str, base, badcheck, TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_str2big_poweroftwo(VALUE arg, int base, int badcheck)
|
rb_str2big_poweroftwo(VALUE arg, int base, int badcheck)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1101,6 +1101,7 @@ size_t rb_big_size(VALUE);
|
||||||
VALUE rb_integer_float_cmp(VALUE x, VALUE y);
|
VALUE rb_integer_float_cmp(VALUE x, VALUE y);
|
||||||
VALUE rb_integer_float_eq(VALUE x, VALUE y);
|
VALUE rb_integer_float_eq(VALUE x, VALUE y);
|
||||||
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
|
VALUE rb_cstr_parse_inum(const char *str, ssize_t len, char **endp, int base);
|
||||||
|
VALUE rb_str_convert_to_inum(VALUE str, int base, int badcheck, int raise_exception);
|
||||||
VALUE rb_big_comp(VALUE x);
|
VALUE rb_big_comp(VALUE x);
|
||||||
VALUE rb_big_aref(VALUE x, VALUE y);
|
VALUE rb_big_aref(VALUE x, VALUE y);
|
||||||
VALUE rb_big_abs(VALUE x);
|
VALUE rb_big_abs(VALUE x);
|
||||||
|
|
57
object.c
57
object.c
|
@ -3108,7 +3108,16 @@ rb_check_to_int(VALUE val)
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_convert_to_integer(VALUE val, int base)
|
rb_check_to_i(VALUE val)
|
||||||
|
{
|
||||||
|
if (RB_INTEGER_TYPE_P(val)) return val;
|
||||||
|
val = try_to_int(val, idTo_i, FALSE);
|
||||||
|
if (RB_INTEGER_TYPE_P(val)) return val;
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
rb_convert_to_integer(VALUE val, int base, int raise_exception)
|
||||||
{
|
{
|
||||||
VALUE tmp;
|
VALUE tmp;
|
||||||
|
|
||||||
|
@ -3124,24 +3133,32 @@ rb_convert_to_integer(VALUE val, int base)
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
else if (RB_TYPE_P(val, T_STRING)) {
|
else if (RB_TYPE_P(val, T_STRING)) {
|
||||||
return rb_str_to_inum(val, base, TRUE);
|
return rb_str_convert_to_inum(val, base, TRUE, raise_exception);
|
||||||
}
|
}
|
||||||
else if (NIL_P(val)) {
|
else if (NIL_P(val)) {
|
||||||
if (base != 0) goto arg_error;
|
if (base != 0) goto arg_error;
|
||||||
|
if (!raise_exception) return Qnil;
|
||||||
rb_raise(rb_eTypeError, "can't convert nil into Integer");
|
rb_raise(rb_eTypeError, "can't convert nil into Integer");
|
||||||
}
|
}
|
||||||
if (base != 0) {
|
if (base != 0) {
|
||||||
tmp = rb_check_string_type(val);
|
tmp = rb_check_string_type(val);
|
||||||
if (!NIL_P(tmp)) return rb_str_to_inum(tmp, base, TRUE);
|
if (!NIL_P(tmp)) return rb_str_convert_to_inum(tmp, base, TRUE, raise_exception);
|
||||||
arg_error:
|
arg_error:
|
||||||
|
if (!raise_exception) return Qnil;
|
||||||
rb_raise(rb_eArgError, "base specified for non string value");
|
rb_raise(rb_eArgError, "base specified for non string value");
|
||||||
}
|
}
|
||||||
tmp = convert_type_with_id(val, "Integer", idTo_int, FALSE, -1);
|
|
||||||
if (!RB_INTEGER_TYPE_P(tmp)) {
|
|
||||||
return rb_to_integer(val, "to_i", idTo_i);
|
|
||||||
}
|
|
||||||
return tmp;
|
|
||||||
|
|
||||||
|
tmp = rb_protect(rb_check_to_int, val, NULL);
|
||||||
|
if (RB_INTEGER_TYPE_P(tmp)) return tmp;
|
||||||
|
rb_set_errinfo(Qnil);
|
||||||
|
|
||||||
|
if (!raise_exception) {
|
||||||
|
VALUE result = rb_protect(rb_check_to_i, val, NULL);
|
||||||
|
rb_set_errinfo(Qnil);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rb_to_integer(val, "to_i", idTo_i);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3153,7 +3170,19 @@ rb_convert_to_integer(VALUE val, int base)
|
||||||
VALUE
|
VALUE
|
||||||
rb_Integer(VALUE val)
|
rb_Integer(VALUE val)
|
||||||
{
|
{
|
||||||
return rb_convert_to_integer(val, 0);
|
return rb_convert_to_integer(val, 0, TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -3183,20 +3212,20 @@ rb_Integer(VALUE val)
|
||||||
static VALUE
|
static VALUE
|
||||||
rb_f_integer(int argc, VALUE *argv, VALUE obj)
|
rb_f_integer(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE arg = Qnil;
|
VALUE arg = Qnil, opts = Qnil;
|
||||||
int base = 0;
|
int base = 0;
|
||||||
|
|
||||||
switch (argc) {
|
switch (rb_scan_args(argc, argv, "11:", NULL, NULL, &opts)) {
|
||||||
case 2:
|
case 2:
|
||||||
base = NUM2INT(argv[1]);
|
base = NUM2INT(argv[1]);
|
||||||
case 1:
|
case 1:
|
||||||
arg = argv[0];
|
arg = argv[0];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
/* should cause ArgumentError */
|
UNREACHABLE;
|
||||||
rb_scan_args(argc, argv, "11", NULL, NULL);
|
|
||||||
}
|
}
|
||||||
return rb_convert_to_integer(arg, base);
|
|
||||||
|
return rb_convert_to_integer(arg, base, opts_exception_p(opts));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
@ -127,6 +127,40 @@ class TestInteger < Test::Unit::TestCase
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_Integer_with_exception_keyword
|
||||||
|
assert_nothing_raised(ArgumentError) {
|
||||||
|
assert_equal(nil, Integer("1z", exception: false))
|
||||||
|
}
|
||||||
|
assert_nothing_raised(ArgumentError) {
|
||||||
|
assert_equal(nil, Integer(Object.new, exception: false))
|
||||||
|
}
|
||||||
|
assert_nothing_raised(ArgumentError) {
|
||||||
|
o = Object.new
|
||||||
|
def o.to_i; 42.5; end
|
||||||
|
assert_equal(nil, Integer(o, exception: false))
|
||||||
|
}
|
||||||
|
assert_nothing_raised(ArgumentError) {
|
||||||
|
o = Object.new
|
||||||
|
def o.to_i; raise; end
|
||||||
|
assert_equal(nil, Integer(o, exception: false))
|
||||||
|
}
|
||||||
|
assert_nothing_raised(ArgumentError) {
|
||||||
|
o = Object.new
|
||||||
|
def o.to_int; raise; end
|
||||||
|
assert_equal(nil, Integer(o, exception: false))
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_raise(ArgumentError) {
|
||||||
|
Integer("1z", exception: true)
|
||||||
|
}
|
||||||
|
assert_raise(TypeError) {
|
||||||
|
Integer(nil, exception: true)
|
||||||
|
}
|
||||||
|
assert_nothing_raised(TypeError) {
|
||||||
|
assert_equal(nil, Integer(nil, exception: false))
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
def test_int_p
|
def test_int_p
|
||||||
assert_not_predicate(1.0, :integer?)
|
assert_not_predicate(1.0, :integer?)
|
||||||
assert_predicate(1, :integer?)
|
assert_predicate(1, :integer?)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue