From 80c4d01b7ff0c7017e4beb443f4004540f4a6efa Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Sat, 16 Jan 2016 21:52:47 -0800 Subject: [PATCH 1/4] Add LowerHex and UpperHex formatting --- src/bigint.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/bigint.rs b/src/bigint.rs index 386e749..07de81e 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -242,6 +242,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_uppercase()) + } +} + impl FromStr for BigUint { type Err = ParseBigIntError; From 04000436b162fc11cb5f8c55855e8a308054c204 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Sat, 16 Jan 2016 22:27:56 -0800 Subject: [PATCH 2/4] Switch to ascii uppercase function --- src/bigint.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bigint.rs b/src/bigint.rs index 07de81e..4acc2ac 100644 --- a/src/bigint.rs +++ b/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 @@ -250,7 +251,7 @@ impl fmt::LowerHex for BigUint { impl fmt::UpperHex for BigUint { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.to_str_radix(16).to_uppercase()) + write!(f, "{}", self.to_str_radix(16).to_ascii_uppercase()) } } From d5d7c855b0c8f4fa1db4e9dee6b22dc873ca25e8 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Sun, 17 Jan 2016 12:13:45 -0800 Subject: [PATCH 3/4] Add tests for UpperHex and LowerHex --- src/bigint.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/bigint.rs b/src/bigint.rs index 4acc2ac..6255070 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -3723,6 +3723,24 @@ mod biguint_tests { } } + #[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_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_factor() { fn factor(n: usize) -> BigUint { From a89a2e82e69199f92823955ee6f2eb9dafb00489 Mon Sep 17 00:00:00 2001 From: Colin Davidson Date: Sun, 17 Jan 2016 12:17:52 -0800 Subject: [PATCH 4/4] Fix test names Upper and Lower test names were swapped --- src/bigint.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bigint.rs b/src/bigint.rs index 6255070..65edeb6 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -3724,7 +3724,7 @@ mod biguint_tests { } #[test] - fn test_upper_hex() { + fn test_lower_hex() { let a = BigUint::parse_bytes(b"A", 16).unwrap(); let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap(); @@ -3733,7 +3733,7 @@ mod biguint_tests { } #[test] - fn test_lower_hex() { + fn test_upper_hex() { let a = BigUint::parse_bytes(b"A", 16).unwrap(); let hello = BigUint::parse_bytes("22405534230753963835153736737".as_bytes(), 10).unwrap();