Boost logo

Boost :

From: Matt Borland (matt_at_[hidden])
Date: 2023-05-10 14:00:57


I’m Matt Borland, who many might recognize from my work in Boost.Math and fewer might know from Boost.Multiprecision. I’m putting the finishing touches on a new library that I hope will become part of Boost, after a successful formal review. This library is essentially already part of C++17 and later but in the Boost tradition we wish to provide it for users and other Boost libraries that want to fully support C++14 or C++11.

What does charconv do? It converts a character sequence into a number (from_chars) or converts a number into a character sequence (to_chars). This is similar to strtod and printf with a few key differences. First and most important is a roundtrip guarantee. That is a number converted into a character sequence, and then back into a number will be identical to the starting value. Second, you can easily specify an input/output format with a chars_format argument, and also provide a target precision. Third, it is much easier to check for errors. Strtod has a few different ways of reporting different errors whereas the results of to_chars returns an error code from errno. Using this is no different from POSIX where you check for 0 and then move on. We use errno rather than std::error_code as specified in the standard. This allows us to avoid including <system_error> and is more ergonomic. Lastly, charconv is significantly more performant than strtod and printf. We utilize algorithms derived from Daniel Lemire’s “Number Parsing at a Gigabyte per Second” and Junkey Jeon’s Dragonbox. You can read about those here:

https://arxiv.org/abs/2101.11408

https://github.com/jk-jeon/dragonbox

And here are a number of example snippets:

https://develop.charconv.cpp.al/

As of now the library supports all built-in integer types, signed and unsigned __int128, float, and double. I am striving to add support for long doubles (64, 80, and 128-bit) to both from_chars and to_chars. With these in hand I have a mind to experiment with integrating this into Boost.JSON, which for lack of a better phrase, sure could use a bit of help with its conversions between IEEE floats and decimal strings. This will also help with checking our validation, accuracy, and performance.

If you want to check out the library it is here:

[https://github.com/cppalliance/charconv](https://github.com/CPPAlliance/charconv)

It is designed to be built and used in the Boost superproject. This means that if you want to clone it, you should place the repository in the libs/charconv directory of your local superproject. Feel free to hammer on it, and report any errors or performance issues that you run into. Also if you find the docs could use improvement please open an issue so that we can address it. These will help us ensure that we have the highest quality product before attempting to pass a formal review for inclusion in Boost.

Thank you for your time.

Matt Borland


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk