Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add counts for different number bases #2

Merged
merged 12 commits into from
Jan 24, 2024
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ keywords = ["decimal", "digit", "count", "integer", "trait"]
categories = [ "algorithms", "mathematics", "no-std" ]
license = "MIT"
edition = "2021"
rust-version = "1.67.1"
rust-version = "1.71.1"

[dev-dependencies]
paste = "1.0.14"
Expand Down
164 changes: 67 additions & 97 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,109 +1,79 @@
# CountDigits
A Rust trait to count the decimal digits of integers
# count-digits

---
## CountDigits

A [no_std](https://docs.rust-embedded.org/book/intro/no-std.html) trait to count
the digits of an integer in various number bases.

Compatible with all primitive integer types and all non-zero integer types.

##### Examples
```rust
use count_digits::CountDigits;
use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};

assert_eq!(03, i8::MIN.count_digits());
assert_eq!(03, i8::MAX.count_digits());
assert_eq!(03, NonZeroI8::MIN.count_digits());
assert_eq!(03, NonZeroI8::MAX.count_digits());

assert_eq!(01, u8::MIN.count_digits());
assert_eq!(03, u8::MAX.count_digits());
assert_eq!(01, NonZeroU8::MIN.count_digits());
assert_eq!(03, NonZeroU8::MAX.count_digits());

assert_eq!(05, i16::MIN.count_digits());
assert_eq!(05, i16::MAX.count_digits());
assert_eq!(05, NonZeroI16::MIN.count_digits());
assert_eq!(05, NonZeroI16::MAX.count_digits());

assert_eq!(01, u16::MIN.count_digits());
assert_eq!(05, u16::MAX.count_digits());
assert_eq!(01, NonZeroU16::MIN.count_digits());
assert_eq!(05, NonZeroU16::MAX.count_digits());

assert_eq!(10, i32::MIN.count_digits());
assert_eq!(10, i32::MAX.count_digits());
assert_eq!(10, NonZeroI32::MIN.count_digits());
assert_eq!(10, NonZeroI32::MAX.count_digits());

assert_eq!(01, u32::MIN.count_digits());
assert_eq!(10, u32::MAX.count_digits());
assert_eq!(01, NonZeroU32::MIN.count_digits());
assert_eq!(10, NonZeroU32::MAX.count_digits());

assert_eq!(19, i64::MIN.count_digits());
assert_eq!(19, i64::MAX.count_digits());
assert_eq!(19, NonZeroI64::MIN.count_digits());
assert_eq!(19, NonZeroI64::MAX.count_digits());

assert_eq!(01, u64::MIN.count_digits());
assert_eq!(20, u64::MAX.count_digits());
assert_eq!(01, NonZeroU64::MIN.count_digits());
assert_eq!(20, NonZeroU64::MAX.count_digits());

assert_eq!(39, i128::MIN.count_digits());
assert_eq!(39, i128::MAX.count_digits());
assert_eq!(39, NonZeroI128::MIN.count_digits());
assert_eq!(39, NonZeroI128::MAX.count_digits());

assert_eq!(01, u128::MIN.count_digits());
assert_eq!(39, u128::MAX.count_digits());
assert_eq!(01, NonZeroU128::MIN.count_digits());
assert_eq!(39, NonZeroU128::MAX.count_digits());

#[cfg(target_pointer_width = "64")] {
assert_eq!(isize::MIN.count_digits(), i64::MIN.count_digits());
assert_eq!(isize::MAX.count_digits(), i64::MAX.count_digits());
assert_eq!(NonZeroIsize::MIN.count_digits(), NonZeroI64::MIN.count_digits());
assert_eq!(NonZeroIsize::MAX.count_digits(), NonZeroI64::MAX.count_digits());

assert_eq!(usize::MIN.count_digits(), u64::MIN.count_digits());
assert_eq!(usize::MAX.count_digits(), u64::MAX.count_digits());
assert_eq!(NonZeroUsize::MIN.count_digits(), NonZeroU64::MIN.count_digits());
assert_eq!(NonZeroUsize::MAX.count_digits(), NonZeroU64::MAX.count_digits());
}
use core::num::NonZeroIsize;

#[cfg(target_pointer_width = "32")] {
assert_eq!(isize::MIN.count_digits(), i32::MIN.count_digits());
assert_eq!(isize::MAX.count_digits(), i32::MAX.count_digits());
assert_eq!(NonZeroIsize::MIN.count_digits(), NonZeroI32::MIN.count_digits());
assert_eq!(NonZeroIsize::MAX.count_digits(), NonZeroI32::MAX.count_digits());
assert_eq!(16, 0b1111000000001101.count_bits());
assert_eq!(16, 0b1111000000001101.count_digits_radix(2_u32));

assert_eq!(usize::MIN.count_digits(), u32::MIN.count_digits());
assert_eq!(usize::MAX.count_digits(), u32::MAX.count_digits());
assert_eq!(NonZeroUsize::MIN.count_digits(), NonZeroU32::MIN.count_digits());
assert_eq!(NonZeroUsize::MAX.count_digits(), NonZeroU32::MAX.count_digits());
}
assert_eq!(06, 0o170015.count_octal_digits());
assert_eq!(06, 0o170015.count_digits_radix(8_u32));

#[cfg(target_pointer_width = "16")] {
assert_eq!(isize::MIN.count_digits(), i16::MIN.count_digits());
assert_eq!(isize::MAX.count_digits(), i16::MAX.count_digits());
assert_eq!(NonZeroIsize::MIN.count_digits(), NonZeroI16::MIN.count_digits());
assert_eq!(NonZeroIsize::MAX.count_digits(), NonZeroI16::MAX.count_digits());
assert_eq!(05, 61453.count_digits());
assert_eq!(05, 61453.count_digits_radix(10_u32));

assert_eq!(usize::MIN.count_digits(), u16::MIN.count_digits());
assert_eq!(usize::MAX.count_digits(), u16::MAX.count_digits());
assert_eq!(NonZeroUsize::MIN.count_digits(), NonZeroU16::MIN.count_digits());
assert_eq!(NonZeroUsize::MAX.count_digits(), NonZeroU16::MAX.count_digits());
}
assert_eq!(04, 0xF00D.count_hex_digits());
assert_eq!(04, 0xF00D.count_digits_radix(16_u32));

assert_eq!(
0xF00D_isize.count_digits(),
NonZeroIsize::new(0xF00D).unwrap().count_digits(),
);
```

#[cfg(target_pointer_width = "8")] {
assert_eq!(isize::MIN.count_digits(), i8::MIN.count_digits());
assert_eq!(isize::MAX.count_digits(), i8::MAX.count_digits());
assert_eq!(NonZeroIsize::MIN.count_digits(), NonZeroI8::MIN.count_digits());
assert_eq!(NonZeroIsize::MAX.count_digits(), NonZeroI8::MAX.count_digits());
###### Note

assert_eq!(usize::MIN.count_digits(), u8::MIN.count_digits());
assert_eq!(usize::MAX.count_digits(), u8::MAX.count_digits());
assert_eq!(NonZeroUsize::MIN.count_digits(), NonZeroU8::MIN.count_digits());
assert_eq!(NonZeroUsize::MAX.count_digits(), NonZeroU8::MAX.count_digits());
Be mindful with negative signed integers in non-decimal number bases.

Since negative decimal numbers are represented with a negative sign,
the decimal digit count of a negative number will be equal to its
positive counterpart.

```rust
assert_eq!(
0xF00D_i32.count_digits(),
0xF00D_i32.wrapping_neg().count_digits(),
);
````

However, a negative number using a different radix will not have the
same count of digits as its positive counterpart.

```rust
assert_ne!(
0xBAD_i32.count_bits(),
0xBAD_i32.wrapping_neg().count_bits(),
);
assert_ne!(
0xBAD_i32.count_octal_digits(),
0xBAD_i32.wrapping_neg().count_octal_digits(),
);
assert_ne!(
0xBAD_i32.count_hex_digits(),
0xBAD_i32.wrapping_neg().count_hex_digits(),
);

for radix in 2..=16 {
match radix {
10 => assert_eq!(
0xF00D_i32.count_digits_radix(radix),
0xF00D_i32.wrapping_neg().count_digits_radix(radix),
),
_ => assert_ne!(
0xBAD_i32.count_digits_radix(radix),
0xBAD_i32.wrapping_neg().count_digits_radix(radix),
),
}
}
```
````

License: MIT
Loading