Permalink
Browse files

Cleanup Bignum handling regarding string handling

This had duplicate logic, but Integer also already has string handling.
We want to have a single place for an optimized version of turning a
string into a bignum, so this ensures we have one place to do it.
  • Loading branch information...
1 parent 18c2577 commit 8e9e537c1876d7c343e77a72fa257577a2356182 @dbussink dbussink committed with brixen Nov 24, 2013
Showing with 6 additions and 108 deletions.
  1. +0 −75 vm/builtin/bignum.cpp
  2. +0 −2 vm/builtin/bignum.hpp
  3. +3 −0 vm/builtin/integer.hpp
  4. +1 −1 vm/builtin/system.cpp
  5. +1 −1 vm/marshal.cpp
  6. +1 −29 vm/test/test_bignum.hpp
View
@@ -1014,81 +1014,6 @@ namespace rubinius {
return str;
}
- Integer* Bignum::from_string_detect(STATE, const char *str) {
- const char *s;
- int radix;
- int sign;
- mp_int n;
- mp_init(&n);
-
- s = str;
- sign = 1;
-
- while(isspace(*s)) { s++; }
-
- if(*s == '+') {
- s++;
- } else if(*s == '-') {
- sign = 0;
- s++;
- }
-
- radix = 10;
- if(*s == '0') {
- switch(s[1]) {
- case 'x': case 'X':
- radix = 16; s += 2;
- break;
- case 'b': case 'B':
- radix = 2; s += 2;
- break;
- case 'o': case 'O':
- radix = 8; s += 2;
- break;
- case 'd': case 'D':
- radix = 10; s += 2;
- break;
- default:
- radix = 8; s += 1;
- }
- }
-
- mp_read_radix(XST, &n, s, radix);
-
- if(!sign) {
- n.sign = MP_NEG;
- }
-
- Integer* res = Bignum::from(state, &n);
-
- mp_clear(&n);
-
- return res;
- }
-
- Integer* Bignum::from_string(STATE, const char *str, size_t radix) {
- char *endptr = NULL;
- long l;
-
- errno = 0;
- l = strtol(str, &endptr, radix);
-
- if(endptr != str && errno == 0 &&
- l >= FIXNUM_MIN && l <= FIXNUM_MAX) {
- return Fixnum::from(l);
- }
-
- mp_int n;
- mp_init(&n);
- mp_read_radix(XST, &n, str, radix);
-
- Integer* res = Bignum::from(state, &n);
-
- mp_clear(&n);
-
- return res;
- }
-
void Bignum::into_string(STATE, size_t radix, char *buf, size_t sz) {
int k;
mp_toradix_nd(XST, mp_val(), buf, radix, sz, &k);
View
@@ -43,8 +43,6 @@ namespace rubinius {
bool even_p();
static Integer* normalize(STATE, Bignum* obj);
- static Integer* from_string_detect(STATE, const char* str);
- static Integer* from_string(STATE, const char* str, size_t radix);
static Integer* from_double(STATE, double d);
static Integer* from_array(STATE, uint32_t *ary, size_t sz);
View
@@ -34,6 +34,9 @@ namespace rubinius {
static Integer* from(STATE, unsigned long long i);
static Integer* from_cstr(STATE, const char* str, const char* end, int base, Object* strict);
+ static Integer* from_cppstr(STATE, std::string str, int base) {
+ return from_cstr(state, str.data(), str.data() + str.size(), base, cTrue);
+ }
unsigned int to_uint();
long to_long();
View
@@ -536,7 +536,7 @@ namespace rubinius {
if(!ent) return cNil;
if(ent->is_number()) {
- return Bignum::from_string(state, ent->value.c_str(), 10);
+ return Integer::from_cppstr(state, ent->value, 10);
} else if(ent->is_true()) {
return cTrue;
}
View
@@ -87,7 +87,7 @@ namespace rubinius {
std::string data;
stream >> data;
- return Bignum::from_string(state, data.c_str(), 16);
+ return Integer::from_cppstr(state, data, 16);
}
String* UnMarshaller::get_string() {
View
@@ -1066,42 +1066,14 @@ class TestBignum : public CxxTest::TestSuite, public VMTest {
TS_ASSERT_EQUALS(as<Integer>(s)->to_native(), -1);
}
- void test_from_string_detect() {
- Object* b = Bignum::from_string_detect(state, "0x47");
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), 0x47);
-
- b = Bignum::from_string_detect(state, "0b1000111");
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), 0x47);
-
- b = Bignum::from_string_detect(state, "+0o107");
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), 0x47);
-
- b = Bignum::from_string_detect(state, "-0d71");
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), -0x47);
-
- b = Bignum::from_string_detect(state, "0107");
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), 0x47);
- }
-
- void test_from_string() {
- Object* b = Bignum::from_string(state, "47", 10);
- TS_ASSERT(b->fixnum_p());
- TS_ASSERT_EQUALS(as<Integer>(b)->to_native(), 47);
- }
-
void test_to_s() {
/* Make sure we have a big enough bignum */
char* buf = new char[2048];
for(int i = 0; i < 2047; i++) {
buf[i] = '1';
}
buf[2047] = '\0';
- Bignum* b = as<Bignum>(Bignum::from_string(state, buf, 10));
+ Bignum* b = as<Bignum>(Integer::from_cppstr(state, std::string(buf), 10));
String* s = b->to_s(state, Fixnum::from(10));
TS_ASSERT_EQUALS(std::string(buf), s->c_str(state));

0 comments on commit 8e9e537

Please sign in to comment.