[PATCH] Define std::to_chars and std::from_chars for C++17 (P0067R5, partial)
Jonathan Wakely
jwakely@redhat.com
Fri Apr 7 12:29:00 GMT 2017
This adds another piece of the C++17 library, the std::to_chars and
std::from_chars functions for converting numbers to/from strings. This
only adds the integer support, floating point types will require a lot
more work.
This has only been lightly optimised, so it beats printf on average,
but there's probably more opportunity for improvement. I didn't
investigate whether doing all work with unsigned long long would be
faster, instead of using function templates specialized for unsigned
int, unsigned long and unsigned long long. It should help code size,
but I don't know if it would be faster. I also didn't investigate
whether doing from_chars in two stages would be better, i.e. finding
all the characters that are valid digits in the given base first, and
then converting them to an integer in a second step. I'm sure there's
lots of tuning that could be done, but I hope this is good enough to
start with.
Although the new functions are only defined by <utility> for C++17,
the implementation doesn't require C++17. so directly including
<bits/string_conv.h> makes them available in C++14. Non-portably, of
course.
* include/Makefile.am: Add <bits/string_conv.h>.
* include/Makefile.in: Regenerate.
* include/bits/string_conv.h: New file.
(to_chars_result, to_chars, from_chars_result, from_chars): Define.
* include/std/utility: Include <bits/string_conv.h>.
* testsuite/20_util/from_chars/1.cc: New test.
* testsuite/20_util/from_chars/1_neg.cc: New test.
* testsuite/20_util/from_chars/2.cc: New test.
* testsuite/20_util/from_chars/requirements.cc: New test.
* testsuite/20_util/to_chars/1.cc: New test.
* testsuite/20_util/to_chars/1_neg.cc: New test.
* testsuite/20_util/to_chars/2.cc: New test.
* testsuite/20_util/to_chars/requirements.cc: New test.
Any suggestions for immediate improvement?
Any reason not to commit this to trunk?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 62115 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20170407/bd8ba0ec/attachment.bin>
More information about the Libstdc++
mailing list