1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* bignum.c (rb_integer_pack): Renamed from rb_int_export.

(rb_integer_unpack): Renamed from rb_int_import.

* internal.h, pack.c: Follow the above change.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41147 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2013-06-07 12:41:02 +00:00
parent 07b72b8c85
commit a08b001100
11 changed files with 166 additions and 176 deletions

View file

@ -1,3 +1,10 @@
Fri Jun 7 21:39:39 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (rb_integer_pack): Renamed from rb_int_export.
(rb_integer_unpack): Renamed from rb_int_import.
* internal.h, pack.c: Follow the above change.
Fri Jun 7 21:05:26 2013 Tanaka Akira <akr@fsij.org>
* bignum.c (integer_format_loop_setup): Extracted from rb_int_export

View file

@ -667,7 +667,7 @@ int_export_take_lowbits(int n, BDIGIT_DBL *ddp, int *numbits_in_dd_p)
*/
void *
rb_int_export(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails)
rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails)
{
int sign;
BDIGIT *dp;
@ -846,7 +846,7 @@ int_import_push_bits(int data, int numbits, BDIGIT_DBL *ddp, int *numbits_in_dd_
* This function returns the imported integer as Fixnum or Bignum.
*/
VALUE
rb_int_import(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails)
rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails)
{
VALUE num_bits, num_bdigits;
VALUE result;

View file

@ -1,4 +1,3 @@
$(OBJS): $(HDRS) $(ruby_headers)
export.o: export.c $(top_srcdir)/internal.h
import.o: import.c $(top_srcdir)/internal.h
pack.o: pack.c $(top_srcdir)/internal.h

View file

@ -1,29 +0,0 @@
#include "ruby.h"
#include "internal.h"
static VALUE
rb_int_export_m(VALUE val, VALUE buf, VALUE wordorder, VALUE wordsize_arg, VALUE endian, VALUE nails)
{
int sign;
size_t count = 0;
void *ret;
size_t wordsize = NUM2SIZET(wordsize_arg);
if (!NIL_P(buf)) {
StringValue(buf);
rb_str_modify(buf);
count = RSTRING_LEN(buf) / wordsize;
}
ret = rb_int_export(val,
&sign, &count, NIL_P(buf) ? NULL : RSTRING_PTR(buf), count,
NUM2INT(wordorder), wordsize, NUM2INT(endian), NUM2INT(nails));
return rb_ary_new_from_args(3, INT2NUM(sign), ret ? rb_str_new(ret, wordsize * count) : Qnil, SIZET2NUM(count));
}
void
Init_export(VALUE klass)
{
rb_define_method(rb_cInteger, "test_export", rb_int_export_m, 5);
}

View file

@ -1,18 +0,0 @@
#include "ruby.h"
#include "internal.h"
static VALUE
rb_int_import_m(VALUE klass, VALUE sign, VALUE buf, VALUE wordcount, VALUE wordorder, VALUE wordsize, VALUE endian, VALUE nails)
{
StringValue(buf);
return rb_int_import(NUM2INT(sign), RSTRING_PTR(buf),
NUM2SIZET(wordcount), NUM2INT(wordorder), NUM2SIZET(wordsize),
NUM2INT(endian), NUM2SIZET(nails));
}
void
Init_import(VALUE klass)
{
rb_define_singleton_method(rb_cInteger, "test_import", rb_int_import_m, 7);
}

40
ext/-test-/bignum/pack.c Normal file
View file

@ -0,0 +1,40 @@
#include "ruby.h"
#include "internal.h"
static VALUE
rb_integer_pack_m(VALUE val, VALUE buf, VALUE wordorder, VALUE wordsize_arg, VALUE endian, VALUE nails)
{
int sign;
size_t count = 0;
void *ret;
size_t wordsize = NUM2SIZET(wordsize_arg);
if (!NIL_P(buf)) {
StringValue(buf);
rb_str_modify(buf);
count = RSTRING_LEN(buf) / wordsize;
}
ret = rb_integer_pack(val,
&sign, &count, NIL_P(buf) ? NULL : RSTRING_PTR(buf), count,
NUM2INT(wordorder), wordsize, NUM2INT(endian), NUM2INT(nails));
return rb_ary_new_from_args(3, INT2NUM(sign), ret ? rb_str_new(ret, wordsize * count) : Qnil, SIZET2NUM(count));
}
static VALUE
rb_integer_unpack_m(VALUE klass, VALUE sign, VALUE buf, VALUE wordcount, VALUE wordorder, VALUE wordsize, VALUE endian, VALUE nails)
{
StringValue(buf);
return rb_integer_unpack(NUM2INT(sign), RSTRING_PTR(buf),
NUM2SIZET(wordcount), NUM2INT(wordorder), NUM2SIZET(wordsize),
NUM2INT(endian), NUM2SIZET(nails));
}
void
Init_pack(VALUE klass)
{
rb_define_method(rb_cInteger, "test_pack", rb_integer_pack_m, 5);
rb_define_singleton_method(rb_cInteger, "test_unpack", rb_integer_unpack_m, 7);
}

View file

@ -428,8 +428,8 @@ const char *rb_objspace_data_type_name(VALUE obj);
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd);
/* bignum.c */
void *rb_int_export(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails);
VALUE rb_int_import(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails);
void *rb_integer_pack(VALUE val, int *signp, size_t *wordcount_allocated, void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails);
VALUE rb_integer_unpack(int sign, const void *words, size_t wordcount, int wordorder, size_t wordsize, int endian, size_t nails);
/* io.c */
void rb_maygvl_fd_fix_cloexec(int fd);

4
pack.c
View file

@ -1023,7 +1023,7 @@ pack_pack(VALUE ary, VALUE fmt)
numbytes = 1;
buf = rb_str_new(NULL, numbytes);
rb_int_export(from, &sign, NULL, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, 1, 1);
rb_integer_pack(from, &sign, NULL, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, 1, 1);
if (sign < 0)
rb_raise(rb_eArgError, "can't compress negative numbers");
@ -2142,7 +2142,7 @@ pack_unpack(VALUE str, VALUE fmt)
}
else {
s++;
UNPACK_PUSH(rb_int_import(1, s0, s-s0, 1, 1, 1, 1));
UNPACK_PUSH(rb_integer_unpack(1, s0, s-s0, 1, 1, 1, 1));
len--;
s0 = s;
}

