pub trait Dollcode {
fn from_dollcode(dollcode: &str) -> Option<Self>
where
Self: Sized;
fn to_dollcode(self) -> String;
}
macro_rules! impl_std_Dollcode {
(for $($t:ty),+) => {
$(impl Dollcode for $t {
fn from_dollcode(input: &str) -> Option<Self> {
let mut acc = 0;
for char in input.chars() {
acc = acc * 3
+ match char {
'▖' => 1,
'▘' => 2,
'▌' => 3,
_ => return None,
}
}
Some(acc)
}
fn to_dollcode(mut self) -> String {
let mut res = String::new();
while self > 0 {
self -= 1;
res.push(['▖', '▘', '▌'][(self % 3) as usize]);
self /= 3;
}
res.chars().rev().collect::<String>()
}
})*
}
}
impl_std_Dollcode!(for u8);
impl_std_Dollcode!(for u16);
impl_std_Dollcode!(for u32);
impl_std_Dollcode!(for u64);
impl_std_Dollcode!(for u128);
impl_std_Dollcode!(for usize);
#[cfg(feature = "num")]
use num::{BigUint, ToPrimitive};
#[cfg(feature = "num")]
impl Dollcode for BigUint {
fn from_dollcode(input: &str) -> Option<Self> {
let mut acc = BigUint::ZERO;
for char in input.chars() {
acc = acc * 3_u32
+ match char {
'▖' => 1_u32,
'▘' => 2_u32,
'▌' => 3_u32,
_ => return None,
}
}
Some(acc)
}
fn to_dollcode(mut self) -> String {
let mut res = String::new();
while self > BigUint::ZERO {
self -= 1_u32;
res.push(
['▖', '▘', '▌'][(self.clone() % 3_u32)
.to_usize()
.expect("failed to convert value to usize")],
);
self /= 3_u32;
}
res.chars().rev().collect::<String>()
}
}
#[cfg(test)]
mod tests {
use crate::Dollcode;
#[test]
fn std_encode() {
assert_eq!(0_u32.to_dollcode(), "");
assert_eq!(1_u32.to_dollcode(), "▖");
assert_eq!(2_u32.to_dollcode(), "▘");
assert_eq!(3_u32.to_dollcode(), "▌");
assert_eq!(4_u32.to_dollcode(), "▖▖");
assert_eq!(13_u32.to_dollcode(), "▖▖▖");
assert_eq!(440729_u32.to_dollcode(), "▖▌▌▌▌▖▖▖▘▌▘▘");
assert_eq!(893271_u32.to_dollcode(), "▖▖▘▘▌▘▌▌▘▘▘▌▌");
assert_eq!(
u64::MAX.to_dollcode(),
"▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"
);
}
#[test]
fn std_decode() {
assert_eq!(u32::from_dollcode("invalid"), None);
assert_eq!(u32::from_dollcode("▖▌▌▌▌▖▖▖▘invalid"), None);
assert_eq!(u32::from_dollcode(""), Some(0));
assert_eq!(u32::from_dollcode("▖"), Some(1));
assert_eq!(u32::from_dollcode("▘"), Some(2));
assert_eq!(u32::from_dollcode("▌"), Some(3));
assert_eq!(u32::from_dollcode("▖▖"), Some(4));
assert_eq!(u32::from_dollcode("▖▖▖"), Some(13));
assert_eq!(u32::from_dollcode("▖▌▌▌▌▖▖▖▘▌▘▘"), Some(440729));
assert_eq!(u32::from_dollcode("▖▖▘▘▌▘▌▌▘▘▘▌▌"), Some(893271));
assert_eq!(
u64::from_dollcode("▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"),
Some(u64::MAX)
);
}
#[test]
#[cfg(feature = "num")]
fn big_int_encode() {
use num::BigUint;
assert_eq!(BigUint::ZERO.to_dollcode(), "");
assert_eq!(
BigUint::from_slice(&[893271]).to_dollcode(),
"▖▖▘▘▌▘▌▌▘▘▘▌▌"
);
assert_eq!(
BigUint::from_slice(&[u32::MAX, u32::MAX, u32::MAX]).to_dollcode(),
"▖▘▖▖▌▌▘▌▌▌▘▌▘▖▌▘▌▘▌▌▖▌▖▘▘▖▘▖▘▖▘▌▌▖▌▖▖▌▖▖▖▘▖▌▖▖▘▌▌▘▌▘▘▘▘▖▘▌▌▘▌"
);
}
#[test]
#[cfg(feature = "num")]
fn big_int_decode() {
use num::BigUint;
assert_eq!(BigUint::from_dollcode("invalid"), None);
assert_eq!(BigUint::from_dollcode(""), Some(BigUint::ZERO));
assert_eq!(
BigUint::from_dollcode("▖▖▘▘▌▘▌▌▘▘▘▌▌"),
Some(BigUint::from_slice(&[893271]))
);
assert_eq!(
BigUint::from_dollcode("▖▘▖▖▌▌▘▌▌▌▘▌▘▖▌▘▌▘▌▌▖▌▖▘▘▖▘▖▘▖▘▌▌▖▌▖▖▌▖▖▖▘▖▌▖▖▘▌▌▘▌▘▘▘▘▖▘▌▌▘▌"),
Some(BigUint::from_slice(&[u32::MAX, u32::MAX, u32::MAX]))
);
}
}
#[deprecated(since = "0.2.0", note = "please use the `Dollcode` trait instead")]
pub fn dollcode_decode(input: &str) -> Option<u64> {
let mut acc = 0;
for char in input.chars() {
acc = acc * 3
+ match char {
'▖' => 1,
'▘' => 2,
'▌' => 3,
_ => return None,
}
}
Some(acc)
}
#[deprecated(since = "0.2.0", note = "please use the `Dollcode` trait instead")]
pub fn dollcode_encode(mut input: u64) -> String {
let mut res = String::new();
while input > 0 {
input -= 1;
res.push(['▖', '▘', '▌'][(input % 3) as usize]);
input /= 3;
}
res.chars().rev().collect::<String>()
}
#[cfg(test)]
mod tests {
use crate::{dollcode_decode, dollcode_encode};
#[test]
fn test_encode() {
assert_eq!(dollcode_encode(0), "");
assert_eq!(dollcode_encode(1), "▖");
assert_eq!(dollcode_encode(2), "▘");
assert_eq!(dollcode_encode(3), "▌");
assert_eq!(dollcode_encode(4), "▖▖");
assert_eq!(dollcode_encode(13), "▖▖▖");
assert_eq!(dollcode_encode(440729), "▖▌▌▌▌▖▖▖▘▌▘▘");
assert_eq!(dollcode_encode(893271), "▖▖▘▘▌▘▌▌▘▘▘▌▌");
assert_eq!(
dollcode_encode(u64::MAX),
"▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"
);
}
#[test]
fn test_decode() {
assert_eq!(dollcode_decode("invalid"), None);
assert_eq!(dollcode_decode("▖▌▌▌▌▖▖▖▘invalid"), None);
assert_eq!(dollcode_decode(""), Some(0));
assert_eq!(dollcode_decode("▖"), Some(1));
assert_eq!(dollcode_decode("▘"), Some(2));
assert_eq!(dollcode_decode("▌"), Some(3));
assert_eq!(dollcode_decode("▖▖"), Some(4));
assert_eq!(dollcode_decode("▖▖▖"), Some(13));
assert_eq!(dollcode_decode("▖▌▌▌▌▖▖▖▘▌▘▘"), Some(440729));
assert_eq!(dollcode_decode("▖▖▘▘▌▘▌▌▘▘▘▌▌"), Some(893271));
assert_eq!(
dollcode_decode("▖▖▖▖▘▘▖▘▌▘▘▖▘▘▖▖▘▌▌▖▖▌▌▌▖▌▖▖▌▖▌▌▖▌▌▘▖▖▘▖▌"),
Some(u64::MAX)
);
}
}