@@ -67,7 +67,7 @@ use std::num::{ToPrimitive, FromPrimitive};
67
67
use std:: num:: { Zero , One , FromStrRadix } ;
68
68
use std:: str;
69
69
use std:: string:: String ;
70
- use std:: { uint , i64, u64} ;
70
+ use std:: { i64, u64} ;
71
71
72
72
/// A `BigDigit` is a `BigUint`'s composing element.
73
73
pub type BigDigit = u32 ;
@@ -294,8 +294,8 @@ impl Mul<BigUint, BigUint> for BigUint {
294
294
if self . is_zero ( ) || other. is_zero ( ) { return Zero :: zero ( ) ; }
295
295
296
296
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 ] ) ; }
299
299
300
300
// Using Karatsuba multiplication
301
301
// (a1 * base + a0) * (b1 * base + b0)
@@ -340,8 +340,8 @@ impl Mul<BigUint, BigUint> for BigUint {
340
340
#[ inline]
341
341
fn cut_at ( a : & BigUint , n : uint ) -> ( BigUint , BigUint ) {
342
342
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] ) )
345
345
}
346
346
347
347
#[ inline]
@@ -488,7 +488,7 @@ impl Integer for BigUint {
488
488
return ( Zero :: zero ( ) , Zero :: zero ( ) , ( * a) . clone ( ) ) ;
489
489
}
490
490
491
- let an = a. data . slice_from ( a. data . len ( ) - n) ;
491
+ let an = a. data [ a. data . len ( ) - n .. ] ;
492
492
let bn = * b. data . last ( ) . unwrap ( ) ;
493
493
let mut d = Vec :: with_capacity ( an. len ( ) ) ;
494
494
let mut carry = 0 ;
@@ -545,7 +545,7 @@ impl Integer for BigUint {
545
545
#[ inline]
546
546
fn is_even ( & self ) -> bool {
547
547
// Considering only the last digit.
548
- match self . data . as_slice ( ) . head ( ) {
548
+ match self . data . head ( ) {
549
549
Some ( x) => x. is_even ( ) ,
550
550
None => true
551
551
}
@@ -574,8 +574,8 @@ impl ToPrimitive for BigUint {
574
574
fn to_u64 ( & self ) -> Option < u64 > {
575
575
match self . data . len ( ) {
576
576
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 ] )
579
579
as u64 ) ,
580
580
_ => None
581
581
}
@@ -658,9 +658,9 @@ fn to_str_radix(me: &BigUint, radix: uint) -> String {
658
658
assert ! ( 1 < radix && radix <= 16 , "The radix must be within (1, 16]" ) ;
659
659
let ( base, max_len) = get_radix_base ( radix) ;
660
660
if base == BigDigit :: base {
661
- return fill_concat ( me. data . as_slice ( ) , radix, max_len)
661
+ return fill_concat ( me. data [ ] , radix, max_len)
662
662
}
663
- return fill_concat ( convert_base ( me, base) . as_slice ( ) , radix, max_len) ;
663
+ return fill_concat ( convert_base ( me, base) [ ] , radix, max_len) ;
664
664
665
665
fn convert_base ( n : & BigUint , base : DoubleBigDigit ) -> Vec < BigDigit > {
666
666
let divider = base. to_biguint ( ) . unwrap ( ) ;
@@ -684,10 +684,10 @@ fn to_str_radix(me: &BigUint, radix: uint) -> String {
684
684
let mut s = String :: with_capacity ( v. len ( ) * l) ;
685
685
for n in v. iter ( ) . rev ( ) {
686
686
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[ ] ) ;
689
689
}
690
- s. as_slice ( ) . trim_left_chars ( '0' ) . to_string ( )
690
+ s. trim_left_chars ( '0' ) . to_string ( )
691
691
}
692
692
}
693
693
@@ -703,46 +703,18 @@ impl FromStrRadix for BigUint {
703
703
/// Creates and initializes a `BigUint`.
704
704
#[ inline]
705
705
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 > {
732
706
let ( base, unit_len) = get_radix_base ( radix) ;
733
707
let base_num = match base. to_biguint ( ) {
734
708
Some ( base_num) => base_num,
735
709
None => { return None ; }
736
710
} ;
737
711
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 ( ) ;
741
715
loop {
742
716
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) {
746
718
Some ( d) => {
747
719
let d: Option < BigUint > = FromPrimitive :: from_uint ( d) ;
748
720
match d {
@@ -765,13 +737,40 @@ impl BigUint {
765
737
power = power * base_num;
766
738
}
767
739
}
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
+ }
768
767
769
768
#[ inline]
770
769
fn shl_unit ( & self , n_unit : uint ) -> BigUint {
771
770
if n_unit == 0 || self . is_zero ( ) { return ( * self ) . clone ( ) ; }
772
771
773
772
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 [ ] ) ;
775
774
BigUint :: new ( v)
776
775
}
777
776
@@ -795,9 +794,7 @@ impl BigUint {
795
794
fn shr_unit ( & self , n_unit : uint ) -> BigUint {
796
795
if n_unit == 0 { return ( * self ) . clone ( ) ; }
797
796
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 ..] )
801
798
}
802
799
803
800
#[ inline]
@@ -1280,7 +1277,15 @@ impl FromStrRadix for BigInt {
1280
1277
/// Creates and initializes a BigInt.
1281
1278
#[ inline]
1282
1279
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) )
1284
1289
}
1285
1290
}
1286
1291
@@ -1396,18 +1401,12 @@ impl BigInt {
1396
1401
}
1397
1402
1398
1403
/// Creates and initializes a `BigInt`.
1404
+ #[ inline]
1399
1405
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) )
1409
1407
}
1410
1408
1409
+
1411
1410
/// Converts this `BigInt` into a `BigUint`, if it's not negative.
1412
1411
#[ inline]
1413
1412
pub fn to_biguint ( & self ) -> Option < BigUint > {
0 commit comments