Skip to content

Commit

Permalink
* include/ruby/ruby.h: add NUM2SHORT(), NUM2USHORT() macros.
Browse files Browse the repository at this point in the history
* numeric.c: ditto.

* test/-ext-/num2int/test_num2int.rb: add testcases for NUM2SHORT().
* ext/-test-/num2int/num2int.c: ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33743 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
kosaki committed Nov 14, 2011
1 parent d3437b7 commit b2a2ba1
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Sun Nov 13 10:28:18 2011 KOSAKI Motohiro <[email protected]>

* include/ruby/ruby.h: add NUM2SHORT(), NUM2USHORT() macros.
* numeric.c: ditto.

* test/-ext-/num2int/test_num2int.rb: add testcases for NUM2SHORT().
* ext/-test-/num2int/num2int.c: ditto.

Sun Nov 13 10:23:48 2011 KOSAKI Motohiro <[email protected]>

* bignum.c (rb_big2ull): fix off-by-twice bug of NUM2ULL.
Expand Down
25 changes: 25 additions & 0 deletions ext/-test-/num2int/num2int.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

extern VALUE rb_stdout;

static VALUE
print_num2short(VALUE obj, VALUE num)
{
char buf[128];
VALUE str;

sprintf(buf, "%d", NUM2SHORT(num));
str = rb_str_new_cstr(buf);
rb_io_write(rb_stdout, str);
}

static VALUE
print_num2ushort(VALUE obj, VALUE num)
{
char buf[128];
VALUE str;

sprintf(buf, "%u", NUM2USHORT(num));
str = rb_str_new_cstr(buf);
rb_io_write(rb_stdout, str);
}

static VALUE
print_num2int(VALUE obj, VALUE num)
{
Expand Down Expand Up @@ -74,6 +96,9 @@ Init_num2int(void)
{
VALUE cNum2int = rb_path2class("TestNum2int::Num2int");

rb_define_singleton_method(cNum2int, "print_num2short", print_num2short, 1);
rb_define_singleton_method(cNum2int, "print_num2ushort", print_num2ushort, 1);

rb_define_singleton_method(cNum2int, "print_num2int", print_num2int, 1);
rb_define_singleton_method(cNum2int, "print_num2uint", print_num2uint, 1);

Expand Down
19 changes: 19 additions & 0 deletions include/ruby/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,25 @@ unsigned long rb_fix2uint(VALUE);
#define FIX2UINT(x) ((unsigned int)FIX2ULONG(x))
#endif

short rb_num2short(VALUE);
unsigned short rb_num2ushort(VALUE);
short rb_fix2short(VALUE);
unsigned short rb_fix2ushort(VALUE);
#define FIX2SHORT(x) (rb_fix2short((VALUE)(x)))
#define NUM2SHORT_internal(x) (FIXNUM_P(x) ? FIX2SHORT(x) : rb_num2short(x))
#ifdef __GNUC__
# define NUM2SHORT(x) \
__extension__ ({VALUE num2short_x = (x); NUM2SHORT_internal(num2short_x);})
#else /* __GNUC__ */
static inline short
NUM2SHORT(VALUE x)
{
return NUM2SHORT_internal(x);
}
#endif /* __GNUC__ */
#define NUM2USHORT(x) rb_num2ushort((VALUE)(x))


#ifdef HAVE_LONG_LONG
LONG_LONG rb_num2ll(VALUE);
unsigned LONG_LONG rb_num2ull(VALUE);
Expand Down
74 changes: 74 additions & 0 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -1953,6 +1953,80 @@ rb_fix2int(VALUE val)
}
#endif

void
rb_out_of_short(SIGNED_VALUE num)
{
rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too %s to convert to `short'",
num, num < 0 ? "small" : "big");
}

static void
check_short(SIGNED_VALUE num)
{
if ((SIGNED_VALUE)(short)num != num) {
rb_out_of_short(num);
}
}

static void
check_ushort(VALUE num, VALUE sign)
{
static const VALUE mask = ~(VALUE)USHRT_MAX;

if (RTEST(sign)) {
/* minus */
if ((num & mask) != mask || (num & ~mask) <= SHRT_MAX)
#define VALUE_MSBMASK ((VALUE)1 << ((sizeof(VALUE) * CHAR_BIT) - 1))
rb_raise(rb_eRangeError, "integer %"PRIdVALUE " too small to convert to `unsigned short'", num|VALUE_MSBMASK);
}
else {
/* plus */
if ((num & mask) != 0)
rb_raise(rb_eRangeError, "integer %"PRIuVALUE " too big to convert to `unsigned short'", num);
}
}

short
rb_num2short(VALUE val)
{
long num = rb_num2long(val);

check_short(num);
return num;
}

short
rb_fix2short(VALUE val)
{
long num = FIXNUM_P(val)?FIX2LONG(val):rb_num2long(val);

check_short(num);
return num;
}

unsigned short
rb_num2ushort(VALUE val)
{
VALUE num = rb_num2ulong(val);

check_ushort(num, rb_funcall(val, '<', 1, INT2FIX(0)));
return (unsigned long)num;
}

unsigned short
rb_fix2ushort(VALUE val)
{
unsigned long num;

if (!FIXNUM_P(val)) {
return rb_num2uint(val);
}
num = FIX2ULONG(val);

check_ushort(num, rb_funcall(val, '<', 1, INT2FIX(0)));
return num;
}

VALUE
rb_num2fix(VALUE val)
{
Expand Down
40 changes: 40 additions & 0 deletions test/-ext-/num2int/test_num2int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module Num2int
end
require '-test-/num2int/num2int'

SHRT_MIN = -32768
SHRT_MAX = 32767
USHRT_MAX = 65535

INT_MIN = -2147483648
INT_MAX = 2147483647
UINT_MAX = 4294967295
Expand All @@ -29,6 +33,42 @@ module Num2int
FIXNUM_MAX = LONG_MAX/2
FIXNUM_MIN = LONG_MIN/2

def test_num2short
assert_output(SHRT_MIN.to_s) do
Num2int.print_num2short(SHRT_MIN)
end
assert_output(SHRT_MAX.to_s) do
Num2int.print_num2short(SHRT_MAX)
end
assert_raise(RangeError) do
Num2int.print_num2short(SHRT_MIN-1)
end
assert_raise(RangeError) do
Num2int.print_num2short(SHRT_MAX+1)
end
end

def test_num2ushort
assert_output("0") do
Num2int.print_num2ushort(0)
end
assert_output(USHRT_MAX.to_s) do
Num2int.print_num2ushort(USHRT_MAX)
end
assert_output(USHRT_MAX.to_s) do
Num2int.print_num2ushort(-1)
end
assert_output((SHRT_MAX+1).to_s) do
Num2int.print_num2ushort(SHRT_MIN)
end
assert_raise(RangeError) do
Num2int.print_num2ushort(SHRT_MIN-1)
end
assert_raise(RangeError) do
Num2int.print_num2ushort(USHRT_MAX+1)
end
end

def test_num2int
assert_output(INT_MIN.to_s) do
Num2int.print_num2int(INT_MIN)
Expand Down

0 comments on commit b2a2ba1

Please sign in to comment.