use std::fmt;
use std::str::FromStr;
use crate::error::{DecodeError, ParseError};
const PACKED_BYTES: usize = DigitString::MAX_DIGITS.div_ceil(2);
const TBCD_FILLER: u8 = 0x0F;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct DigitString {
packed: [u8; PACKED_BYTES],
len: u8,
}
impl DigitString {
pub const MAX_DIGITS: usize = 20;
pub const EMPTY: Self = Self {
packed: [0; PACKED_BYTES],
len: 0,
};
#[must_use]
pub fn parse(s: &str, max: usize) -> Option<Self> {
let mut out = Self::EMPTY;
for byte in s.bytes() {
if byte.is_ascii_whitespace() || byte == b'-' {
continue;
}
if !byte.is_ascii_digit() {
return None;
}
out = out.checked_push(byte - b'0')?;
if out.len() > max {
return None;
}
}
Some(out)
}
#[must_use]
pub const fn at(self, index: usize) -> u8 {
if index >= self.len as usize {
return 0;
}
(self.packed[index / 2] >> (4 * (index % 2))) & 0x0F
}
#[must_use]
pub const fn len(self) -> usize {
self.len as usize
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.len == 0
}
#[must_use]
pub fn slice(self, start: usize, count: usize) -> Self {
let mut out = Self::EMPTY;
if start + count > self.len() {
return out;
}
for i in 0..count {
out = out
.checked_push(self.at(start + i))
.expect("a sub-run of a valid run always fits");
}
out
}
#[must_use]
pub fn to_u64(self) -> u64 {
let mut value = 0u64;
for i in 0..self.len() {
value = value
.saturating_mul(10)
.saturating_add(u64::from(self.at(i)));
}
value
}
#[must_use]
pub const fn checked_push(mut self, digit: u8) -> Option<Self> {
let i = self.len as usize;
if i >= Self::MAX_DIGITS || digit > 9 {
return None;
}
self.packed[i / 2] |= digit << (4 * (i % 2));
self.len += 1;
Some(self)
}
#[must_use]
pub fn luhn_valid(self) -> bool {
if self.len() < 2 {
return false;
}
let mut sum = 0u32;
for (position, index) in (0..self.len()).rev().enumerate() {
let mut d = u32::from(self.at(index));
if position % 2 == 1 {
d *= 2;
if d > 9 {
d -= 9;
}
}
sum += d;
}
sum.is_multiple_of(10)
}
#[must_use]
pub fn luhn_check_digit(self) -> u8 {
let mut sum = 0u32;
for (position, index) in (0..self.len()).rev().enumerate() {
let mut d = u32::from(self.at(index));
if position % 2 == 0 {
d *= 2;
if d > 9 {
d -= 9;
}
}
sum += d;
}
((10 - (sum % 10)) % 10) as u8
}
pub fn from_tbcd(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut out = Self::EMPTY;
for (offset, octet) in bytes.iter().enumerate() {
let low = octet & 0x0F;
let high = octet >> 4;
if low == TBCD_FILLER {
return Err(DecodeError::new(offset, "a decimal digit, not a filler"));
}
out = out.checked_push(low).ok_or_else(|| {
DecodeError::new(offset, "a decimal digit within the length limit")
})?;
if high == TBCD_FILLER {
if offset + 1 != bytes.len() {
return Err(DecodeError::new(
offset,
"a filler only in the final high nibble",
));
}
return Ok(out);
}
out = out.checked_push(high).ok_or_else(|| {
DecodeError::new(offset, "a decimal digit within the length limit")
})?;
}
Ok(out)
}
pub fn to_tbcd(self, out: &mut [u8]) -> Result<usize, DecodeError> {
let needed = self.len().div_ceil(2);
if out.len() < needed {
return Err(DecodeError::new(0, "a buffer of at least len()/2 octets"));
}
for octet in out.iter_mut().take(needed) {
*octet = 0;
}
for i in 0..self.len() {
let digit = self.at(i);
if i % 2 == 0 {
out[i / 2] |= digit;
} else {
out[i / 2] |= digit << 4;
}
}
if !self.len().is_multiple_of(2) {
out[needed - 1] |= TBCD_FILLER << 4;
}
Ok(needed)
}
pub fn from_bcd(bytes: &[u8]) -> Result<Self, DecodeError> {
let mut out = Self::EMPTY;
for (offset, octet) in bytes.iter().enumerate() {
for digit in [octet >> 4, octet & 0x0F] {
out = out.checked_push(digit).ok_or_else(|| {
DecodeError::new(offset, "a decimal digit within the length limit")
})?;
}
}
Ok(out)
}
pub fn to_bcd(self, out: &mut [u8]) -> Result<usize, DecodeError> {
if !self.len().is_multiple_of(2) {
return Err(DecodeError::new(
0,
"an even digit count; plain BCD has no filler",
));
}
let needed = self.len() / 2;
if out.len() < needed {
return Err(DecodeError::new(0, "a buffer of at least len()/2 octets"));
}
for (index, octet) in out.iter_mut().enumerate().take(needed) {
*octet = (self.at(index * 2) << 4) | self.at(index * 2 + 1);
}
Ok(needed)
}
}
impl fmt::Display for DigitString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for i in 0..self.len() {
write!(f, "{}", self.at(i))?;
}
Ok(())
}
}
impl FromStr for DigitString {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s, Self::MAX_DIGITS).ok_or_else(|| {
ParseError::format(
"DigitString",
s,
"up to 20 decimal digits, optionally separated by spaces or hyphens",
)
})
}
}
#[cfg(feature = "serde")]
mod serde_impl {
use super::DigitString;
use serde::de::{self, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
impl Serialize for DigitString {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for DigitString {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct V;
impl Visitor<'_> for V {
type Value = DigitString;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a run of up to 20 decimal digits")
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<DigitString, E> {
v.parse().map_err(de::Error::custom)
}
}
deserializer.deserialize_str(V)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn packed_digits_compare_by_sequence() {
let a: DigitString = "0123456789".parse().unwrap();
assert_eq!(a.to_string(), "0123456789");
assert_eq!(a.len(), 10);
assert_eq!(a.at(0), 0);
assert_eq!(a.at(9), 9);
assert_eq!(a, DigitString::parse("01-23 456789", 20).unwrap());
assert_ne!(
DigitString::parse("01", 20).unwrap(),
DigitString::parse("1", 20).unwrap()
);
}
#[test]
fn digits_reject_non_numeric_and_overlong_input() {
assert!(DigitString::parse("12a4", 20).is_none());
assert!(DigitString::parse("123456789012345678901", 20).is_none());
assert!(DigitString::parse("1234", 3).is_none());
assert!(DigitString::EMPTY.is_empty());
}
#[test]
fn tbcd_matches_the_published_example() {
let decoded = DigitString::from_tbcd(&[0x21, 0x43, 0xF5]).unwrap();
assert_eq!(decoded.to_string(), "12345");
let mut buffer = [0u8; 3];
assert_eq!(decoded.to_tbcd(&mut buffer).unwrap(), 3);
assert_eq!(buffer, [0x21, 0x43, 0xF5]);
}
#[test]
fn the_two_conventions_disagree_on_the_same_octets() {
let octets = [0x21, 0x43];
assert_eq!(DigitString::from_tbcd(&octets).unwrap().to_string(), "1234");
assert_eq!(DigitString::from_bcd(&octets).unwrap().to_string(), "2143");
}
#[test]
fn tbcd_round_trips_at_every_length() {
for length in 1..=DigitString::MAX_DIGITS {
let text: String = (0..length)
.map(|i| char::from(b'0' + (i % 10) as u8))
.collect();
let digits: DigitString = text.parse().unwrap();
let mut buffer = [0u8; 10];
let written = digits.to_tbcd(&mut buffer).unwrap();
assert_eq!(written, length.div_ceil(2));
assert_eq!(
DigitString::from_tbcd(&buffer[..written]).unwrap(),
digits,
"length {length}"
);
}
}
#[test]
fn tbcd_rejects_the_extended_values() {
for nibble in 0xA..=0xE {
assert!(
DigitString::from_tbcd(&[nibble]).is_err(),
"low nibble {nibble:#X} must be rejected"
);
assert!(
DigitString::from_tbcd(&[0x01 | (nibble << 4)]).is_err(),
"high nibble {nibble:#X} must be rejected"
);
}
}
#[test]
fn tbcd_rejects_a_misplaced_filler() {
assert!(
DigitString::from_tbcd(&[0x1F]).is_err(),
"filler in a low nibble"
);
assert!(
DigitString::from_tbcd(&[0x2F, 0x43]).is_err(),
"filler before the end"
);
assert_eq!(DigitString::from_tbcd(&[0xF1]).unwrap().to_string(), "1");
assert!(
DigitString::from_tbcd(&[0x21, 0xF3, 0x45]).is_err(),
"high-nibble filler mid-run"
);
assert!(DigitString::from_tbcd(&[0x21, 0xF3]).is_ok());
}
#[test]
fn tbcd_rejects_an_overlong_field_and_a_short_buffer() {
let too_long = [0x12u8; 11]; assert!(DigitString::from_tbcd(&too_long).is_err());
let digits: DigitString = "12345".parse().unwrap();
let mut small = [0u8; 2];
assert!(digits.to_tbcd(&mut small).is_err());
}
#[test]
fn plain_bcd_round_trips() {
let digits: DigitString = "260728100000".parse().unwrap();
let mut buffer = [0u8; 6];
assert_eq!(digits.to_bcd(&mut buffer).unwrap(), 6);
assert_eq!(buffer, [0x26, 0x07, 0x28, 0x10, 0x00, 0x00]);
assert_eq!(DigitString::from_bcd(&buffer).unwrap(), digits);
}
#[test]
fn plain_bcd_refuses_an_odd_digit_count() {
let odd: DigitString = "12345".parse().unwrap();
let mut buffer = [0u8; 3];
assert!(odd.to_bcd(&mut buffer).is_err());
}
#[test]
fn plain_bcd_rejects_a_non_decimal_nibble() {
assert!(DigitString::from_bcd(&[0xA1]).is_err());
assert!(DigitString::from_bcd(&[0x1F]).is_err());
}
#[test]
fn luhn_verification_and_generation_agree() {
for seed in 0u64..200 {
let payload = format!("{:014}", seed * 7_919);
let digits: DigitString = payload.parse().unwrap();
let full = digits.checked_push(digits.luhn_check_digit()).unwrap();
assert!(full.luhn_valid(), "{payload} + check digit must validate");
}
assert!(!DigitString::parse("7", 20).unwrap().luhn_valid());
}
#[test]
fn a_slice_that_does_not_fit_is_empty_rather_than_wrong() {
let digits: DigitString = "12345".parse().unwrap();
assert_eq!(digits.slice(0, 3).to_string(), "123");
assert_eq!(digits.slice(3, 2).to_string(), "45");
assert!(digits.slice(3, 9).is_empty());
}
#[test]
fn to_u64_saturates_rather_than_wrapping() {
let twenty: DigitString = "99999999999999999999".parse().unwrap();
assert_eq!(twenty.to_u64(), u64::MAX);
assert_eq!("262".parse::<DigitString>().unwrap().to_u64(), 262);
assert_eq!("02".parse::<DigitString>().unwrap().to_u64(), 2);
let a: DigitString = "99999999999999999998".parse().unwrap();
let b: DigitString = "99999999999999999999".parse().unwrap();
assert_eq!(a.to_u64(), b.to_u64(), "both saturate");
assert_ne!(a, b, "…but the digit strings are distinct");
}
}