View file

@ -1,67 +0,0 @@
# coding: ASCII-8BIT
require 'test/unit'
require "-test-/bignum"
class TestBignum < Test::Unit::TestCase
class TestExport < Test::Unit::TestCase
def test_export_zero
assert_equal([0, "", 0], 0.test_export(nil, 1, 1, 1, 0))
end
def test_argument_check
assert_raise(ArgumentError) { 0.test_export(nil, 0, 1, 1, 0) }
assert_raise(ArgumentError) { 0.test_export(nil, 1, 1, 2, 0) }
assert_raise(ArgumentError) { 0.test_export(nil, 1, 0, 1, 0) }
assert_raise(ArgumentError) { 0.test_export(nil, 1, 1, 1, 8) }
# assume sizeof(ssize_t) == sizeof(intptr_t)
assert_raise(ArgumentError) { 0.test_export(nil, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) }
end
def test_export_wordsize
assert_equal([1, "\x01", 1], 1.test_export(nil, 1, 1, 1, 0))
assert_equal([1, "\x00\x01", 1], 1.test_export(nil, 1, 2, 1, 0))
assert_equal([1, "\x00\x00\x01", 1], 1.test_export(nil, 1, 3, 1, 0))
assert_equal([1, "\x01", 1], 1.test_export(nil, 1, 1, -1, 0))
assert_equal([1, "\x01\x00", 1], 1.test_export(nil, 1, 2, -1, 0))
assert_equal([1, "\x01\x00\x00", 1], 1.test_export(nil, 1, 3, -1, 0))
end
def test_export_fixed_buffer
assert_equal([0, "\x00\x00", 2], 0.test_export("xx", 1, 1, 1, 0))
assert_equal([1, "\x00\x01", 2], 0x01.test_export("xx", 1, 1, 1, 0))
assert_equal([1, "\x02\x01", 2], 0x0201.test_export("xx", 1, 1, 1, 0))
assert_equal([2, "\x02\x01", 2], 0x030201.test_export("xx", 1, 1, 1, 0))
assert_equal([2, "\x02\x01", 2], 0x04030201.test_export("xx", 1, 1, 1, 0))
assert_equal([0, "\x00\x00", 2], 0.test_export("xx", -1, 1, 1, 0))
assert_equal([1, "\x01\x00", 2], 0x01.test_export("xx", -1, 1, 1, 0))
assert_equal([1, "\x01\x02", 2], 0x0201.test_export("xx", -1, 1, 1, 0))
assert_equal([2, "\x01\x02", 2], 0x030201.test_export("xx", -1, 1, 1, 0))
assert_equal([2, "\x01\x02", 2], 0x04030201.test_export("xx", -1, 1, 1, 0))
end
def test_export_wordorder_and_endian
assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_export(nil, 1, 2, 1, 0))
assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_export(nil, 1, 2, -1, 0))
assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_export(nil, -1, 2, 1, 0))
assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_export(nil, -1, 2, -1, 0))
end
def test_export_native_endian
assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_export(nil, 1, 2, 0, 0))
end
def test_export_nail
assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_export(nil, 1, 1, 1, 7))
assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_export(nil, 1, 1, 1, 4))
assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_export(nil, 1, 2, 1, 8))
end
def test_export_sign
assert_equal([-1, "\x01", 1], (-1).test_export(nil, 1, 1, 1, 0))
assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_export(nil, 1, 1, 1, 0))
end
end
end

