summaryrefslogtreecommitdiff
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
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
-rw-r--r--ChangeLog7
-rw-r--r--numeric.c55
-rw-r--r--ruby.h6
3 files changed, 60 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 57f8c76c69e..d1c950a4581 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Fri May 30 22:28:04 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
+
+ * numeric.c (rb_num2uint, rb_fix2int): new function to convert
+ values over INT_MAX. [ruby-core:01099]
+
+ * ruby.h (NUM2UINT, FIX2INT): ditto.
+
Fri May 30 14:55:44 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>
* eval.c (rb_Array): exclude Kernel#to_a instead of Object#to_a.
diff --git a/numeric.c b/numeric.c
index 322a1a1f71c..b8b8a35d0bd 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
diff --git a/ruby.h b/ruby.h
index 41d89beb893..76ea5d8b7c8 100644
--- a/ruby.h
+++ b/ruby.h
@@ -241,8 +241,10 @@ int rb_num2int _((VALUE));
#define NUM2INT(x) (FIXNUM_P(x)?FIX2INT(x):rb_num2int((VALUE)x))
int rb_fix2int _((VALUE));
#define FIX2INT(x) rb_fix2int((VALUE)x)
-#define NUM2UINT(x) ((unsigned int)NUM2INT(x))
-#define FIX2UINT(x) ((unsigned int)FIX2INT(x))
+unsigned int rb_num2uint _((VALUE));
+#define NUM2UINT(x) rb_num2uint(x)
+unsigned int rb_fix2uint _((VALUE));
+#define FIX2UINT(x) rb_fix2uint(x)
#else
#define NUM2INT(x) ((int)NUM2LONG(x))
#define NUM2UINT(x) ((unsigned int)NUM2ULONG(x))