Replace std::to_string for integers with optimized version
authorJonathan Wakely <jwakely@redhat.com>
Wed, 12 Jun 2019 14:52:02 +0000 (15:52 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 12 Jun 2019 14:52:02 +0000 (15:52 +0100)
commitcd0b94e650a880b2ab04922e476aa28007277d5c
tree392d0abf2fbedc380e69b203ddb9b74c775d072d
parentff7b3aa51f8edd24fdc599d9f95a5cbf61aeb76e
Replace std::to_string for integers with optimized version

The std::to_chars functions from C++17 can be used to implement
std::to_string with much better performance than calling snprintf. Only
the __detail::__to_chars_len and __detail::__to_chars_10 functions are
needed for to_string, because it always outputs base 10 representations.

The return type of __detail::__to_chars_10 should not be declared before
C++17, so the function body is extracted into a new function that can be
reused by to_string and __detail::__to_chars_10.

The existing tests for to_chars rely on to_string to check for correct
answers. Now that they use the same code that doesn't actually ensure
correctness, so add new tests for std::to_string that compare against
printf output.

* include/Makefile.am: Add new <bits/charconv.h> header.
* include/Makefile.in: Regenerate.
* include/bits/basic_string.h (to_string(int), to_string(unsigned))
(to_string(long), to_string(unsigned long), to_string(long long))
(to_string(unsigned long long)): Rewrite to use __to_chars_10_impl.
* include/bits/charconv.h: New header.
(__detail::__to_chars_len): Move here from <charconv>.
(__detail::__to_chars_10_impl): New function extracted from
__detail::__to_chars_10.
* include/std/charconv (__cpp_lib_to_chars): Add, but comment out.
(__to_chars_unsigned_type): New class template that reuses
__make_unsigned_selector_base::__select to pick a type.
(__unsigned_least_t): Redefine as __to_chars_unsigned_type<T>::type.
(__detail::__to_chars_len): Move to new header.
(__detail::__to_chars_10): Add inline specifier. Move code doing the
output to __detail::__to_chars_10_impl and call that.
* include/std/version (__cpp_lib_to_chars): Add, but comment out.
* testsuite/21_strings/basic_string/numeric_conversions/char/
to_string.cc: Fix reference in comment. Remove unused variable.
* testsuite/21_strings/basic_string/numeric_conversions/char/
to_string_int.cc: New test.

From-SVN: r272186
libstdc++-v3/ChangeLog
libstdc++-v3/include/Makefile.am
libstdc++-v3/include/Makefile.in
libstdc++-v3/include/bits/basic_string.h
libstdc++-v3/include/bits/charconv.h [new file with mode: 0644]
libstdc++-v3/include/std/charconv
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/to_string.cc
libstdc++-v3/testsuite/21_strings/basic_string/numeric_conversions/char/to_string_int.cc [new file with mode: 0644]