{"id":17299302,"url":"https://github.com/nordzilla/count-digits","last_synced_at":"2025-04-14T12:24:48.991Z","repository":{"id":218655636,"uuid":"747002872","full_name":"nordzilla/count-digits","owner":"nordzilla","description":"A no-std trait with functions to determine the lengths of integers in various number bases.","archived":false,"fork":false,"pushed_at":"2024-02-11T18:45:38.000Z","size":141954,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T01:46:40.064Z","etag":null,"topics":["bit-count","count-bits","count-digits","integer","integer-length","length","no-alloc","no-allocation","no-std","nostd","nostdlib","number-length","rust","rust-lang","rust-trait","rust-traits","trait","traits"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/count-digits","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nordzilla.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-23T04:08:38.000Z","updated_at":"2024-06-11T12:04:02.000Z","dependencies_parsed_at":"2025-02-25T09:43:31.763Z","dependency_job_id":null,"html_url":"https://github.com/nordzilla/count-digits","commit_stats":{"total_commits":41,"total_committers":2,"mean_commits":20.5,"dds":0.04878048780487809,"last_synced_commit":"c8e161f49d43875d8e6e0a54ecfb64d9bb793cb0"},"previous_names":["nordzilla/count-digits"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordzilla%2Fcount-digits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordzilla%2Fcount-digits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordzilla%2Fcount-digits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nordzilla%2Fcount-digits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nordzilla","download_url":"https://codeload.github.com/nordzilla/count-digits/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248879253,"owners_count":21176469,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bit-count","count-bits","count-digits","integer","integer-length","length","no-alloc","no-allocation","no-std","nostd","nostdlib","number-length","rust","rust-lang","rust-trait","rust-traits","trait","traits"],"created_at":"2024-10-15T11:21:53.694Z","updated_at":"2025-04-14T12:24:48.945Z","avatar_url":"https://github.com/nordzilla.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# count-digits\n\n[![github]](https://github.com/nordzilla/count-digits)\n[![crates-io]](https://crates.io/crates/count-digits)\n[![docs-rs]](https://docs.rs/count-digits)\n\n[github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge\u0026labelColor=555555\u0026logo=github\n[crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge\u0026labelColor=555555\u0026logo=rust\n[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge\u0026labelColor=555555\u0026logo=docs.rs\n\n[![license]](https://github.com/nordzilla/count-digits/blob/main/LICENSE)\n[![build]](https://github.com/nordzilla/count-digits/commits/main/)\n[![codecov]](https://app.codecov.io/gh/nordzilla/count-digits)\n\n[license]: https://img.shields.io/github/license/nordzilla/count-digits?style=flat-square\u0026color=009050\u0026label=License\n[build]: https://img.shields.io/github/actions/workflow/status/nordzilla/count-digits/rust.yml?style=flat-square\u0026logo=github\u0026color=009050\u0026label=Build\n[codecov]: https://img.shields.io/codecov/c/github/nordzilla/count-digits?style=flat-square\u0026logo=codecov\u0026color=009050\u0026label=Test+Coverage\n\n\u003cbr\u003e\n\n[CountDigits](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html)\nis a [no-std](https://docs.rust-embedded.org/book/intro/no-std.html) trait with functions\nto determine the lengths of integers in various number bases.\n\nIt is [implemented](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#foreign-impls)\nfor all primitive integer types and all non-zero integer types.\n\n```rust\npub trait CountDigits: Copy + Sized {\n    /// The type of integer that should be used for radix arguments.\n    type Radix;\n\n    /// Returns the count of bits in an integer.\n    fn count_bits(self) -\u003e u32;\n\n    /// Returns the count of octal digits in an integer.\n    fn count_octal_digits(self) -\u003e u32;\n\n    /// Returns the count of hexadecimal digits in an integer.\n    fn count_hex_digits(self) -\u003e u32;\n\n    /// Returns the count of decimal digits in an integer.\n    fn count_digits(self) -\u003e usize;\n\n    /// Returns the count of digits in an integer for a given radix.\n    /// Panics if the provided radix is invalid.\n    fn count_digits_radix(self, radix: Self::Radix) -\u003e usize;\n\n    /// Returns the count of digits in an integer for a given radix.\n    /// Returns None if the given radix is invalid.\n    fn checked_count_digits_radix(self, radix: Self::Radix) -\u003e Option\u003cusize\u003e;\n}\n```\n\n### Examples\n\n```rust\nuse count_digits::CountDigits;\n\n// Base 2\nassert_eq!(16, 0b1111000000001101.count_bits());\nassert_eq!(16, 0b1111000000001101.count_digits_radix(2_u32));\n\n// Base 8\nassert_eq!(06, 0o170015.count_octal_digits());\nassert_eq!(06, 0o170015.count_digits_radix(8_u32));\n\n// Base 10\nassert_eq!(05, 61453.count_digits());\nassert_eq!(05, 61453.count_digits_radix(10_u32));\n\n// Base 16\nassert_eq!(04, 0xF00D.count_hex_digits());\nassert_eq!(04, 0xF00D.count_digits_radix(16_u32));\n```\n\n#### Functions That Return u32\n\nNamed functions for which the radix is a power of two return\n[u32](https://doc.rust-lang.org/core/primitive.u32.html) for\ncompatibility with Rust's bitwise functions and constants.\n\n* [count_bits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_bits)\n* [count_octal_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_octal_digits)\n* [count_hex_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_hex_digits)\n\n```rust\nassert_eq!(0b1011___u8.count_bits(),   u8::BITS - 0b1011___u8.leading_zeros());\nassert_eq!(0b1011__i32.count_bits(),  i32::BITS - 0b1011__i32.leading_zeros());\nassert_eq!(0b1011_u128.count_bits(), u128::BITS - 0b1011_u128.leading_zeros());\n```\n\n#### Functions That Return usize\n\nFunctions that are not inherently meaningful in a bitwise context return [usize](https://doc.rust-lang.org/core/primitive.usize.html)\nfor compatibility with Rust's formatting functions and macros.\n\n* [count_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits)\n* [count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix)\n* [checked_count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.checked_count_digits_radix)\n\n```rust\nlet numbers = [2, 3, 13, 103, 1337];\nlet max_digits = numbers\n    .iter()\n    .map(CountDigits::count_digits)\n    .max()\n    .unwrap();\n\nfor n in numbers {\n    assert_eq!(4, format!(\"{n:\u003emax_digits$}\").chars().count());\n}\n```\n\nWhen formatting binary, octal, or hexadecimal numbers, the\n[count_digits_radix(2 | 8 | 16)](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix)\nand [checked_count_digits_radix(2 | 8 | 16)](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.checked_count_digits_radix)\nfunctions can be used in place of [count_bits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_bits),\n[count_octal_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_octal_digits), and\n[count_hex_digits()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_hex_digits)\nto retrieve the desired count directly as a [usize](https://doc.rust-lang.org/core/primitive.usize.html).\n\n```rust\nlet numbers = [0b1, 0b10, 0b101, 0b1011];\nlet max_bits = numbers\n    .iter()\n    .map(|n| n.count_digits_radix(2u32))\n    .max()\n    .unwrap();\n\nfor n in numbers {\n    assert_eq!(4, format!(\"{n:\u003emax_bits$}\").chars().count());\n}\n```\n\n#### Invalid Radix Values\n\nValues passed to [count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix)\nand [checked_count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.checked_count_digits_radix)\nmust be greater than or equal to 2.\n\n* [count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.count_digits_radix)\nwill [panic](https://doc.rust-lang.org/stable/core/macro.panic.html) if given an invalid radix.\n\n```rust\nfor n in 0..100 {\n    assert!(std::panic::catch_unwind(|| n.count_digits_radix(0_u32)).is_err());\n    assert!(std::panic::catch_unwind(|| n.count_digits_radix(1_u32)).is_err());\n}\n```\n\n* [checked_count_digits_radix()](https://docs.rs/count-digits/latest/count_digits/trait.CountDigits.html#tymethod.checked_count_digits_radix)\nwill return [None](https://doc.rust-lang.org/stable/core/option/enum.Option.html#variant.None) if given an invalid radix.\n\n```rust\nfor n in 0..100 {\n    assert!(n.checked_count_digits_radix(0_u32).is_none());\n    assert!(n.checked_count_digits_radix(1_u32).is_none());\n}\n```\n\n#### Negative Numbers\n\nSince negative numbers represented in base 10 are displayed with a negative sign,\nthe base-10 digit count of a positive number will be equal to the base-10 digit count\nof the number's negated value, assuming no wrapping occurs.\n\n```rust\nassert_eq!(\n    867_5309_i32.count_digits(),\n    867_5309_i32.wrapping_neg().count_digits(),\n);\n```\n\n\u003e [!NOTE]\n\u003e The negative sign itself is not included in the count because\n\u003e the negative sign is not a digit.\n\nThe digit counts of negative numbers represented in other bases reflect the\n[twos-complement](https://en.wikipedia.org/wiki/Two%27s_complement) representation,\nand the digit count of a positive number will _not_ be the same as the count\nof its negated value.\n\n```rust\nfor radix in 2..=16 {\n    match radix {\n        10 =\u003e assert_eq!(\n            0xF00D_i32.count_digits_radix(radix),\n            0xF00D_i32.wrapping_neg().count_digits_radix(radix),\n        ),\n        _ =\u003e assert_ne!(\n            0xBAD_i32.count_digits_radix(radix),\n            0xBAD_i32.wrapping_neg().count_digits_radix(radix),\n        ),\n    }\n}\n```\n\nThis is consistent with Rust's display format.\n```rust\n// Base 2\nassert_eq!(01, format!(\"{:b}\",  1_i32).chars().count());\nassert_eq!(32, format!(\"{:b}\", -1_i32).chars().count());\n\n// Base 8\nassert_eq!(01, format!(\"{:o}\",  1_i32).chars().count());\nassert_eq!(11, format!(\"{:o}\", -1_i32).chars().count());\n\n// Base 10\nassert_eq!(01, format!(\"{  }\",  1_i32).chars().count());\nassert_eq!(01, format!(\"{  }\", -1_i32).strip_prefix('-').unwrap().chars().count());\n\n// Base 16\nassert_eq!(01, format!(\"{:x}\",  1_i32).chars().count());\nassert_eq!(08, format!(\"{:x}\", -1_i32).chars().count());\n```\n\n### Benchmarks\n\n* [table](https://nordzilla.github.io/count-digits)\n* [count_bits()](https://nordzilla.github.io/count-digits/count_bits/report/index.html)\n* [count_octal_digits()](https://nordzilla.github.io/count-digits/count_octal_digits/report/index.html)\n* [count_digits()](https://nordzilla.github.io/count-digits/count_digits/report/index.html)\n* [count_hex_digits()](https://nordzilla.github.io/count-digits/count_hex_digits/report/index.html)\n\nLicense: MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnordzilla%2Fcount-digits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnordzilla%2Fcount-digits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnordzilla%2Fcount-digits/lists"}