summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-30 13:28:10 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-05-30 13:28:10 +0000
commit045eb9773e37dbe31c5ff595d380beb71ac62863 (patch)
tree1232d8d143fa078d57f8da0fcf53ec8dc06243e7 /numeric.c
parent7d9628ef0200936d1deb0ab866a006a94fe00d91 (diff)
* numeric.c (rb_num2uint, rb_fix2int): new function to convert
values over INT_MAX. [ruby-core:01099] * ruby.h (NUM2UINT, FIX2INT): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c55
1 files changed, 49 insertions, 6 deletions
diff --git a/numeric.c b/numeric.c
index 322a1a1..b8b8a35 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1000,15 +1000,40 @@ rb_num2ulong(val)
}
#if SIZEOF_INT < SIZEOF_LONG
+static void
+check_int(num)
+ long num;
+{
+ const char *s;
+
+ if (num < INT_MIN) {
+ s = "small";
+ }
+ else if (num > INT_MAX) {
+ s = "big";
+ }
+ else {
+ return;
+ }
+ rb_raise(rb_eRangeError, "integer %ld too %s to convert to `int'", num, s);
+}
+
+static void
+check_uint(num)
+ unsigned long num;
+{
+ if (num > INT_MAX) {
+ rb_raise(rb_eRangeError, "integer %lu too big to convert to `int'", num);
+ }
+}
+
int
rb_num2int(val)
VALUE val;
{
long num = rb_num2long(val);
- if (num < INT_MIN || INT_MAX < num) {
- rb_raise(rb_eRangeError, "integer %ld too big to convert to `int'", num);
- }
+ check_int(num);
return (int)num;
}
@@ -1018,9 +1043,27 @@ rb_fix2int(val)
{
long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);
- if (num < INT_MIN || INT_MAX < num) {
- rb_raise(rb_eRangeError, "integer %ld too big to convert to `int'", num);
- }
+ check_int(num);
+ return (int)num;
+}
+
+unsigned int
+rb_num2uint(val)
+ VALUE val;
+{
+ unsigned long num = rb_num2ulong(val);
+
+ check_uint(num);
+ return (int)num;
+}
+
+unsigned int
+rb_fix2int(val)
+ VALUE val;
+{
+ unsigned long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2ulong(val);
+
+ check_uint(num);
return (int)num;
}
#else