#[cfg(any(not(target_endian = "big"), test))]
#[must_use]
fn parse_digits2_le(digits: [u8; 2]) -> u8 {
let chunk = u16::from_le_bytes(digits);
((chunk & 0x00_0f) * 10 + ((chunk & 0x0f_00) >> 8)) as u8
}
#[cfg(any(target_endian = "big", test))]
#[must_use]
fn parse_digits2_be(digits: [u8; 2]) -> u8 {
let chunk = u16::from_be_bytes(digits);
(((chunk & 0x0f_00) >> 8) * 10 + (chunk & 0x00_0f)) as u8
}
#[inline]
#[must_use]
pub(crate) fn parse_digits2(digits: [u8; 2]) -> u8 {
#[cfg(not(target_endian = "big"))]
{
parse_digits2_le(digits)
}
#[cfg(target_endian = "big")]
{
parse_digits2_be(digits)
}
}
#[cfg(test)]
mod tests {
use super::*;
use core::convert::TryFrom;
#[cfg(feature = "alloc")]
use alloc::format;
fn as_digit2(s: &str) -> [u8; 2] {
TryFrom::try_from(s.as_bytes()).unwrap()
}
#[test]
fn digits2_le() {
#[cfg(not(feature = "alloc"))]
{
assert_eq!(parse_digits2_le(as_digit2("00")), 0);
assert_eq!(parse_digits2_le(as_digit2("01")), 1);
assert_eq!(parse_digits2_le(as_digit2("10")), 10);
assert_eq!(parse_digits2_le(as_digit2("12")), 12);
assert_eq!(parse_digits2_le(as_digit2("55")), 55);
assert_eq!(parse_digits2_le(as_digit2("99")), 99);
}
#[cfg(feature = "alloc")]
{
for i in 0_u8..=99_u8 {
let s = format!("{:02}", i);
assert_eq!(parse_digits2_le(as_digit2(&s)), i);
}
}
}
#[test]
fn digits2_be() {
#[cfg(not(feature = "alloc"))]
{
assert_eq!(parse_digits2_be(as_digit2("00")), 0);
assert_eq!(parse_digits2_be(as_digit2("01")), 1);
assert_eq!(parse_digits2_be(as_digit2("10")), 10);
assert_eq!(parse_digits2_be(as_digit2("12")), 12);
assert_eq!(parse_digits2_be(as_digit2("55")), 55);
assert_eq!(parse_digits2_be(as_digit2("99")), 99);
}
#[cfg(feature = "alloc")]
{
for i in 0_u8..=99_u8 {
let s = format!("{:02}", i);
assert_eq!(parse_digits2_be(as_digit2(&s)), i);
}
}
}
#[test]
fn digits2() {
#[cfg(not(feature = "alloc"))]
{
assert_eq!(parse_digits2(as_digit2("00")), 0);
assert_eq!(parse_digits2(as_digit2("01")), 1);
assert_eq!(parse_digits2(as_digit2("10")), 10);
assert_eq!(parse_digits2(as_digit2("12")), 12);
assert_eq!(parse_digits2(as_digit2("55")), 55);
assert_eq!(parse_digits2(as_digit2("99")), 99);
}
#[cfg(feature = "alloc")]
{
for i in 0_u8..=99_u8 {
let s = format!("{:02}", i);
assert_eq!(parse_digits2(as_digit2(&s)), i);
}
}
}
}