Skip to content

Commit b27db91

Browse files
committed
Merge pull request #22 from gifnksm/master
bigint: Remove `parse_bytes` method
2 parents c0cf374 + d3fd33a commit b27db91

File tree

4 files changed

+62
-62
lines changed

4 files changed

+62
-62
lines changed

src/bigint.rs

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use std::num::{ToPrimitive, FromPrimitive};
6767
use std::num::{Zero, One, FromStrRadix};
6868
use std::str;
6969
use std::string::String;
70-
use std::{uint, i64, u64};
70+
use std::{i64, u64};
7171

7272
/// A `BigDigit` is a `BigUint`'s composing element.
7373
pub type BigDigit = u32;
@@ -294,8 +294,8 @@ impl Mul<BigUint, BigUint> for BigUint {
294294
if self.is_zero() || other.is_zero() { return Zero::zero(); }
295295

296296
let (s_len, o_len) = (self.data.len(), other.data.len());
297-
if s_len == 1 { return mul_digit(other, self.data.as_slice()[0]); }
298-
if o_len == 1 { return mul_digit(self, other.data.as_slice()[0]); }
297+
if s_len == 1 { return mul_digit(other, self.data[0]); }
298+
if o_len == 1 { return mul_digit(self, other.data[0]); }
299299

300300
// Using Karatsuba multiplication
301301
// (a1 * base + a0) * (b1 * base + b0)
@@ -340,8 +340,8 @@ impl Mul<BigUint, BigUint> for BigUint {
340340
#[inline]
341341
fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) {
342342
let mid = cmp::min(a.data.len(), n);
343-
return (BigUint::from_slice(a.data.slice(mid, a.data.len())),
344-
BigUint::from_slice(a.data.slice(0, mid)));
343+
(BigUint::from_slice(a.data[mid ..]),
344+
BigUint::from_slice(a.data[.. mid]))
345345
}
346346

347347
#[inline]
@@ -488,7 +488,7 @@ impl Integer for BigUint {
488488
return (Zero::zero(), Zero::zero(), (*a).clone());
489489
}
490490

491-
let an = a.data.slice_from(a.data.len() - n);
491+
let an = a.data[a.data.len() - n ..];
492492
let bn = *b.data.last().unwrap();
493493
let mut d = Vec::with_capacity(an.len());
494494
let mut carry = 0;
@@ -545,7 +545,7 @@ impl Integer for BigUint {
545545
#[inline]
546546
fn is_even(&self) -> bool {
547547
// Considering only the last digit.
548-
match self.data.as_slice().head() {
548+
match self.data.head() {
549549
Some(x) => x.is_even(),
550550
None => true
551551
}
@@ -574,8 +574,8 @@ impl ToPrimitive for BigUint {
574574
fn to_u64(&self) -> Option<u64> {
575575
match self.data.len() {
576576
0 => Some(0),
577-
1 => Some(self.data.as_slice()[0] as u64),
578-
2 => Some(BigDigit::to_doublebigdigit(self.data.as_slice()[1], self.data.as_slice()[0])
577+
1 => Some(self.data[0] as u64),
578+
2 => Some(BigDigit::to_doublebigdigit(self.data[1], self.data[0])
579579
as u64),
580580
_ => None
581581
}
@@ -658,9 +658,9 @@ fn to_str_radix(me: &BigUint, radix: uint) -> String {
658658
assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]");
659659
let (base, max_len) = get_radix_base(radix);
660660
if base == BigDigit::base {
661-
return fill_concat(me.data.as_slice(), radix, max_len)
661+
return fill_concat(me.data[], radix, max_len)
662662
}
663-
return fill_concat(convert_base(me, base).as_slice(), radix, max_len);
663+
return fill_concat(convert_base(me, base)[], radix, max_len);
664664

665665
fn convert_base(n: &BigUint, base: DoubleBigDigit) -> Vec<BigDigit> {
666666
let divider = base.to_biguint().unwrap();
@@ -684,10 +684,10 @@ fn to_str_radix(me: &BigUint, radix: uint) -> String {
684684
let mut s = String::with_capacity(v.len() * l);
685685
for n in v.iter().rev() {
686686
let ss = fmt::radix(*n as uint, radix as u8).to_string();
687-
s.push_str("0".repeat(l - ss.len()).as_slice());
688-
s.push_str(ss.as_slice());
687+
s.push_str("0".repeat(l - ss.len())[]);
688+
s.push_str(ss[]);
689689
}
690-
s.as_slice().trim_left_chars('0').to_string()
690+
s.trim_left_chars('0').to_string()
691691
}
692692
}
693693

@@ -703,46 +703,18 @@ impl FromStrRadix for BigUint {
703703
/// Creates and initializes a `BigUint`.
704704
#[inline]
705705
fn from_str_radix(s: &str, radix: uint) -> Option<BigUint> {
706-
BigUint::parse_bytes(s.as_bytes(), radix)
707-
}
708-
}
709-
710-
impl BigUint {
711-
/// Creates and initializes a `BigUint`.
712-
///
713-
/// The digits are be in base 2^32.
714-
#[inline]
715-
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
716-
// omit trailing zeros
717-
let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
718-
digits.truncate(new_len);
719-
BigUint { data: digits }
720-
}
721-
722-
/// Creates and initializes a `BigUint`.
723-
///
724-
/// The digits are be in base 2^32.
725-
#[inline]
726-
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
727-
BigUint::new(slice.to_vec())
728-
}
729-
730-
/// Creates and initializes a `BigUint`.
731-
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigUint> {
732706
let (base, unit_len) = get_radix_base(radix);
733707
let base_num = match base.to_biguint() {
734708
Some(base_num) => base_num,
735709
None => { return None; }
736710
};
737711

738-
let mut end = buf.len();
739-
let mut n: BigUint = Zero::zero();
740-
let mut power: BigUint = One::one();
712+
let mut end = s.len();
713+
let mut n: BigUint = Zero::zero();
714+
let mut power: BigUint = One::one();
741715
loop {
742716
let start = cmp::max(end, unit_len) - unit_len;
743-
match str::from_utf8(buf.slice(start, end)).and_then(|s| {
744-
FromStrRadix::from_str_radix(s, radix)
745-
}) {
717+
match FromStrRadix::from_str_radix(s[start .. end], radix) {
746718
Some(d) => {
747719
let d: Option<BigUint> = FromPrimitive::from_uint(d);
748720
match d {
@@ -765,13 +737,40 @@ impl BigUint {
765737
power = power * base_num;
766738
}
767739
}
740+
}
741+
742+
impl BigUint {
743+
/// Creates and initializes a `BigUint`.
744+
///
745+
/// The digits are be in base 2^32.
746+
#[inline]
747+
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
748+
// omit trailing zeros
749+
let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
750+
digits.truncate(new_len);
751+
BigUint { data: digits }
752+
}
753+
754+
/// Creates and initializes a `BigUint`.
755+
///
756+
/// The digits are be in base 2^32.
757+
#[inline]
758+
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
759+
BigUint::new(slice.to_vec())
760+
}
761+
762+
/// Creates and initializes a `BigUint`.
763+
#[inline]
764+
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigUint> {
765+
str::from_utf8(buf).and_then(|s| FromStrRadix::from_str_radix(s, radix))
766+
}
768767

769768
#[inline]
770769
fn shl_unit(&self, n_unit: uint) -> BigUint {
771770
if n_unit == 0 || self.is_zero() { return (*self).clone(); }
772771

773772
let mut v = Vec::from_elem(n_unit, ZERO_BIG_DIGIT);
774-
v.push_all(self.data.as_slice());
773+
v.push_all(self.data[]);
775774
BigUint::new(v)
776775
}
777776

@@ -795,9 +794,7 @@ impl BigUint {
795794
fn shr_unit(&self, n_unit: uint) -> BigUint {
796795
if n_unit == 0 { return (*self).clone(); }
797796
if self.data.len() < n_unit { return Zero::zero(); }
798-
return BigUint::from_slice(
799-
self.data.slice(n_unit, self.data.len())
800-
);
797+
BigUint::from_slice(self.data[n_unit ..])
801798
}
802799

803800
#[inline]
@@ -1280,7 +1277,15 @@ impl FromStrRadix for BigInt {
12801277
/// Creates and initializes a BigInt.
12811278
#[inline]
12821279
fn from_str_radix(s: &str, radix: uint) -> Option<BigInt> {
1283-
BigInt::parse_bytes(s.as_bytes(), radix)
1280+
if s.is_empty() { return None; }
1281+
let mut sign = Plus;
1282+
let mut start = 0;
1283+
if s.starts_with("-") {
1284+
sign = Minus;
1285+
start = 1;
1286+
}
1287+
FromStrRadix::from_str_radix(s[start ..], radix)
1288+
.map(|bu| BigInt::from_biguint(sign, bu))
12841289
}
12851290
}
12861291

@@ -1396,18 +1401,12 @@ impl BigInt {
13961401
}
13971402

13981403
/// Creates and initializes a `BigInt`.
1404+
#[inline]
13991405
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigInt> {
1400-
if buf.is_empty() { return None; }
1401-
let mut sign = Plus;
1402-
let mut start = 0;
1403-
if buf[0] == b'-' {
1404-
sign = Minus;
1405-
start = 1;
1406-
}
1407-
return BigUint::parse_bytes(buf.slice(start, buf.len()), radix)
1408-
.map(|bu| BigInt::from_biguint(sign, bu));
1406+
str::from_utf8(buf).and_then(|s| FromStrRadix::from_str_radix(s, radix))
14091407
}
14101408

1409+
14111410
/// Converts this `BigInt` into a `BigUint`, if it's not negative.
14121411
#[inline]
14131412
pub fn to_biguint(&self) -> Option<BigUint> {

src/complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<T: fmt::Show + Num + PartialOrd> fmt::Show for Complex<T> {
174174

175175
#[cfg(test)]
176176
mod test {
177-
#![allow(non_uppercase_statics)]
177+
#![allow(non_upper_case_globals)]
178178

179179
use super::{Complex64, Complex};
180180
use std::num::{Zero, One, Float};

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
4545
#![feature(macro_rules)]
4646
#![feature(default_type_params)]
47+
#![feature(slicing_syntax)]
4748

4849
#![crate_name = "num"]
4950
#![experimental]

src/rational.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ mod test {
668668
#[test]
669669
fn test_to_from_str() {
670670
fn test(r: Rational, s: String) {
671-
assert_eq!(FromStr::from_str(s.as_slice()), Some(r));
671+
assert_eq!(FromStr::from_str(s[]), Some(r));
672672
assert_eq!(r.to_string(), s);
673673
}
674674
test(_1, "1".to_string());

0 commit comments

Comments
 (0)