use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, Default)]
pub struct Fixed {
digits: Vec<u8>,
scale: u16,
negative: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseFixedError;
impl fmt::Display for ParseFixedError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid fixed-point literal")
}
}
impl std::error::Error for ParseFixedError {}
impl Fixed {
pub fn from_digits(digits: &[u8], scale: u16, negative: bool) -> Option<Fixed> {
if digits.iter().any(|&d| d > 9) || digits.len() < scale as usize {
return None;
}
let int_len = digits.len() - scale as usize;
let skip = digits[..int_len].iter().take_while(|&&d| d == 0).count();
let digits = digits[skip..].to_vec();
let negative = negative && digits.iter().any(|&d| d != 0);
Some(Fixed {
digits,
scale,
negative,
})
}
pub fn digits(&self) -> &[u8] {
&self.digits
}
pub fn scale(&self) -> u16 {
self.scale
}
pub fn is_negative(&self) -> bool {
self.negative
}
fn int_digits(&self) -> &[u8] {
&self.digits[..self.digits.len() - self.scale as usize]
}
fn frac_digits(&self) -> &[u8] {
&self.digits[self.digits.len() - self.scale as usize..]
}
pub fn truncate(&self, scale: u16) -> Fixed {
if scale >= self.scale {
return self.clone();
}
let cut = (self.scale - scale) as usize;
let digits = &self.digits[..self.digits.len() - cut];
Fixed::from_digits(digits, scale, self.negative).expect("valid digits")
}
pub fn round(&self, scale: u16) -> Fixed {
if scale >= self.scale {
return self.clone();
}
let cut = (self.scale - scale) as usize;
let keep = self.digits.len() - cut;
let mut digits = self.digits[..keep].to_vec();
if self.digits[keep] >= 5 {
let mut i = digits.len();
loop {
if i == 0 {
digits.insert(0, 1);
break;
}
i -= 1;
if digits[i] == 9 {
digits[i] = 0;
} else {
digits[i] += 1;
break;
}
}
}
Fixed::from_digits(&digits, scale, self.negative).expect("valid digits")
}
pub fn to_digits(&self, digits: u16, scale: u16) -> Option<Vec<u8>> {
let int = self.int_digits();
let int_room = digits.checked_sub(scale)? as usize;
if int.len() > int_room {
return None;
}
let mut out = vec![0; int_room - int.len()];
out.extend_from_slice(int);
let frac = self.frac_digits();
for i in 0..scale as usize {
out.push(frac.get(i).copied().unwrap_or(0));
}
Some(out)
}
fn normalized(&self) -> (&[u8], &[u8]) {
let frac = self.frac_digits();
let end = frac.len() - frac.iter().rev().take_while(|&&d| d == 0).count();
(self.int_digits(), &frac[..end])
}
fn is_zero(&self) -> bool {
self.digits.iter().all(|&d| d == 0)
}
}
impl PartialEq for Fixed {
fn eq(&self, other: &Fixed) -> bool {
if self.is_zero() && other.is_zero() {
return true;
}
self.negative == other.negative && self.normalized() == other.normalized()
}
}
impl Eq for Fixed {}
impl FromStr for Fixed {
type Err = ParseFixedError;
fn from_str(s: &str) -> Result<Fixed, ParseFixedError> {
let s = s.strip_suffix(['d', 'D']).unwrap_or(s);
let (negative, s) = match s.as_bytes().first() {
Some(b'-') => (true, &s[1..]),
Some(b'+') => (false, &s[1..]),
_ => (false, s),
};
let (int, frac) = s.split_once('.').unwrap_or((s, ""));
if int.is_empty() && frac.is_empty() {
return Err(ParseFixedError);
}
let mut digits = Vec::with_capacity(int.len() + frac.len());
for c in int.bytes().chain(frac.bytes()) {
if !c.is_ascii_digit() {
return Err(ParseFixedError);
}
digits.push(c - b'0');
}
let scale = u16::try_from(frac.len()).map_err(|_| ParseFixedError)?;
Fixed::from_digits(&digits, scale, negative).ok_or(ParseFixedError)
}
}
impl fmt::Display for Fixed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.negative {
f.write_str("-")?;
}
let int = self.int_digits();
if int.is_empty() {
f.write_str("0")?;
}
for d in int {
write!(f, "{d}")?;
}
let frac = self.frac_digits();
if !frac.is_empty() {
f.write_str(".")?;
for d in frac {
write!(f, "{d}")?;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn f(s: &str) -> Fixed {
s.parse().unwrap()
}
#[test]
fn parses_and_prints() {
assert_eq!(f("123.45").to_string(), "123.45");
assert_eq!(f("-0.50").to_string(), "-0.50");
assert_eq!(f("-1.50").to_string(), "-1.50");
assert_eq!(f("007").to_string(), "7");
assert_eq!(f(".5").to_string(), "0.5");
assert_eq!(f("1.5d").to_string(), "1.5");
assert_eq!(f("-0").to_string(), "0");
assert!("".parse::<Fixed>().is_err());
assert!("1.2.3".parse::<Fixed>().is_err());
assert!("-".parse::<Fixed>().is_err());
}
#[test]
fn compares_by_number() {
assert_eq!(f("1.50"), f("1.5"));
assert_eq!(f("-0.0"), f("0"));
assert_ne!(f("1.5"), f("-1.5"));
assert_ne!(f("1.5"), f("15"));
}
#[test]
fn fits_to_a_type() {
assert_eq!(f("12.345").to_digits(5, 2), Some(vec![0, 1, 2, 3, 4]));
assert_eq!(f("-1").to_digits(3, 1), Some(vec![0, 1, 0]));
assert_eq!(f("1234").to_digits(5, 2), None);
assert_eq!(f("0.5").to_digits(1, 1), Some(vec![5]));
}
#[test]
fn rounds_and_truncates() {
assert_eq!(f("1.25").round(1).to_string(), "1.3");
assert_eq!(f("-1.25").round(1).to_string(), "-1.3");
assert_eq!(f("9.96").round(1).to_string(), "10.0");
assert_eq!(f("1.29").truncate(1).to_string(), "1.2");
assert_eq!(f("1.2").round(3).to_string(), "1.2");
}
}