#unsigned-integer #integer #formatting #printing #signed-integer #thousands #byte-slice

dactyl

A small library to quickly stringify integers with basic formatting

36 releases

0.7.4 Sep 15, 2024
0.7.2 Jul 25, 2024
0.7.0 Feb 8, 2024
0.6.0 Oct 16, 2023
0.1.7 Mar 17, 2021

#26 in Value formatting

Download history 69/week @ 2024-06-16 63/week @ 2024-06-23 56/week @ 2024-06-30 38/week @ 2024-07-07 65/week @ 2024-07-14 230/week @ 2024-07-21 210/week @ 2024-07-28 89/week @ 2024-08-04 99/week @ 2024-08-11 35/week @ 2024-08-18 93/week @ 2024-08-25 272/week @ 2024-09-01 66/week @ 2024-09-08 255/week @ 2024-09-15 179/week @ 2024-09-22 99/week @ 2024-09-29

627 downloads per month
Used in 18 crates (5 directly)

WTFPL license

185KB
3.5K SLoC

Dactyl

docs.rs changelog
crates.io ci deps.rs
license contributions welcome

This crate provides a fast interface to "stringify" unsigned integers, formatted with commas at each thousand. It prioritizes speed and simplicity over configurability.

If your application just wants to quickly turn 1010 into "1,010", Dactyl is a great choice. If your application requires locale awareness or other options, something like num-format would probably make more sense.

Similar to itoa, Dactyl writes ASCII conversions to a temporary buffer, but does so using fixed arrays sized for each type's maximum value, minimizing the allocation overhead for, say, tiny little u8s.

Each type has its own struct, each of which works exactly the same way:

  • NiceU8
  • NiceU16
  • NiceU32
  • NiceU64 (also covers usize)
  • NiceFloat
  • NiceElapsed (for durations)
  • NicePercent (for floats representing percentages)

The intended use case is to simply call the appropriate from() for the type, then use either the as_str() or as_bytes() struct methods to retrieve the output in the desired format. Each struct also implements traits like Deref, Display, AsRef<str>, AsRef<[u8]>, etc., if you prefer those.

use dactyl::NiceU16;

assert_eq!(NiceU16::from(11234_u16).as_str(), "11,234");
assert_eq!(NiceU16::from(11234_u16).as_bytes(), b"11,234");

But the niceness doesn't stop there. Dactyl provides several other structs, methods, and traits to performantly work with integers, such as:

  • NoHash: a passthrough hasher for integer HashSet/HashMap collections
  • traits::BytesToSigned: signed integer parsing from byte slices
  • traits::BytesToUnsigned: unsigned integer parsing from byte slices
  • traits::HexToSigned: signed integer parsing from hex
  • traits::HexToUnsigned: unsigned integer parsing from hex

Installation

Add dactyl to your dependencies in Cargo.toml, like:

[dependencies]
dactyl = "0.7.*"

License

See also: CREDITS.md

Copyright © 2024 Blobfolio, LLC <hello@blobfolio.com>

This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

No runtime deps