use crate::numeric_contract::EncodingError;
pub(crate) const INPUT_CAP: usize = 8192;
pub(crate) const COEFFICIENT_BITS: u64 = 32768;
pub(crate) const INTERMEDIATE_BITS: u64 = 131072;
pub(crate) const POWER_CAP: u32 = 20000;
pub(crate) const SHIFT_CAP: u32 = 65536;
pub(crate) const OUTPUT_CAP: usize = 16384;
pub(crate) const EXPONENT_CAP: u32 = 1_000_000;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Profile {
decimal: u8,
thousands: &'static [u8],
grouping: &'static [u8],
}
impl Profile {
pub const C: Self = Self::new(b'.', b"", b"");
pub const EN_NUMERIC: Self = Self::new(b'.', b",", b"\x03");
pub const DE_NUMERIC: Self = Self::new(b',', b".", b"\x03");
pub const fn new(decimal: u8, thousands: &'static [u8], grouping: &'static [u8]) -> Self {
Self {
decimal,
thousands,
grouping,
}
}
pub fn from_name(name: &str) -> Result<Self, Error> {
match name {
"C" => Ok(Self::C),
"de-numeric" => Ok(Self::DE_NUMERIC),
_ => Err(Error::UnsupportedProfile),
}
}
pub const fn radix(self) -> u8 {
self.decimal
}
pub const fn erange(self) -> i32 {
34
}
pub const fn grouping(self) -> &'static [u8] {
self.grouping
}
pub const fn thousands(self) -> &'static [u8] {
self.thousands
}
pub fn separates(self, right: usize) -> bool {
if self.thousands.is_empty() || right == 0 {
return false;
}
let mut boundary = 0usize;
let mut size = 0usize;
for &next in self.grouping {
if next == 0 {
break;
}
if next >= 127 {
return false;
}
size = usize::from(next);
boundary += size;
if boundary >= right {
return boundary == right;
}
}
size != 0 && (right - boundary).is_multiple_of(size)
}
}
#[cfg(test)]
mod tests {
use super::Profile;
fn marks(profile: Profile, digits: usize) -> String {
(1..digits)
.rev()
.map(|right| if profile.separates(right) { ',' } else { '-' })
.collect()
}
#[test]
fn grouping_follows_the_c_library_rules() {
let group = |grouping| marks(Profile::new(b'.', b",", grouping), 10);
assert_eq!(group(b"\x03"), ",--,--,--");
assert_eq!(group(b"\x03\x02"), ",-,-,-,--");
assert_eq!(group(b"\x03\x00"), group(b"\x03"));
assert_eq!(group(b"\x03\x02\x00"), group(b"\x03\x02"));
assert_eq!(group(b"\x03\x7f"), "------,--");
assert_eq!(group(b"\x00"), "---------");
assert_eq!(group(b"\x7f"), "---------");
assert_eq!(marks(Profile::new(b'.', b"", b"\x03"), 10), "---------");
}
#[test]
fn grouping_a_long_integer_is_linear() {
let profile = Profile::new(b'.', b",", b"\x02\x02\x02\x03");
assert_eq!(marks(profile, 5_000).matches(',').count(), 1_667);
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Error {
UnsupportedProfile,
UnsupportedFormat,
InputCapacity,
OutputCapacity,
CoefficientCapacity,
IntermediateCapacity,
PowerCapacity,
ShiftCapacity,
InvalidView,
Allocation,
InternalInvariant,
Encoding(EncodingError),
}
impl From<EncodingError> for Error {
fn from(error: EncodingError) -> Self {
Self::Encoding(error)
}
}
pub(crate) fn copy_bytes(bytes: &[u8]) -> Result<Vec<u8>, Error> {
let mut result = Vec::new();
result
.try_reserve_exact(bytes.len())
.map_err(|_| Error::Allocation)?;
result.extend_from_slice(bytes);
Ok(result)
}