@@ -27,48 +27,49 @@ pub fn calc_combination(n: u64, k: u64) -> u64 {
27
27
numerator / denominator
28
28
}
29
29
30
+ /// B: byte
30
31
#[ derive( PartialEq , PartialOrd ) ]
31
- enum BitUnit {
32
- Bit ,
32
+ enum ByteUnit {
33
+ B ,
33
34
KB ,
34
35
MB ,
35
36
GB ,
36
37
TB ,
37
38
}
38
39
39
- impl BitUnit {
40
+ impl ByteUnit {
40
41
fn to_bits ( & self ) -> u64 {
41
42
match self {
42
- BitUnit :: Bit => 1 ,
43
- BitUnit :: KB => 2u64 . pow ( 10 ) , // 2^10
44
- BitUnit :: MB => 2u64 . pow ( 20 ) , // 2^20
45
- BitUnit :: GB => 2u64 . pow ( 30 ) , // 2^30
46
- BitUnit :: TB => 2u64 . pow ( 40 ) , // 2^40
43
+ ByteUnit :: B => 1 ,
44
+ ByteUnit :: KB => 2u64 . pow ( 10 ) , // 2^10
45
+ ByteUnit :: MB => 2u64 . pow ( 20 ) , // 2^20
46
+ ByteUnit :: GB => 2u64 . pow ( 30 ) , // 2^30
47
+ ByteUnit :: TB => 2u64 . pow ( 40 ) , // 2^40
47
48
}
48
49
}
49
50
}
50
51
51
52
pub fn devide_bytes ( expression : & str ) -> String {
52
- let re = Regex :: new ( r"(?<num1>\d+)\s*(?<unit1>bits|bit |kb|mb|gb|tb)\s*(?<op>/)\s*(?<num2>\d+)\s*(?<unit2>bits|bit |kb|mb|gb|tb)" ) . unwrap ( ) ;
53
+ let re = Regex :: new ( r"(?<num1>\d+)\s*(?<unit1>byte|bytes |kb|mb|gb|tb)\s*(?<op>/)\s*(?<num2>\d+)\s*(?<unit2>byte|bytes |kb|mb|gb|tb)" ) . unwrap ( ) ;
53
54
if let Some ( caps) = re. captures ( expression) {
54
55
let num1: u64 = caps. name ( "num1" ) . unwrap ( ) . as_str ( ) . parse ( ) . unwrap ( ) ;
55
56
let unit1 = match caps. name ( "unit1" ) . unwrap ( ) . as_str ( ) {
56
- "bits " => BitUnit :: Bit ,
57
- "bit " => BitUnit :: Bit ,
58
- "kb" => BitUnit :: KB ,
59
- "mb" => BitUnit :: MB ,
60
- "gb" => BitUnit :: GB ,
61
- "tb" => BitUnit :: TB ,
57
+ "byte " => ByteUnit :: B ,
58
+ "bytes " => ByteUnit :: B ,
59
+ "kb" => ByteUnit :: KB ,
60
+ "mb" => ByteUnit :: MB ,
61
+ "gb" => ByteUnit :: GB ,
62
+ "tb" => ByteUnit :: TB ,
62
63
_ => return "Wrong input" . to_string ( ) ,
63
64
} ;
64
65
let num2: u64 = caps. name ( "num2" ) . unwrap ( ) . as_str ( ) . parse ( ) . unwrap ( ) ;
65
66
let unit2 = match caps. name ( "unit2" ) . unwrap ( ) . as_str ( ) {
66
- "bits " => BitUnit :: Bit ,
67
- "bit " => BitUnit :: Bit ,
68
- "kb" => BitUnit :: KB ,
69
- "mb" => BitUnit :: MB ,
70
- "gb" => BitUnit :: GB ,
71
- "tb" => BitUnit :: TB ,
67
+ "byte " => ByteUnit :: B ,
68
+ "bytes " => ByteUnit :: B ,
69
+ "kb" => ByteUnit :: KB ,
70
+ "mb" => ByteUnit :: MB ,
71
+ "gb" => ByteUnit :: GB ,
72
+ "tb" => ByteUnit :: TB ,
72
73
_ => return "Wrong input" . to_string ( ) ,
73
74
} ;
74
75
0 commit comments