From dd8616cff88bf62dae06ebcf6f0432f1d8dc870c Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 15:18:31 +0100 Subject: [PATCH 01/10] Allow noncompact CompactDecimal --- utils/fixed_decimal/src/compact.rs | 65 ++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 45a95c95749..5663feb852f 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -12,8 +12,10 @@ use crate::FixedDecimal; /// A struct containing a [`FixedDecimal`] significand together with an exponent, representing a /// number written in compact notation (such as 1.2M). -/// This represents a _source number_ that uses compact decimal notation, as defined +/// This represents a _source number_, as defined /// [in UTS #35](https://www.unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax). +/// The value exponent=0 is used internally to represent a non-compact notation +/// (such as 1 200 000). /// /// This is distinct from [`crate::ScientificDecimal`] because it does not represent leading 0s /// nor a sign in the exponent, and behaves differently in pluralization. @@ -23,6 +25,16 @@ pub struct CompactDecimal { exponent: i16, } +impl CompactDecimal { + pub fn significand(self) -> FixedDecimal { + self.significand + } + + pub fn exponent(self) -> i16 { + self.exponent + } +} + /// Render the [`CompactDecimal`] in sampleValue syntax. /// The letter c is used, rather than the deprecated e. /// @@ -34,12 +46,16 @@ pub struct CompactDecimal { /// # use writeable::assert_writeable_eq; /// # /// assert_writeable_eq!(CompactDecimal::from_str("+1.20c6").unwrap(), "+1.20c6"); +/// assert_writeable_eq!(CompactDecimal::from_str("+1729").unwrap(), "+1729"); /// ``` impl writeable::Writeable for CompactDecimal { fn write_to(&self, sink: &mut W) -> fmt::Result { self.significand.write_to(sink)?; - sink.write_char('c')?; - self.exponent.write_to(sink) + if self.exponent != 0 { + sink.write_char('c')?; + self.exponent.write_to(sink)?; + } + Ok(()) } fn writeable_length_hint(&self) -> writeable::LengthHint { @@ -65,37 +81,42 @@ impl TryFrom<&[u8]> for CompactDecimal { } let mut parts = input_str.split(|&c| c == b'c'); let significand = FixedDecimal::try_from(parts.next().ok_or(Error::Syntax)?)?; - let exponent_str = - core::str::from_utf8(parts.next().ok_or(Error::Syntax)?).map_err(|_| Error::Syntax)?; - if parts.next().is_some() { - return Err(Error::Syntax); - } - if exponent_str.is_empty() - || exponent_str.bytes().next() == Some(b'0') - || !exponent_str.bytes().all(|c| (b'0'..=b'9').contains(&c)) - { - return Err(Error::Syntax); + match parts.next() { + None => Ok(CompactDecimal { + significand, + exponent: 0, + }), + Some(exponent_str) => { + let exponent_str = core::str::from_utf8(exponent_str).map_err(|_| Error::Syntax)?; + if parts.next().is_some() { + return Err(Error::Syntax); + } + if exponent_str.is_empty() + || exponent_str.bytes().next() == Some(b'0') + || !exponent_str.bytes().all(|c| (b'0'..=b'9').contains(&c)) + { + return Err(Error::Syntax); + } + let exponent = exponent_str.parse().map_err(|_| Error::Limit)?; + Ok(CompactDecimal { + significand, + exponent, + }) + } } - let exponent = exponent_str.parse().map_err(|_| Error::Limit)?; - Ok(CompactDecimal { - significand, - exponent, - }) } } #[test] fn test_compact_syntax_error() { + assert_eq!(CompactDecimal::from_str("+1729").unwrap().significand(), FixedDecimal::from_str("+1729").unwrap()); + assert_eq!(format!("{}", CompactDecimal::from_str("+1729").unwrap()), "+1729"); #[derive(Debug)] struct TestCase { pub input_str: &'static str, pub expected_err: Option, } let cases = [ - TestCase { - input_str: "5", - expected_err: Some(Error::Syntax), - }, TestCase { input_str: "-123e4", expected_err: Some(Error::Syntax), From 0b693bcdca96560c748db70c6511e147f1f94923 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 15:32:36 +0100 Subject: [PATCH 02/10] remove stray assertions --- utils/fixed_decimal/src/compact.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 5663feb852f..4df9800af56 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -109,8 +109,6 @@ impl TryFrom<&[u8]> for CompactDecimal { #[test] fn test_compact_syntax_error() { - assert_eq!(CompactDecimal::from_str("+1729").unwrap().significand(), FixedDecimal::from_str("+1729").unwrap()); - assert_eq!(format!("{}", CompactDecimal::from_str("+1729").unwrap()), "+1729"); #[derive(Debug)] struct TestCase { pub input_str: &'static str, From 8bbc3b267616863296bf0d64b68bb1ff0d275f49 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 15:33:59 +0100 Subject: [PATCH 03/10] noun --- utils/fixed_decimal/src/compact.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 4df9800af56..239be28b72f 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -14,8 +14,8 @@ use crate::FixedDecimal; /// number written in compact notation (such as 1.2M). /// This represents a _source number_, as defined /// [in UTS #35](https://www.unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax). -/// The value exponent=0 is used internally to represent a non-compact notation -/// (such as 1 200 000). +/// The value exponent=0 is used internally to represent a number in non-compact +/// notation (such as 1 200 000). /// /// This is distinct from [`crate::ScientificDecimal`] because it does not represent leading 0s /// nor a sign in the exponent, and behaves differently in pluralization. From 7127e5f45e0b2d0a568a5304b4515961d2ce9b64 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 16:50:19 +0100 Subject: [PATCH 04/10] Update utils/fixed_decimal/src/compact.rs Co-authored-by: Shane F. Carr --- utils/fixed_decimal/src/compact.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 239be28b72f..8b919dc4eeb 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -26,7 +26,7 @@ pub struct CompactDecimal { } impl CompactDecimal { - pub fn significand(self) -> FixedDecimal { + pub fn into_significand(self) -> FixedDecimal { self.significand } From 193e9b3d735e06fd89f35a5ebf8ce3a6c1750e73 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 16:50:25 +0100 Subject: [PATCH 05/10] Update utils/fixed_decimal/src/compact.rs Co-authored-by: Shane F. Carr --- utils/fixed_decimal/src/compact.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 8b919dc4eeb..d1a9d773f74 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -30,7 +30,7 @@ impl CompactDecimal { self.significand } - pub fn exponent(self) -> i16 { + pub fn exponent(&self) -> i16 { self.exponent } } From 03afa3ed8693d50398e6ac3ba9d1907f4c1ae65d Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 16:50:35 +0100 Subject: [PATCH 06/10] Update utils/fixed_decimal/src/compact.rs Co-authored-by: Shane F. Carr --- utils/fixed_decimal/src/compact.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index d1a9d773f74..a85810fea4b 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -14,7 +14,7 @@ use crate::FixedDecimal; /// number written in compact notation (such as 1.2M). /// This represents a _source number_, as defined /// [in UTS #35](https://www.unicode.org/reports/tr35/tr35-numbers.html#Plural_rules_syntax). -/// The value exponent=0 is used internally to represent a number in non-compact +/// The value exponent=0 represents a number in non-compact /// notation (such as 1 200 000). /// /// This is distinct from [`crate::ScientificDecimal`] because it does not represent leading 0s From 8285b0ad00fef3b9ca012958c9b7121c0a3a9892 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Wed, 14 Dec 2022 17:10:01 +0100 Subject: [PATCH 07/10] dottore --- utils/fixed_decimal/src/compact.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index a85810fea4b..16705c24ac3 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -26,10 +26,27 @@ pub struct CompactDecimal { } impl CompactDecimal { + /// Returns the significand of `self`. + /// ``` + /// # use fixed_decimal::CompactDecimal; + /// # use fixed_decimal::FixedDecimal; + /// # use std::str::FromStr; + /// # + /// assert_eq!(CompactDecimal::from_str("+1.20c6").unwrap().into_significand(), FixedDecimal::from_str("+1.20").unwrap()); + /// ``` pub fn into_significand(self) -> FixedDecimal { self.significand } + /// Returns the exponent of `self`. + /// ``` + /// # use fixed_decimal::CompactDecimal; + /// # use fixed_decimal::FixedDecimal; + /// # use std::str::FromStr; + /// # + /// assert_eq!(CompactDecimal::from_str("+1.20c6").unwrap().exponent(), 6); + /// assert_eq!(CompactDecimal::from_str("1729").unwrap().exponent(), 0); + /// ``` pub fn exponent(&self) -> i16 { self.exponent } From 31d35b3f063c963449d7e0c84f508928b5bdd6b0 Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Thu, 15 Dec 2022 15:34:28 +0100 Subject: [PATCH 08/10] cargo make diplomat-coverage --- ffi/diplomat/ffi_coverage/tests/missing_apis.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ffi/diplomat/ffi_coverage/tests/missing_apis.txt b/ffi/diplomat/ffi_coverage/tests/missing_apis.txt index ff0374af1fe..f2d9eacb4af 100644 --- a/ffi/diplomat/ffi_coverage/tests/missing_apis.txt +++ b/ffi/diplomat/ffi_coverage/tests/missing_apis.txt @@ -1,5 +1,7 @@ fixed_decimal::CompactDecimal#Struct +fixed_decimal::CompactDecimal::exponent#FnInStruct fixed_decimal::CompactDecimal::from_str#FnInStruct +fixed_decimal::CompactDecimal::into_significand#FnInStruct fixed_decimal::CompactDecimal::write_to#FnInStruct fixed_decimal::FixedInteger#Struct fixed_decimal::FixedInteger::from_str#FnInStruct From 3e6086b39758ea821f49541c774342cd8efceb9a Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Thu, 15 Dec 2022 19:10:12 +0100 Subject: [PATCH 09/10] fix test --- utils/fixed_decimal/src/compact.rs | 6 +++++- utils/writeable/src/lib.rs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index 16705c24ac3..fbe9dd87ae8 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -76,7 +76,11 @@ impl writeable::Writeable for CompactDecimal { } fn writeable_length_hint(&self) -> writeable::LengthHint { - self.significand.writeable_length_hint() + 1 + self.exponent.writeable_length_hint() + let mut result = self.significand.writeable_length_hint(); + if self.exponent != 0 { + result += self.exponent.writeable_length_hint() + 1; + } + return result; } } diff --git a/utils/writeable/src/lib.rs b/utils/writeable/src/lib.rs index 1d0597b052b..afba49cf847 100644 --- a/utils/writeable/src/lib.rs +++ b/utils/writeable/src/lib.rs @@ -329,9 +329,17 @@ macro_rules! assert_writeable_eq { assert_eq!(actual_str, $expected_str, $($arg)*); assert_eq!(actual_str, $crate::Writeable::write_to_string(actual_writeable), $($arg)+); let length_hint = $crate::Writeable::writeable_length_hint(actual_writeable); - assert!(length_hint.0 <= actual_str.len(), $($arg)*); + assert!( + length_hint.0 <= actual_str.len(), + "hint lower bound {} larger than actual length {}: {}", + length_hint.0, actual_str.len(), format!($($arg)*), + ); if let Some(upper) = length_hint.1 { - assert!(actual_str.len() <= upper, $($arg)*); + assert!( + actual_str.len() <= upper, + "hint upper bound {} smaller than actual length {}: {}", + length_hint.0, actual_str.len(), format!($($arg)*), + ); } assert_eq!(actual_writeable.to_string(), $expected_str); }}; From 4305c73763732369a6a790ea578592ba2196468f Mon Sep 17 00:00:00 2001 From: Robin Leroy Date: Fri, 16 Dec 2022 09:55:13 +0100 Subject: [PATCH 10/10] appease clippy --- utils/fixed_decimal/src/compact.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/fixed_decimal/src/compact.rs b/utils/fixed_decimal/src/compact.rs index fbe9dd87ae8..d20f8e41fcc 100644 --- a/utils/fixed_decimal/src/compact.rs +++ b/utils/fixed_decimal/src/compact.rs @@ -80,7 +80,7 @@ impl writeable::Writeable for CompactDecimal { if self.exponent != 0 { result += self.exponent.writeable_length_hint() + 1; } - return result; + result } }