lexical_write_integer::algorithm

Function algorithm

Source
pub fn algorithm<T>(
    value: T,
    radix: u32,
    table: &[u8],
    buffer: &mut [u8],
) -> usize
where T: UnsignedInteger + DigitCount,
Expand description

Optimized implementation for radix-N numbers.

This uses an Alexandrescu algorithm, which prints 2 digits at a time which is much faster than a naive approach. However, the jeaiii algorithm can be faster still for decimal numbers: https://jk-jeon.github.io/posts/2022/02/jeaiii-algorithm/

ยงSafety

Safe as long as digit_count returns the number of written digits. For performance reasons, this is always calculated as the exact number of digits. If the value is too small, then the buffer will underflow, causing out-of-bounds read/writes. Care must be used that digit_count is correctly implemented.

Since digit_count is implemented as an unsafe trait, these guarantees must be held.

See the crate crate documentation for more security considerations.