浏览代码

Auto merge of #158 - anchovieshat:master, r=cuviper

Add LowerHex and UpperHex formatting

These were mysteriously missing from BigUint.
Homu 9 年之前
父节点
当前提交
e3bc794ca4
共有 1 个文件被更改,包括 31 次插入0 次删除
  1. 31 0
      src/bigint.rs

+ 31 - 0
src/bigint.rs

@@ -76,6 +76,7 @@ use std::fmt;
 use std::cmp::Ordering::{self, Less, Greater, Equal};
 use std::{f32, f64};
 use std::{u8, i64, u64};
+use std::ascii::AsciiExt;
 
 // Some of the tests of non-RNG-based functionality are randomized using the
 // RNG-based functionality, so the RNG-based functionality needs to be enabled
@@ -242,6 +243,18 @@ impl fmt::Display for BigUint {
     }
 }
 
+impl fmt::LowerHex for BigUint {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.to_str_radix(16))
+    }
+}
+
+impl fmt::UpperHex for BigUint {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        write!(f, "{}", self.to_str_radix(16).to_ascii_uppercase())
+    }
+}
+
 impl FromStr for BigUint {
     type Err = ParseBigIntError;
 
@@ -3710,6 +3723,24 @@ mod biguint_tests {
         }
     }
 
+    #[test]
+    fn test_lower_hex() {
+        let a = BigUint::parse_bytes(b"A", 16).unwrap();
+        let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
+
+        assert_eq!(format!("{:x}", a), "a");
+        assert_eq!(format!("{:x}", hello), "48656c6c6f20776f726c6421");
+    }
+
+    #[test]
+    fn test_upper_hex() {
+        let a = BigUint::parse_bytes(b"A", 16).unwrap();
+        let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();
+
+        assert_eq!(format!("{:X}", a), "A");
+        assert_eq!(format!("{:X}", hello), "48656C6C6F20776F726C6421");
+    }
+
     #[test]
     fn test_factor() {
         fn factor(n: usize) -> BigUint {