nanokit
About
A collection of tiny, reusable utility methods that reduce code size and improve performance. Readme for developers/contributors is available at README-DEV.MD.
String Construction
Fast way to concatenate strings:
use concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2;
assert_eq!;
And with any type that implements AsRef<str>:
use concat_2;
let base_string = Stringfrom;
let text_string = Stringfrom;
let result = concat_2;
assert_eq!;
This is similar to libraries like concat_strs and string_concat, except that instead of pushing strings onto a preallocated String instance, we instead unsafely create a new String instance and adjust the length.
This saves around 150 bytes of code in codebases that otherwise don't use string concatenation (push_str or add).
Also saves some instructions.
Additional methods concat_3, concat_4, concat_5 exist.
Unsafe Concat
You can save on another 2 instructions per concatenation if you know the final string length
is lesser than isize::MAX.
use concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2_no_overflow;
assert_eq!;
Features
no-inline-concat: Disables inlining of string concat functions (saves code size).
Numeric Utilities
Count Needed Bits
Provides a method to determine the number of bits needed to store a given number.
let number: u64 = 5;
println!;
The produced code does not panic, nor does it branch.
Generated x86_64 assembly for u64:
bits_needed_to_store:
lzcnt rcx, rdi
mov eax, 64
sub eax, ecx
ret
Note: The number of bits needed to store the value 0 is returned as 0.
This may not match your expectations. If you expect the result to be 1; try
the count-digits crate by nordzilla. Do note that said crate uses ilog2, which
may panic.
Related Crates
- itoa: Integer to text.
Contributing
See CONTRIBUTING for guidance on how to contribute to this project.
License
nanokit is part of the Reloaded suite of libraries.
Licensed under MIT.
If you find this library useful, please contribute back!!