View file

@ -1,54 +0,0 @@
# coding: ASCII-8BIT
require 'test/unit'
require "-test-/bignum"
class TestBignum < Test::Unit::TestCase
class TestImport < Test::Unit::TestCase
def test_import_zero
assert_equal(0, Integer.test_import(0, "", 1, 1, 1, 1, 0))
end
def test_argument_check
assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 0, 1, 1, 0) }
assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1, 2, 0) }
assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 0, 1, 0) }
assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1, 1, 8) }
# assume sizeof(ssize_t) == sizeof(intptr_t)
assert_raise(ArgumentError) { Integer.test_import(1, "x", 1, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) }
end
def test_import_wordsize
assert_equal(1, Integer.test_import(1, "\x01", 1, 1, 1, 1, 0))
assert_equal(1, Integer.test_import(1, "\x00\x01", 1, 1, 2, 1, 0))
assert_equal(1, Integer.test_import(1, "\x00\x00\x01", 1, 1, 3, 1, 0))
assert_equal(1, Integer.test_import(1, "\x01", 1, 1, 1, -1, 0))
assert_equal(1, Integer.test_import(1, "\x01\x00", 1, 1, 2, -1, 0))
assert_equal(1, Integer.test_import(1, "\x01\x00\x00", 1, 1, 3, -1, 0))
end
def test_import_wordorder_and_endian
assert_equal(0x01020304, Integer.test_import(1, "\x01\x02\x03\x04", 2, 1, 2, 1, 0))
assert_equal(0x02010403, Integer.test_import(1, "\x01\x02\x03\x04", 2, 1, 2, -1, 0))
assert_equal(0x03040102, Integer.test_import(1, "\x01\x02\x03\x04", 2, -1, 2, 1, 0))
assert_equal(0x04030201, Integer.test_import(1, "\x01\x02\x03\x04", 2, -1, 2, -1, 0))
end
def test_import_native_endian
assert_equal("\x12\x34".unpack("S!")[0], Integer.test_import(1, "\x12\x34", 1, 1, 2, 0, 0))
end
def test_import_nail
assert_equal(0b100011, Integer.test_import(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 1, 1, 7))
assert_equal(0x12345678, Integer.test_import(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 1, 1, 4))
assert_equal(0x12345678, Integer.test_import(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 1, 2, 1, 8))
end
def test_import_sign
assert_equal(-1, Integer.test_import(-1, "\x01", 1, 1, 1, 1, 0))
assert_equal(-0x8070605040302010, Integer.test_import(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 1, 1, 0))
end
end
end

View file

