|
10 | 10 | extern crate libc;
|
11 | 11 |
|
12 | 12 | use libc::{c_char, c_double, c_int, c_long, c_ulong, c_void, size_t};
|
13 |
| -use std::num::{One, Zero, ToStrRadix}; |
| 13 | +use std::num::{One, Zero}; |
14 | 14 | use std::mem::{uninitialized,size_of};
|
15 | 15 | use std::{cmp, fmt};
|
16 | 16 | use std::from_str::FromStr;
|
@@ -181,6 +181,29 @@ impl Mpz {
|
181 | 181 | }
|
182 | 182 | }
|
183 | 183 |
|
| 184 | + // TODO: fail on an invalid base |
| 185 | + // FIXME: Unfortunately it isn't currently possible to use the fmt::RadixFmt |
| 186 | + // machinery for a custom type. |
| 187 | + fn to_str_radix(&self, base: uint) -> String { |
| 188 | + unsafe { |
| 189 | + // Extra two bytes are for possible minus sign and null terminator |
| 190 | + let len = __gmpz_sizeinbase(&self.mpz, base as c_int) as uint + 2; |
| 191 | + |
| 192 | + // Allocate and write into a raw *c_char of the correct length |
| 193 | + let mut vector: Vec<u8> = Vec::with_capacity(len); |
| 194 | + vector.set_len(len); |
| 195 | + |
| 196 | + let mut cstr = vector.as_slice().to_c_str_unchecked(); |
| 197 | + |
| 198 | + __gmpz_get_str(cstr.as_mut_ptr(), base as c_int, &self.mpz); |
| 199 | + |
| 200 | + match cstr.as_str() { |
| 201 | + Some(slice) => slice.to_string(), |
| 202 | + None => fail!("GMP returned invalid UTF-8!") |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + |
184 | 207 | pub fn from_str_radix(s: &str, base: uint) -> Option<Mpz> {
|
185 | 208 | unsafe {
|
186 | 209 | assert!(base == 0 || (base >= 2 && base <= 62));
|
@@ -540,29 +563,6 @@ impl FromStr for Mpz {
|
540 | 563 | }
|
541 | 564 | }
|
542 | 565 |
|
543 |
| -impl ToStrRadix for Mpz { |
544 |
| - // TODO: fail on an invalid base |
545 |
| - fn to_str_radix(&self, base: uint) -> String { |
546 |
| - unsafe { |
547 |
| - // Extra two bytes are for possible minus sign and null terminator |
548 |
| - let len = __gmpz_sizeinbase(&self.mpz, base as c_int) as uint + 2; |
549 |
| - |
550 |
| - // Allocate and write into a raw *c_char of the correct length |
551 |
| - let mut vector: Vec<u8> = Vec::with_capacity(len); |
552 |
| - vector.set_len(len); |
553 |
| - |
554 |
| - let mut cstr = vector.as_slice().to_c_str_unchecked(); |
555 |
| - |
556 |
| - __gmpz_get_str(cstr.as_mut_ptr(), base as c_int, &self.mpz); |
557 |
| - |
558 |
| - match cstr.as_str() { |
559 |
| - Some(slice) => slice.to_string(), |
560 |
| - None => fail!("GMP returned invalid UTF-8!") |
561 |
| - } |
562 |
| - } |
563 |
| - } |
564 |
| -} |
565 |
| - |
566 | 566 | impl fmt::Show for Mpz {
|
567 | 567 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
568 | 568 | write!(f, "{}", self.to_str_radix(10))
|
|
0 commit comments