use std::fmt::Write;
use crate::InlineString;
#[macro_export]
macro_rules! sub_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {
if $arg_lhs > $arg_rhs {
$arg_lhs - $arg_rhs
} else {
0
}
};
}
#[macro_export]
macro_rules! add_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {{
type BigInt = u64;
let lhs_big: BigInt = $arg_lhs as BigInt;
let rhs_big: BigInt = $arg_rhs as BigInt;
let sum_big: BigInt = lhs_big + rhs_big;
if sum_big > ChUnitPrimitiveType::MAX as BigInt {
ChUnitPrimitiveType::MAX
} else {
$arg_lhs + $arg_rhs
}
}};
}
#[macro_export]
macro_rules! mul_unsigned {
($arg_lhs: expr, $arg_rhs: expr) => {{
type BigInt = u64;
let lhs_big: BigInt = $arg_lhs as BigInt;
let rhs_big: BigInt = $arg_rhs as BigInt;
let mul_big: BigInt = lhs_big + rhs_big;
if mul_big > ChUnitPrimitiveType::MAX as BigInt {
ChUnitPrimitiveType::MAX
} else {
$arg_lhs * $arg_rhs
}
}};
}
#[macro_export]
macro_rules! inc_unsigned {
($arg_lhs: expr) => {
$arg_lhs = add_unsigned!($arg_lhs, 1);
};
($arg_lhs: expr, by: $arg_amount: expr) => {
$arg_lhs = add_unsigned!($arg_lhs, $arg_amount);
};
($arg_lhs: expr, max: $arg_max: expr) => {
if $arg_lhs >= $arg_max {
$arg_lhs = $arg_max;
} else {
$arg_lhs = add_unsigned!($arg_lhs, 1);
}
};
($arg_lhs: expr, by: $arg_amount:expr, max: $arg_max: expr) => {
if add_unsigned!($arg_lhs, $arg_amount) >= $arg_max {
$arg_lhs = $arg_max;
} else {
$arg_lhs = add_unsigned!($arg_lhs, $arg_amount);
}
};
}
#[macro_export]
macro_rules! dec_unsigned {
($arg_lhs: expr) => {
if $arg_lhs > 1 {
$arg_lhs = sub_unsigned!($arg_lhs, 1);
} else {
$arg_lhs = 0;
}
};
($arg_lhs: expr, by: $arg_amount: expr) => {
$arg_lhs = sub_unsigned!($arg_lhs, $arg_amount);
};
}
pub fn format_as_kilobytes_with_commas(bytes_size: usize) -> InlineString {
if bytes_size < 1024 {
let mut acc = format_with_commas(bytes_size);
_ = write!(acc, " B");
acc
} else {
let kilobytes = bytes_size / 1024;
let mut acc = format_with_commas(kilobytes);
_ = write!(acc, " KB");
acc
}
}
#[test]
fn test_format_as_kilobytes_with_commas() {
{
let bytes_size = 512;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "512 B");
}
{
let bytes_size = 1024;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "1 KB");
}
{
let bytes_size = 2048;
let result = format_as_kilobytes_with_commas(bytes_size);
assert_eq!(result, "2 KB");
}
}
pub fn format_with_commas(num: usize) -> InlineString {
let num_str = num.to_string();
let ir = {
let mut acc = InlineString::with_capacity(num_str.len());
for (count, ch) in num_str.chars().rev().enumerate() {
if count % 3 == 0 && count != 0 {
acc.push(',');
}
acc.push(ch);
}
acc
};
{
let mut acc = InlineString::with_capacity(ir.len());
for ch in ir.chars().rev() {
acc.push(ch);
}
acc
}
}
#[test]
fn test_format_with_commas() {
{
let num = 5;
let result = format_with_commas(num);
assert_eq!(result, "5");
}
{
let num = 12;
let result = format_with_commas(num);
assert_eq!(result, "12");
}
{
let num = 123;
let result = format_with_commas(num);
assert_eq!(result, "123");
}
{
let num = 987654;
let result = format_with_commas(num);
assert_eq!(result, "987,654");
}
{
let num = 123456789;
let result = format_with_commas(num);
assert_eq!(result, "123,456,789");
}
}