@ -0,0 +1,112 @@
# coding: ASCII-8BIT
require 'test/unit'
require "-test-/bignum"
class TestBignum < Test::Unit::TestCase
class TestPack < Test::Unit::TestCase
def test_pack_zero
assert_equal([0, "", 0], 0.test_pack(nil, 1, 1, 1, 0))
end
def test_argument_check
assert_raise(ArgumentError) { 0.test_pack(nil, 0, 1, 1, 0) }
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1, 2, 0) }
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 0, 1, 0) }
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1, 1, 8) }
# assume sizeof(ssize_t) == sizeof(intptr_t)
assert_raise(ArgumentError) { 0.test_pack(nil, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) }
end
def test_pack_wordsize
assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 1, 1, 0))
assert_equal([1, "\x00\x01", 1], 1.test_pack(nil, 1, 2, 1, 0))
assert_equal([1, "\x00\x00\x01", 1], 1.test_pack(nil, 1, 3, 1, 0))
assert_equal([1, "\x01", 1], 1.test_pack(nil, 1, 1, -1, 0))
assert_equal([1, "\x01\x00", 1], 1.test_pack(nil, 1, 2, -1, 0))
assert_equal([1, "\x01\x00\x00", 1], 1.test_pack(nil, 1, 3, -1, 0))
end
def test_pack_fixed_buffer
assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", 1, 1, 1, 0))
assert_equal([1, "\x00\x01", 2], 0x01.test_pack("xx", 1, 1, 1, 0))
assert_equal([1, "\x02\x01", 2], 0x0201.test_pack("xx", 1, 1, 1, 0))
assert_equal([2, "\x02\x01", 2], 0x030201.test_pack("xx", 1, 1, 1, 0))
assert_equal([2, "\x02\x01", 2], 0x04030201.test_pack("xx", 1, 1, 1, 0))
assert_equal([0, "\x00\x00", 2], 0.test_pack("xx", -1, 1, 1, 0))
assert_equal([1, "\x01\x00", 2], 0x01.test_pack("xx", -1, 1, 1, 0))
assert_equal([1, "\x01\x02", 2], 0x0201.test_pack("xx", -1, 1, 1, 0))
assert_equal([2, "\x01\x02", 2], 0x030201.test_pack("xx", -1, 1, 1, 0))
assert_equal([2, "\x01\x02", 2], 0x04030201.test_pack("xx", -1, 1, 1, 0))
end
def test_pack_wordorder_and_endian
assert_equal([1, "\x12\x34\x56\x78", 2], 0x12345678.test_pack(nil, 1, 2, 1, 0))
assert_equal([1, "\x34\x12\x78\x56", 2], 0x12345678.test_pack(nil, 1, 2, -1, 0))
assert_equal([1, "\x56\x78\x12\x34", 2], 0x12345678.test_pack(nil, -1, 2, 1, 0))
assert_equal([1, "\x78\x56\x34\x12", 2], 0x12345678.test_pack(nil, -1, 2, -1, 0))
end
def test_pack_native_endian
assert_equal([1, [0x1234].pack("S!"), 1], 0x1234.test_pack(nil, 1, 2, 0, 0))
end
def test_pack_nail
assert_equal([1, "\x01\x00\x00\x00\x01\x01", 6], 0b100011.test_pack(nil, 1, 1, 1, 7))
assert_equal([1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8], 0x12345678.test_pack(nil, 1, 1, 1, 4))
assert_equal([1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4], 0x12345678.test_pack(nil, 1, 2, 1, 8))
end
def test_pack_sign
assert_equal([-1, "\x01", 1], (-1).test_pack(nil, 1, 1, 1, 0))
assert_equal([-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8], (-0x8070605040302010).test_pack(nil, 1, 1, 1, 0))
end
def test_unpack_zero
assert_equal(0, Integer.test_unpack(0, "", 1, 1, 1, 1, 0))
end
def test_argument_check
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 0, 1, 1, 0) }
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1, 2, 0) }
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 0, 1, 0) }
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1, 1, 8) }
# assume sizeof(ssize_t) == sizeof(intptr_t)
assert_raise(ArgumentError) { Integer.test_unpack(1, "x", 1, 1, 1 << ([""].pack("p").length * 8 - 1), 1, 0) }
end
def test_unpack_wordsize
assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 1, 1, 0))
assert_equal(1, Integer.test_unpack(1, "\x00\x01", 1, 1, 2, 1, 0))
assert_equal(1, Integer.test_unpack(1, "\x00\x00\x01", 1, 1, 3, 1, 0))
assert_equal(1, Integer.test_unpack(1, "\x01", 1, 1, 1, -1, 0))
assert_equal(1, Integer.test_unpack(1, "\x01\x00", 1, 1, 2, -1, 0))
assert_equal(1, Integer.test_unpack(1, "\x01\x00\x00", 1, 1, 3, -1, 0))
end
def test_unpack_wordorder_and_endian
assert_equal(0x01020304, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 1, 2, 1, 0))
assert_equal(0x02010403, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, 1, 2, -1, 0))
assert_equal(0x03040102, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, -1, 2, 1, 0))
assert_equal(0x04030201, Integer.test_unpack(1, "\x01\x02\x03\x04", 2, -1, 2, -1, 0))
end
def test_unpack_native_endian
assert_equal("\x12\x34".unpack("S!")[0], Integer.test_unpack(1, "\x12\x34", 1, 1, 2, 0, 0))
end
def test_unpack_nail
assert_equal(0b100011, Integer.test_unpack(1, "\x01\x00\x00\x00\x01\x01", 6, 1, 1, 1, 7))
assert_equal(0x12345678, Integer.test_unpack(1, "\x01\x02\x03\x04\x05\x06\x07\x08", 8, 1, 1, 1, 4))
assert_equal(0x12345678, Integer.test_unpack(1, "\x00\x12\x00\x34\x00\x56\x00\x78", 4, 1, 2, 1, 8))
end
def test_unpack_sign
assert_equal(-1, Integer.test_unpack(-1, "\x01", 1, 1, 1, 1, 0))
assert_equal(-0x8070605040302010, Integer.test_unpack(-1, "\x80\x70\x60\x50\x40\x30\x20\x10", 8, 1, 1, 1, 0))
end
end
end