mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* internal.h (INTEGER_PACK_NEGATIVE): Defined.
(rb_integer_unpack): sign argument removed. * bignum.c (rb_integer_unpack): sign argument removed. Non-negative integers generated by default. INTEGER_PACK_NEGATIVE flag is used to generate non-positive integers. * pack.c (pack_unpack): Follow the above change. * random.c (int_pair_to_real_inclusive): Ditto. (make_seed_value): Ditto. (mt_state): Ditto. (limited_big_rand): Ditto. * marshal.c (r_object0): Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41239 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a338c4ed5d
commit
0c9a719d77
8 changed files with 62 additions and 41 deletions
18
ChangeLog
18
ChangeLog
|
@ -1,3 +1,21 @@
|
|||
Wed Jun 12 06:35:01 2013 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* internal.h (INTEGER_PACK_NEGATIVE): Defined.
|
||||
(rb_integer_unpack): sign argument removed.
|
||||
|
||||
* bignum.c (rb_integer_unpack): sign argument removed.
|
||||
Non-negative integers generated by default.
|
||||
INTEGER_PACK_NEGATIVE flag is used to generate non-positive integers.
|
||||
|
||||
* pack.c (pack_unpack): Follow the above change.
|
||||
|
||||
* random.c (int_pair_to_real_inclusive): Ditto.
|
||||
(make_seed_value): Ditto.
|
||||
(mt_state): Ditto.
|
||||
(limited_big_rand): Ditto.
|
||||
|
||||
* marshal.c (r_object0): Ditto.
|
||||
|
||||
Wed Jun 12 00:07:46 2013 Kouhei Sutou <kou@cozmixng.org>
|
||||
|
||||
* lib/xmlrpc/client.rb (XMLRPC::Client#parse_set_cookies): Extract.
|
||||
|
|
13
bignum.c
13
bignum.c
|
@ -1178,8 +1178,6 @@ integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in
|
|||
/*
|
||||
* Import an integer into a buffer.
|
||||
*
|
||||
* [sign] signedness of the value.
|
||||
* -1 for non-positive. 0 or 1 for non-negative.
|
||||
* [words] buffer to import.
|
||||
* [numwords] the size of given buffer as number of words.
|
||||
* [wordsize] the size of word as number of bytes.
|
||||
|
@ -1187,17 +1185,20 @@ integer_unpack_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in
|
|||
* Most significant nails bits of each word are ignored.
|
||||
* [flags] bitwise or of constants which name starts "INTEGER_PACK_".
|
||||
* It specifies word order and byte order.
|
||||
* Also, INTEGER_PACK_FORCE_BIGNUM specifies that the result will be a Bignum
|
||||
* even if it is representable as a Fixnum.
|
||||
* [INTEGER_PACK_FORCE_BIGNUM] the result will be a Bignum
|
||||
* even if it is representable as a Fixnum.
|
||||
* [INTEGER_PACK_NEGATIVE] Returns non-positive value.
|
||||
* (Returns non-negative value if not specified.)
|
||||
*
|
||||
* This function returns the imported integer as Fixnum or Bignum.
|
||||
*/
|
||||
VALUE
|
||||
rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
|
||||
rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
|
||||
{
|
||||
VALUE result;
|
||||
const unsigned char *buf = words;
|
||||
size_t num_bdigits;
|
||||
int sign = (flags & INTEGER_PACK_NEGATIVE) ? -1 : 1;
|
||||
|
||||
BDIGIT *dp;
|
||||
BDIGIT *de;
|
||||
|
@ -1215,8 +1216,6 @@ rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize,
|
|||
int numbits_in_dd;
|
||||
|
||||
validate_integer_pack_format(numwords, wordsize, nails, flags);
|
||||
if (sign != 1 && sign != 0 && sign != -1)
|
||||
rb_raise(rb_eArgError, "unexpected sign: %d", sign);
|
||||
|
||||
if (numwords <= (SIZE_MAX - (SIZEOF_BDIGITS*CHAR_BIT-1)) / CHAR_BIT / wordsize) {
|
||||
num_bdigits = integer_unpack_num_bdigits_small(numwords, wordsize, nails);
|
||||
|
|
|
@ -54,11 +54,11 @@ rb_integer_pack_2comp_m(VALUE val, VALUE numwords_arg, VALUE wordsize_arg, VALUE
|
|||
}
|
||||
|
||||
static VALUE
|
||||
rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
|
||||
rb_integer_unpack_m(VALUE klass, VALUE buf, VALUE numwords, VALUE wordsize, VALUE nails, VALUE flags)
|
||||
{
|
||||
StringValue(buf);
|
||||
|
||||
return rb_integer_unpack(NUM2INT(sign), RSTRING_PTR(buf),
|
||||
return rb_integer_unpack(RSTRING_PTR(buf),
|
||||
NUM2SIZET(numwords), NUM2SIZET(wordsize),
|
||||
NUM2SIZET(nails), NUM2INT(flags));
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ Init_pack(VALUE klass)
|
|||
rb_define_method(rb_cInteger, "test_pack_raw", rb_integer_pack_raw_m, 5);
|
||||
rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 4);
|
||||
rb_define_method(rb_cInteger, "test_pack_2comp", rb_integer_pack_2comp_m, 4);
|
||||
rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 6);
|
||||
rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 5);
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_MSWORD_FIRST", INT2NUM(INTEGER_PACK_MSWORD_FIRST));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_LSWORD_FIRST", INT2NUM(INTEGER_PACK_LSWORD_FIRST));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_MSBYTE_FIRST", INT2NUM(INTEGER_PACK_MSBYTE_FIRST));
|
||||
|
@ -77,4 +77,6 @@ Init_pack(VALUE klass)
|
|||
rb_define_const(rb_cInteger, "INTEGER_PACK_NATIVE_BYTE_ORDER", INT2NUM(INTEGER_PACK_NATIVE_BYTE_ORDER));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_LITTLE_ENDIAN", INT2NUM(INTEGER_PACK_LITTLE_ENDIAN));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_BIG_ENDIAN", INT2NUM(INTEGER_PACK_BIG_ENDIAN));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_FORCE_BIGNUM", INT2NUM(INTEGER_PACK_FORCE_BIGNUM));
|
||||
rb_define_const(rb_cInteger, "INTEGER_PACK_NEGATIVE", INT2NUM(INTEGER_PACK_NEGATIVE));
|
||||
}
|
||||
|
|
|
@ -63,6 +63,7 @@ extern "C" {
|
|||
#define INTEGER_PACK_NATIVE_BYTE_ORDER 0x40
|
||||
/* For rb_integer_unpack: */
|
||||
#define INTEGER_PACK_FORCE_BIGNUM 0x100
|
||||
#define INTEGER_PACK_NEGATIVE 0x200
|
||||
/* Combinations: */
|
||||
#define INTEGER_PACK_LITTLE_ENDIAN \
|
||||
(INTEGER_PACK_LSWORD_FIRST | \
|
||||
|
@ -448,7 +449,7 @@ VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, in
|
|||
|
||||
/* bignum.c */
|
||||
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
|
||||
VALUE rb_integer_unpack(int sign, const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
|
||||
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
|
||||
int rb_integer_pack_2comp(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags);
|
||||
|
||||
/* io.c */
|
||||
|
|
|
@ -1616,8 +1616,8 @@ r_object0(struct load_arg *arg, int *ivp, VALUE extmod)
|
|||
sign = r_byte(arg);
|
||||
len = r_long(arg);
|
||||
data = r_bytes0(len * 2, arg);
|
||||
v = rb_integer_unpack(sign == '-' ? -1 : +1, RSTRING_PTR(data), len, 2, 0,
|
||||
INTEGER_PACK_LITTLE_ENDIAN);
|
||||
v = rb_integer_unpack(RSTRING_PTR(data), len, 2, 0,
|
||||
INTEGER_PACK_LITTLE_ENDIAN | (sign == '-' ? INTEGER_PACK_NEGATIVE : 0));
|
||||
rb_str_resize(data, 0L);
|
||||
v = r_entry(v, arg);
|
||||
v = r_leave(v, arg);
|
||||
|
|
2
pack.c
2
pack.c
|
@ -2142,7 +2142,7 @@ pack_unpack(VALUE str, VALUE fmt)
|
|||
}
|
||||
else {
|
||||
s++;
|
||||
UNPACK_PUSH(rb_integer_unpack(1, s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
|
||||
UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
|
||||
len--;
|
||||
s0 = s;
|
||||
}
|
||||
|
|
10
random.c
10
random.c
|
@ -274,14 +274,14 @@ int_pair_to_real_inclusive(uint32_t a, uint32_t b)
|
|||
/* (a << 32) | b */
|
||||
xary[0] = a;
|
||||
xary[1] = b;
|
||||
x = rb_integer_unpack(+1, xary, 2, sizeof(uint32_t), 0,
|
||||
x = rb_integer_unpack(xary, 2, sizeof(uint32_t), 0,
|
||||
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER|
|
||||
INTEGER_PACK_FORCE_BIGNUM);
|
||||
|
||||
/* (1 << 53) | 1 */
|
||||
mary[0] = 0x00200000;
|
||||
mary[1] = 0x00000001;
|
||||
m = rb_integer_unpack(+1, mary, 2, sizeof(uint32_t), 0,
|
||||
m = rb_integer_unpack(mary, 2, sizeof(uint32_t), 0,
|
||||
INTEGER_PACK_MSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER|
|
||||
INTEGER_PACK_FORCE_BIGNUM);
|
||||
|
||||
|
@ -508,7 +508,7 @@ make_seed_value(const uint32_t *ptr)
|
|||
len = DEFAULT_SEED_CNT;
|
||||
}
|
||||
|
||||
seed = rb_integer_unpack(+1, ptr, len, sizeof(uint32_t), 0,
|
||||
seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0,
|
||||
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||
|
||||
return seed;
|
||||
|
@ -571,7 +571,7 @@ random_copy(VALUE obj, VALUE orig)
|
|||
static VALUE
|
||||
mt_state(const struct MT *mt)
|
||||
{
|
||||
return rb_integer_unpack(1, mt->state, numberof(mt->state),
|
||||
return rb_integer_unpack(mt->state, numberof(mt->state),
|
||||
sizeof(*mt->state), 0,
|
||||
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||
}
|
||||
|
@ -778,7 +778,7 @@ limited_big_rand(struct MT *mt, VALUE limit)
|
|||
}
|
||||
rnd_array[i] = rnd;
|
||||
}
|
||||
val = rb_integer_unpack(+1, rnd_array, len, sizeof(uint32_t), 0,
|
||||
val = rb_integer_unpack(rnd_array, len, sizeof(uint32_t), 0,
|
||||
INTEGER_PACK_LSWORD_FIRST|INTEGER_PACK_NATIVE_BYTE_ORDER);
|
||||
ALLOCV_END(vtmp);
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ class TestBignum < Test::Unit::TestCase
|
|||
NATIVE_BYTE_ORDER = Integer::INTEGER_PACK_NATIVE_BYTE_ORDER
|
||||
LITTLE_ENDIAN = Integer::INTEGER_PACK_LITTLE_ENDIAN
|
||||
BIG_ENDIAN = Integer::INTEGER_PACK_BIG_ENDIAN
|
||||
NEGATIVE = Integer::INTEGER_PACK_NEGATIVE
|
||||
|
||||
def test_pack_zero
|
||||
assert_equal([0, ""], 0.test_pack(0, 1, 0, BIG_ENDIAN))
|
||||
|
@ -117,48 +118,48 @@ class TestBignum < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_unpack_zero
|
||||
assert_equal(0, Integer.test_unpack(0, "", 0, 1, 0, BIG_ENDIAN))
|
||||
assert_equal(0, Integer.test_unpack("", 0, 1, 0, BIG_ENDIAN))
|
||||
end
|
||||
|
||||
def test_unpack_argument_check
|
||||
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 2, 1, 0, MSBYTE_FIRST) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, MSWORD_FIRST) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 0, 0, BIG_ENDIAN) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 8, BIG_ENDIAN) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack("x", 2, 1, 0, MSBYTE_FIRST) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 0, MSWORD_FIRST) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 0, 0, BIG_ENDIAN) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1, 8, BIG_ENDIAN) }
|
||||
|
||||
# assume sizeof(ssize_t) == sizeof(intptr_t)
|
||||
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
|
||||
assert_raise(ArgumentError) { Integer.test_unpack("x", 1, 1 << ([""].pack("p").length * 8 - 1), 0, BIG_ENDIAN) }
|
||||
end
|
||||
|
||||
def test_unpack_wordsize
|
||||
assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack(1, "\x00\x01", 1, 2, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack(1, "\x00\x00\x01", 1, 3, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 0, LITTLE_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack(1, "\x01\x00", 1, 2, 0, LITTLE_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack(1, "\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x00\x01", 1, 2, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x00\x00\x01", 1, 3, 0, BIG_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x01", 1, 1, 0, LITTLE_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x01\x00", 1, 2, 0, LITTLE_ENDIAN))
|
||||
assert_equal(1, Integer.test_unpack("\x01\x00\x00", 1, 3, 0, LITTLE_ENDIAN))
|
||||
end
|
||||
|
||||
def test_unpack_wordorder_and_endian
|
||||
assert_equal(0x01020304, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
|
||||
assert_equal(0x02010403, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
|
||||
assert_equal(0x03040102, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
|
||||
assert_equal(0x04030201, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
|
||||
assert_equal(0x01020304, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|MSBYTE_FIRST))
|
||||
assert_equal(0x02010403, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, MSWORD_FIRST|LSBYTE_FIRST))
|
||||
assert_equal(0x03040102, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|MSBYTE_FIRST))
|
||||
assert_equal(0x04030201, Integer.test_unpack("\x01\x02\x03\x04", 2, 2, 0, LSWORD_FIRST|LSBYTE_FIRST))
|
||||
end
|
||||
|
||||
def test_unpack_native_endian
|
||||
assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack(1, "\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
|
||||
assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack("\x12\x34", 1, 2, 0, MSWORD_FIRST|NATIVE_BYTE_ORDER))
|
||||
end
|
||||
|
||||
def test_unpack_nail
|
||||
assert_equal(0b100011, Integer.test_unpack(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN))
|
||||
assert_equal(0x12345678, Integer.test_unpack(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN))
|
||||
assert_equal(0x12345678, Integer.test_unpack(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN))
|
||||
assert_equal(0b100011, Integer.test_unpack("\x01\x00\x00\x00\x01\x01", 6, 1, 7, BIG_ENDIAN))
|
||||
assert_equal(0x12345678, Integer.test_unpack("\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 4, BIG_ENDIAN))
|
||||
assert_equal(0x12345678, Integer.test_unpack("\x00\x12\x00\x34\x00\x56\x00\x78", 4, 2, 8, BIG_ENDIAN))
|
||||
end
|
||||
|
||||
def test_unpack_sign
|
||||
assert_equal(-1, Integer.test_unpack(-1, "\x01", 1, 1, 0, BIG_ENDIAN))
|
||||
assert_equal(-0x8070605040302010, Integer.test_unpack(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN))
|
||||
assert_equal(-1, Integer.test_unpack("\x01", 1, 1, 0, BIG_ENDIAN|NEGATIVE))
|
||||
assert_equal(-0x8070605040302010, Integer.test_unpack("\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 0, BIG_ENDIAN|NEGATIVE))
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue