summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-11 21:39:55 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-11 21:39:55 +0000
commit0c9a719d7728e026e2bd4dfef61d940d3ca7248f (patch)
treeabf593edd8848f8f9e18699b07a11aacb2389da8
parenta338c4ed5d519f05bf1f9fcd6a7b237a3101df0a (diff)
* 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
-rw-r--r--ChangeLog18
-rw-r--r--bignum.c13
-rw-r--r--ext/-test-/bignum/pack.c8
-rw-r--r--internal.h3
-rw-r--r--marshal.c4
-rw-r--r--pack.c2
-rw-r--r--random.c10
-rw-r--r--test/-ext-/bignum/test_pack.rb45
8 files changed, 62 insertions, 41 deletions
diff --git a/ChangeLog b/ChangeLog
index 3cddcfea7ab..55f838d873f 100644
--- a/ChangeLog
+++ b/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.
diff --git a/bignum.c b/bignum.c
index 0315cd5fc5d..c766daaf10d 100644
--- a/bignum.c
+++ b/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);
diff --git a/ext/-test-/bignum/pack.c b/ext/-test-/bignum/pack.c
index 2544a921768..553c101be66 100644
--- a/ext/-test-/bignum/pack.c
+++ b/ext/-test-/bignum/pack.c
@@ -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));
}
diff --git a/internal.h b/internal.h
index bcc6c58a311..46f0890899d 100644
--- a/internal.h
+++ b/internal.h
@@ -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 */
diff --git a/marshal.c b/marshal.c
index b4ba0f31200..be5068c360f 100644
--- a/marshal.c
+++ b/marshal.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);
diff --git a/pack.c b/pack.c
index debcef1ffd0..4d877b3a35c 100644
--- a/pack.c
+++ b/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;
}
diff --git a/random.c b/random.c
index 33fd1e94eda..4175e94b444 100644
--- a/random.c
+++ b/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);
diff --git a/test/-ext-/bignum/test_pack.rb b/test/-ext-/bignum/test_pack.rb
index 21abf093e5f..ea5dedd585a 100644
--- a/test/-ext-/bignum/test_pack.rb
+++ b/test/-ext-/bignum/test_pack.rb
@@ -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