diff --git a/CHANGELOG.md b/CHANGELOG.md index b396690ec..0e41e1e0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## v0.3.1 (2024-02-07) + +**Minor Changes** + +* Updates the documentation to more closely match the changes in v0.3.0 + ## v0.3.0 (2024-02-04) **Major Changes** diff --git a/Cargo.toml b/Cargo.toml index d02433943..445ef954d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "count-digits" -version = "0.3.0" +version = "0.3.1" authors = ["Erik Nordin "] description = "A no-std trait to count the digits of integer types in various number bases." homepage = "https://github.com/nordzilla/count-digits" @@ -19,7 +19,3 @@ paste = "1.0.14" [[bench]] name = "benchmark" harness = false - -[badges] -codecov = { repository = "nordzilla/count-digits", branch = "main", service = "github" } - diff --git a/README.md b/README.md index 0551c9308..7e7751604 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,109 @@ -[![Coverage Status](https://codecov.io/gh/nordzilla/count-digits/branch/main/graph/badge.svg)](https://codecov.io/gh/nordzilla/count-digits) - # count-digits +[![github]](https://github.com/nordzilla/count-digits) +[![crates-io]](https://crates.io/crates/count-digits) +[![docs-rs]](https://docs.rs/count-digits) + +[github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github +[crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust +[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs + +[![license]](https://github.com/nordzilla/count-digits/blob/main/LICENSE) +[![build]](https://github.com/nordzilla/count-digits/commits/main/) +[![codecov]](https://app.codecov.io/gh/nordzilla/count-digits) + +[license]: https://img.shields.io/github/license/nordzilla/count-digits?style=flat-square&color=009050&label=License +[build]: https://img.shields.io/github/actions/workflow/status/nordzilla/count-digits/rust.yml?style=flat-square&logo=github&color=009050&label=Build +[codecov]: https://img.shields.io/codecov/c/github/nordzilla/count-digits?style=flat-square&logo=codecov&color=009050&label=Test+Coverage + +
+ A [no_std](https://docs.rust-embedded.org/book/intro/no-std.html) trait to count the digits of integer types in various number bases. Compatible with all primitive integer types and all non-zero integer types. -#### Examples +```rust +pub trait CountDigits: Copy + Sized { + type Radix; + + /// Returns the count of bits in an integer. + fn count_bits(self) -> u32; + + /// Returns the count of octal digits in an integer. + fn count_octal_digits(self) -> u32; + + /// Returns the count of hexadecimal digits in an integer. + fn count_hex_digits(self) -> u32; + + /// Returns the count of decimal digits in an integer. + fn count_digits(self) -> usize; + + /// Returns the count of digits in an integer for a given radix. + fn count_digits_radix(self, radix: Self::Radix) -> usize; +} +``` + +### Examples ```rust use count_digits::CountDigits; -assert_eq!(16, 0b1111000000001101.count_bits()); -assert_eq!(16, 0b1111000000001101.count_digits_radix(2_u32)); +assert_eq!(16___u32, 0b1111000000001101.count_bits()); +assert_eq!(16_usize, 0b1111000000001101.count_digits_radix(2_u32)); -assert_eq!(06, 0o170015.count_octal_digits()); -assert_eq!(06, 0o170015.count_digits_radix(8_u32)); +assert_eq!(06___u32, 0o170015.count_octal_digits()); +assert_eq!(06_usize, 0o170015.count_digits_radix(8_u32)); -assert_eq!(05, 61453.count_digits()); -assert_eq!(05, 61453.count_digits_radix(10_u32)); +assert_eq!(04___u32, 0xF00D.count_hex_digits()); +assert_eq!(04_usize, 0xF00D.count_digits_radix(16_u32)); -assert_eq!(04, 0xF00D.count_hex_digits()); -assert_eq!(04, 0xF00D.count_digits_radix(16_u32)); +assert_eq!(05_usize, 61453.count_digits()); +assert_eq!(05_usize, 61453.count_digits_radix(10_u32)); +``` + +The named functions for which the radix is a power of two ( +[count_bits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_bits), +[count_octal_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_octal_digits), and +[count_hex_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_hex_digits) +) return a [u32](https://doc.rust-lang.org/core/primitive.u32.html) for compatibility with Rust's bit-shifting functions +and constants, which all use [u32](https://doc.rust-lang.org/core/primitive.u32.html) for arguments and return types. + +```rust +assert_eq!(0b1011___u8.count_bits(), u8::BITS - 0b1011___u8.leading_zeros()); +assert_eq!(0b1011__i32.count_bits(), i32::BITS - 0b1011__i32.leading_zeros()); +assert_eq!(0b1011_u128.count_bits(), u128::BITS - 0b1011_u128.leading_zeros()); +``` + +The base-10 [count_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits) +function returns [usize](https://doc.rust-lang.org/core/primitive.usize.html) for compatibility with Rust's formatting macros. + +```rust +let max_digits = [1, 2, 15, 105] + .iter() + .map(CountDigits::count_digits) + .max() + .unwrap(); + +for n in [1, 2, 15, 105] { + assert_eq!(3, format!("{n:0>pad$}", pad = max_digits).len()); +} +``` + +In the case of formatting binary, octal, or hex numbers, the +[count_digits_radix(2 | 8 | 16)](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix) +function can be used to retrieve the desired count directly as a [usize](https://doc.rust-lang.org/core/primitive.usize.html). + +```rust +let max_bits = [0b1, 0b10, 0b101, 0b1011] + .iter() + .map(|n| n.count_digits_radix(2u32)) + .max() + .unwrap(); + +for n in [0b1, 0b10, 0b101, 0b1011] { + assert_eq!(4, format!("{n:0>pad$}", pad = max_bits).len()); +} ``` --- @@ -61,19 +142,6 @@ and the digit count of a positive number will _not_ be the same as the count of its negated value. ```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!( @@ -101,7 +169,7 @@ assert_eq!(1, format!("{ }", -1_i8).strip_prefix('-').unwrap().chars().count()) assert_eq!(2, format!("{:x}", -1_i8).chars().count()); ``` -#### Benchmarks +### Benchmarks * [table](https://nordzilla.github.io/count-digits) * [count_bits()](https://nordzilla.github.io/count-digits/count_bits/report/index.html) diff --git a/src/lib.rs b/src/lib.rs index 215493ade..d274db5f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,27 +1,113 @@ #![cfg_attr(not(test), no_std)] #![allow(clippy::zero_prefixed_literal)] +//! [![github]](https://github.com/nordzilla/count-digits) +//! [![crates-io]](https://crates.io/crates/count-digits) +//! [![docs-rs]](https://docs.rs/count-digits) +//! +//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github +//! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust +//! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs +//! +//! [![license]](https://github.com/nordzilla/count-digits/blob/main/LICENSE) +//! [![build]](https://github.com/nordzilla/count-digits/commits/main/) +//! [![codecov]](https://app.codecov.io/gh/nordzilla/count-digits) +//! +//! [license]: https://img.shields.io/github/license/nordzilla/count-digits?style=flat-square&color=009050&label=License +//! [build]: https://img.shields.io/github/actions/workflow/status/nordzilla/count-digits/rust.yml?style=flat-square&logo=github&color=009050&label=Build +//! [codecov]: https://img.shields.io/codecov/c/github/nordzilla/count-digits?style=flat-square&logo=codecov&color=009050&label=Test+Coverage +//! +//!
+//! //! A [no_std](https://docs.rust-embedded.org/book/intro/no-std.html) trait to count //! the digits of integer types in various number bases. //! //! Compatible with all primitive integer types and all non-zero integer types. //! -//! ### Examples +//! ```rust +//! pub trait CountDigits: Copy + Sized { +//! type Radix; +//! +//! /// Returns the count of bits in an integer. +//! fn count_bits(self) -> u32; +//! +//! /// Returns the count of octal digits in an integer. +//! fn count_octal_digits(self) -> u32; +//! +//! /// Returns the count of hexadecimal digits in an integer. +//! fn count_hex_digits(self) -> u32; +//! +//! /// Returns the count of decimal digits in an integer. +//! fn count_digits(self) -> usize; +//! +//! /// Returns the count of digits in an integer for a given radix. +//! fn count_digits_radix(self, radix: Self::Radix) -> usize; +//! } +//! ``` +//! +//! ## Examples //! //! ```rust //! use count_digits::CountDigits; //! # use core::num::NonZeroIsize; //! -//! assert_eq!(16, 0b1111000000001101.count_bits()); -//! assert_eq!(16, 0b1111000000001101.count_digits_radix(2_u32)); +//! assert_eq!(16___u32, 0b1111000000001101.count_bits()); +//! assert_eq!(16_usize, 0b1111000000001101.count_digits_radix(2_u32)); +//! +//! assert_eq!(06___u32, 0o170015.count_octal_digits()); +//! assert_eq!(06_usize, 0o170015.count_digits_radix(8_u32)); +//! +//! assert_eq!(04___u32, 0xF00D.count_hex_digits()); +//! assert_eq!(04_usize, 0xF00D.count_digits_radix(16_u32)); //! -//! assert_eq!(06, 0o170015.count_octal_digits()); -//! assert_eq!(06, 0o170015.count_digits_radix(8_u32)); +//! assert_eq!(05_usize, 61453.count_digits()); +//! assert_eq!(05_usize, 61453.count_digits_radix(10_u32)); +//! ``` +//! +//! The named functions for which the radix is a power of two ( +//! [count_bits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_bits), +//! [count_octal_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_octal_digits), and +//! [count_hex_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_hex_digits) +//! ) return a [u32](https://doc.rust-lang.org/core/primitive.u32.html) for compatibility with Rust's bit-shifting functions +//! and constants, which all use [u32](https://doc.rust-lang.org/core/primitive.u32.html) for arguments and return types. +//! +//! ```rust +//! # use count_digits::CountDigits; +//! assert_eq!(0b1011___u8.count_bits(), u8::BITS - 0b1011___u8.leading_zeros()); +//! assert_eq!(0b1011__i32.count_bits(), i32::BITS - 0b1011__i32.leading_zeros()); +//! assert_eq!(0b1011_u128.count_bits(), u128::BITS - 0b1011_u128.leading_zeros()); +//! ``` +//! +//! The base-10 [count_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits) +//! function returns [usize](https://doc.rust-lang.org/core/primitive.usize.html) for compatibility with Rust's formatting macros. +//! +//! ```rust +//! # use count_digits::CountDigits; +//! let max_digits = [1, 2, 15, 105] +//! .iter() +//! .map(CountDigits::count_digits) +//! .max() +//! .unwrap(); +//! +//! for n in [1, 2, 15, 105] { +//! assert_eq!(3, format!("{n:0>pad$}", pad = max_digits).len()); +//! } +//! ``` +//! +//! In the case of formatting binary, octal, or hex numbers, the +//! [count_digits_radix(2 | 8 | 16)](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix) +//! function can be used to retrieve the desired count directly as a [usize](https://doc.rust-lang.org/core/primitive.usize.html). //! -//! assert_eq!(05, 61453.count_digits()); -//! assert_eq!(05, 61453.count_digits_radix(10_u32)); +//! ```rust +//! # use count_digits::CountDigits; +//! let max_bits = [0b1, 0b10, 0b101, 0b1011] +//! .iter() +//! .map(|n| n.count_digits_radix(2u32)) +//! .max() +//! .unwrap(); //! -//! assert_eq!(04, 0xF00D.count_hex_digits()); -//! assert_eq!(04, 0xF00D.count_digits_radix(16_u32)); +//! for n in [0b1, 0b10, 0b101, 0b1011] { +//! assert_eq!(4, format!("{n:0>pad$}", pad = max_bits).len()); +//! } //! ``` //! //! --- @@ -70,19 +156,6 @@ //! //! ```rust //! # use count_digits::CountDigits; -//! 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!( @@ -111,7 +184,7 @@ //! assert_eq!(2, format!("{:x}", -1_i8).chars().count()); //! ``` //! -//! ### Benchmarks +//! ## Benchmarks //! //! * [table](https://nordzilla.github.io/count-digits) //! * [count_bits()](https://nordzilla.github.io/count-digits/count_bits/report/index.html) @@ -132,7 +205,7 @@ pub trait CountDigits: Copy + Sized { /// and is equal to the corresponding primitive type for non-zero types. type Radix; - /// Returns the count of bits in an integer starting with the first non-zero bit. + /// Returns the count of bits in an integer. /// /// # Examples /// @@ -241,7 +314,7 @@ pub trait CountDigits: Copy + Sized { /// ``` fn count_bits(self) -> u32; - /// Returns the count of octal digits in an integer starting with the first non-zero digit. + /// Returns the count of octal digits in an integer. /// /// # Examples /// @@ -350,6 +423,115 @@ pub trait CountDigits: Copy + Sized { /// ``` fn count_octal_digits(self) -> u32; + /// Returns the count of hexadecimal digits in an integer. + /// + /// # 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!(02, i8::MIN.count_hex_digits()); + /// assert_eq!(02, i8::MAX.count_hex_digits()); + /// assert_eq!(02, NonZeroI8::MIN.count_hex_digits()); + /// assert_eq!(02, NonZeroI8::MAX.count_hex_digits()); + /// + /// assert_eq!(01, u8::MIN.count_hex_digits()); + /// assert_eq!(02, u8::MAX.count_hex_digits()); + /// assert_eq!(01, NonZeroU8::MIN.count_hex_digits()); + /// assert_eq!(02, NonZeroU8::MAX.count_hex_digits()); + /// + /// assert_eq!(04, i16::MIN.count_hex_digits()); + /// assert_eq!(04, i16::MAX.count_hex_digits()); + /// assert_eq!(04, NonZeroI16::MIN.count_hex_digits()); + /// assert_eq!(04, NonZeroI16::MAX.count_hex_digits()); + /// + /// assert_eq!(01, u16::MIN.count_hex_digits()); + /// assert_eq!(04, u16::MAX.count_hex_digits()); + /// assert_eq!(01, NonZeroU16::MIN.count_hex_digits()); + /// assert_eq!(04, NonZeroU16::MAX.count_hex_digits()); + /// + /// assert_eq!(08, i32::MIN.count_hex_digits()); + /// assert_eq!(08, i32::MAX.count_hex_digits()); + /// assert_eq!(08, NonZeroI32::MIN.count_hex_digits()); + /// assert_eq!(08, NonZeroI32::MAX.count_hex_digits()); + /// + /// assert_eq!(01, u32::MIN.count_hex_digits()); + /// assert_eq!(08, u32::MAX.count_hex_digits()); + /// assert_eq!(01, NonZeroU32::MIN.count_hex_digits()); + /// assert_eq!(08, NonZeroU32::MAX.count_hex_digits()); + /// + /// assert_eq!(16, i64::MIN.count_hex_digits()); + /// assert_eq!(16, i64::MAX.count_hex_digits()); + /// assert_eq!(16, NonZeroI64::MIN.count_hex_digits()); + /// assert_eq!(16, NonZeroI64::MAX.count_hex_digits()); + /// + /// assert_eq!(01, u64::MIN.count_hex_digits()); + /// assert_eq!(16, u64::MAX.count_hex_digits()); + /// assert_eq!(01, NonZeroU64::MIN.count_hex_digits()); + /// assert_eq!(16, NonZeroU64::MAX.count_hex_digits()); + /// + /// assert_eq!(32, i128::MIN.count_hex_digits()); + /// assert_eq!(32, i128::MAX.count_hex_digits()); + /// assert_eq!(32, NonZeroI128::MIN.count_hex_digits()); + /// assert_eq!(32, NonZeroI128::MAX.count_hex_digits()); + /// + /// assert_eq!(01, u128::MIN.count_hex_digits()); + /// assert_eq!(32, u128::MAX.count_hex_digits()); + /// assert_eq!(01, NonZeroU128::MIN.count_hex_digits()); + /// assert_eq!(32, NonZeroU128::MAX.count_hex_digits()); + /// + /// #[cfg(target_pointer_width = "64")] { + /// assert_eq!(isize::MIN.count_hex_digits(), i64::MIN.count_hex_digits()); + /// assert_eq!(isize::MAX.count_hex_digits(), i64::MAX.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI64::MIN.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI64::MAX.count_hex_digits()); + /// + /// assert_eq!(usize::MIN.count_hex_digits(), u64::MIN.count_hex_digits()); + /// assert_eq!(usize::MAX.count_hex_digits(), u64::MAX.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU64::MIN.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU64::MAX.count_hex_digits()); + /// } + /// + /// #[cfg(target_pointer_width = "32")] { + /// assert_eq!(isize::MIN.count_hex_digits(), i32::MIN.count_hex_digits()); + /// assert_eq!(isize::MAX.count_hex_digits(), i32::MAX.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI32::MIN.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI32::MAX.count_hex_digits()); + /// + /// assert_eq!(usize::MIN.count_hex_digits(), u32::MIN.count_hex_digits()); + /// assert_eq!(usize::MAX.count_hex_digits(), u32::MAX.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU32::MIN.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU32::MAX.count_hex_digits()); + /// } + /// + /// #[cfg(target_pointer_width = "16")] { + /// assert_eq!(isize::MIN.count_hex_digits(), i16::MIN.count_hex_digits()); + /// assert_eq!(isize::MAX.count_hex_digits(), i16::MAX.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI16::MIN.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI16::MAX.count_hex_digits()); + /// + /// assert_eq!(usize::MIN.count_hex_digits(), u16::MIN.count_hex_digits()); + /// assert_eq!(usize::MAX.count_hex_digits(), u16::MAX.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU16::MIN.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU16::MAX.count_hex_digits()); + /// } + /// + /// #[cfg(target_pointer_width = "8")] { + /// assert_eq!(isize::MIN.count_hex_digits(), i8::MIN.count_hex_digits()); + /// assert_eq!(isize::MAX.count_hex_digits(), i8::MAX.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI8::MIN.count_hex_digits()); + /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI8::MAX.count_hex_digits()); + /// + /// assert_eq!(usize::MIN.count_hex_digits(), u8::MIN.count_hex_digits()); + /// assert_eq!(usize::MAX.count_hex_digits(), u8::MAX.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU8::MIN.count_hex_digits()); + /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU8::MAX.count_hex_digits()); + /// } + /// ``` + fn count_hex_digits(self) -> u32; + /// Returns the count of decimal digits in an integer. /// ///
@@ -463,115 +645,6 @@ pub trait CountDigits: Copy + Sized { /// ``` fn count_digits(self) -> usize; - /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. - /// - /// # 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!(02, i8::MIN.count_hex_digits()); - /// assert_eq!(02, i8::MAX.count_hex_digits()); - /// assert_eq!(02, NonZeroI8::MIN.count_hex_digits()); - /// assert_eq!(02, NonZeroI8::MAX.count_hex_digits()); - /// - /// assert_eq!(01, u8::MIN.count_hex_digits()); - /// assert_eq!(02, u8::MAX.count_hex_digits()); - /// assert_eq!(01, NonZeroU8::MIN.count_hex_digits()); - /// assert_eq!(02, NonZeroU8::MAX.count_hex_digits()); - /// - /// assert_eq!(04, i16::MIN.count_hex_digits()); - /// assert_eq!(04, i16::MAX.count_hex_digits()); - /// assert_eq!(04, NonZeroI16::MIN.count_hex_digits()); - /// assert_eq!(04, NonZeroI16::MAX.count_hex_digits()); - /// - /// assert_eq!(01, u16::MIN.count_hex_digits()); - /// assert_eq!(04, u16::MAX.count_hex_digits()); - /// assert_eq!(01, NonZeroU16::MIN.count_hex_digits()); - /// assert_eq!(04, NonZeroU16::MAX.count_hex_digits()); - /// - /// assert_eq!(08, i32::MIN.count_hex_digits()); - /// assert_eq!(08, i32::MAX.count_hex_digits()); - /// assert_eq!(08, NonZeroI32::MIN.count_hex_digits()); - /// assert_eq!(08, NonZeroI32::MAX.count_hex_digits()); - /// - /// assert_eq!(01, u32::MIN.count_hex_digits()); - /// assert_eq!(08, u32::MAX.count_hex_digits()); - /// assert_eq!(01, NonZeroU32::MIN.count_hex_digits()); - /// assert_eq!(08, NonZeroU32::MAX.count_hex_digits()); - /// - /// assert_eq!(16, i64::MIN.count_hex_digits()); - /// assert_eq!(16, i64::MAX.count_hex_digits()); - /// assert_eq!(16, NonZeroI64::MIN.count_hex_digits()); - /// assert_eq!(16, NonZeroI64::MAX.count_hex_digits()); - /// - /// assert_eq!(01, u64::MIN.count_hex_digits()); - /// assert_eq!(16, u64::MAX.count_hex_digits()); - /// assert_eq!(01, NonZeroU64::MIN.count_hex_digits()); - /// assert_eq!(16, NonZeroU64::MAX.count_hex_digits()); - /// - /// assert_eq!(32, i128::MIN.count_hex_digits()); - /// assert_eq!(32, i128::MAX.count_hex_digits()); - /// assert_eq!(32, NonZeroI128::MIN.count_hex_digits()); - /// assert_eq!(32, NonZeroI128::MAX.count_hex_digits()); - /// - /// assert_eq!(01, u128::MIN.count_hex_digits()); - /// assert_eq!(32, u128::MAX.count_hex_digits()); - /// assert_eq!(01, NonZeroU128::MIN.count_hex_digits()); - /// assert_eq!(32, NonZeroU128::MAX.count_hex_digits()); - /// - /// #[cfg(target_pointer_width = "64")] { - /// assert_eq!(isize::MIN.count_hex_digits(), i64::MIN.count_hex_digits()); - /// assert_eq!(isize::MAX.count_hex_digits(), i64::MAX.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI64::MIN.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI64::MAX.count_hex_digits()); - /// - /// assert_eq!(usize::MIN.count_hex_digits(), u64::MIN.count_hex_digits()); - /// assert_eq!(usize::MAX.count_hex_digits(), u64::MAX.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU64::MIN.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU64::MAX.count_hex_digits()); - /// } - /// - /// #[cfg(target_pointer_width = "32")] { - /// assert_eq!(isize::MIN.count_hex_digits(), i32::MIN.count_hex_digits()); - /// assert_eq!(isize::MAX.count_hex_digits(), i32::MAX.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI32::MIN.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI32::MAX.count_hex_digits()); - /// - /// assert_eq!(usize::MIN.count_hex_digits(), u32::MIN.count_hex_digits()); - /// assert_eq!(usize::MAX.count_hex_digits(), u32::MAX.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU32::MIN.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU32::MAX.count_hex_digits()); - /// } - /// - /// #[cfg(target_pointer_width = "16")] { - /// assert_eq!(isize::MIN.count_hex_digits(), i16::MIN.count_hex_digits()); - /// assert_eq!(isize::MAX.count_hex_digits(), i16::MAX.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI16::MIN.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI16::MAX.count_hex_digits()); - /// - /// assert_eq!(usize::MIN.count_hex_digits(), u16::MIN.count_hex_digits()); - /// assert_eq!(usize::MAX.count_hex_digits(), u16::MAX.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU16::MIN.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU16::MAX.count_hex_digits()); - /// } - /// - /// #[cfg(target_pointer_width = "8")] { - /// assert_eq!(isize::MIN.count_hex_digits(), i8::MIN.count_hex_digits()); - /// assert_eq!(isize::MAX.count_hex_digits(), i8::MAX.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MIN.count_hex_digits(), NonZeroI8::MIN.count_hex_digits()); - /// assert_eq!(NonZeroIsize::MAX.count_hex_digits(), NonZeroI8::MAX.count_hex_digits()); - /// - /// assert_eq!(usize::MIN.count_hex_digits(), u8::MIN.count_hex_digits()); - /// assert_eq!(usize::MAX.count_hex_digits(), u8::MAX.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MIN.count_hex_digits(), NonZeroU8::MIN.count_hex_digits()); - /// assert_eq!(NonZeroUsize::MAX.count_hex_digits(), NonZeroU8::MAX.count_hex_digits()); - /// } - /// ``` - fn count_hex_digits(self) -> u32; - /// Returns the count of digits in an integer as interpreted with the given [radix](https://en.wikipedia.org/wiki/Radix). /// ///
@@ -609,7 +682,7 @@ macro_rules! impl_count_digits { type Radix = $radix_type; #[inline(always)] - /// Returns the count of bits in an integer starting with the first non-zero bit. + /// Returns the count of bits in an integer. fn count_bits(self) -> u32 { if self.is_negative() { $min_value_bits @@ -619,7 +692,7 @@ macro_rules! impl_count_digits { } #[inline(always)] - /// Returns the count of octal digits in an integer starting with the first non-zero digit. + /// Returns the count of octal digits in an integer. fn count_octal_digits(self) -> u32 { if self.is_negative() { $min_value_octal_digits @@ -629,7 +702,7 @@ macro_rules! impl_count_digits { } #[inline(always)] - /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. + /// Returns the count of hexadecimal digits in an integer. fn count_hex_digits(self) -> u32 { if self.is_negative() { $min_value_hex_digits @@ -667,7 +740,7 @@ macro_rules! impl_count_digits { type Radix = $radix_type; #[inline(always)] - /// Returns the count of bits in an integer starting with the first non-zero bit. + /// Returns the count of bits in an integer. fn count_bits(self) -> u32 { if self.is_negative() { $min_value_bits @@ -677,7 +750,7 @@ macro_rules! impl_count_digits { } #[inline(always)] - /// Returns the count of octal digits in an integer starting with the first non-zero digit. + /// Returns the count of octal digits in an integer. fn count_octal_digits(self) -> u32 { if self.is_negative() { $min_value_octal_digits @@ -687,7 +760,7 @@ macro_rules! impl_count_digits { } #[inline(always)] - /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. + /// Returns the count of hexadecimal digits in an integer. fn count_hex_digits(self) -> u32 { if self.is_negative() { $min_value_hex_digits @@ -729,19 +802,19 @@ macro_rules! impl_count_digits { type Radix = $primitive_type; #[inline(always)] - /// Returns the count of bits in an integer starting with the first non-zero bit. + /// Returns the count of bits in an integer. fn count_bits(self) -> u32 { 1 + self.checked_ilog2().unwrap_or_default() } #[inline(always)] - /// Returns the count of octal digits in an integer starting with the first non-zero digit. + /// Returns the count of octal digits in an integer. fn count_octal_digits(self) -> u32 { 1 + self.checked_ilog2().unwrap_or_default() / 3 } #[inline(always)] - /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. + /// Returns the count of hexadecimal digits in an integer. fn count_hex_digits(self) -> u32 { 1 + self.checked_ilog2().unwrap_or_default() / 4 } @@ -769,19 +842,19 @@ macro_rules! impl_count_digits { type Radix = $primitive_type; #[inline(always)] - /// Returns the count of bits in an integer starting with the first non-zero bit. + /// Returns the count of bits in an integer. fn count_bits(self) -> u32 { 1 + self.ilog2() } #[inline(always)] - /// Returns the count of octal digits in an integer starting with the first non-zero digit. + /// Returns the count of octal digits in an integer. fn count_octal_digits(self) -> u32 { 1 + self.get().ilog2() / 3 } #[inline(always)] - /// Returns the count of hexadecimal digits in an integer starting with the first non-zero digit. + /// Returns the count of hexadecimal digits in an integer. fn count_hex_digits(self) -> u32 { 1 + self.get().ilog2() / 4 }