Tested x86_64-linux. Pushed to trunk. -- >8 --
This is an order of magnitude faster than calling inet_ntop (and not only because we now avoid allocating a string that is one byte larger than the SSO buffer). libstdc++-v3/ChangeLog: * include/experimental/internet (address_v4::to_string): Optimize. * testsuite/experimental/net/internet/address/v4/members.cc: Check more addresses. --- libstdc++-v3/include/experimental/internet | 28 +++++++++++++------ .../net/internet/address/v4/members.cc | 11 ++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 707370d5611..08bd0db4bb2 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -44,6 +44,7 @@ #include <sstream> #include <cstdint> #include <experimental/string_view> +#include <bits/charconv.h> #ifdef _GLIBCXX_HAVE_UNISTD_H # include <unistd.h> #endif @@ -241,17 +242,28 @@ namespace ip __string_with<_Allocator> to_string(const _Allocator& __a = _Allocator()) const { -#ifdef _GLIBCXX_HAVE_ARPA_INET_H + auto __write = [__addr = to_uint()](char* __p, size_t __n) { + auto __to_chars = [](char* __p, uint8_t __v) { + unsigned __n = __v >= 100u ? 3 : __v >= 10u ? 2 : 1; + std::__detail::__to_chars_10_impl(__p, __n, __v); + return __p + __n; + }; + const auto __begin = __p; + __p = __to_chars(__p, uint8_t(__addr >> 24)); + for (int __i = 2; __i >= 0; __i--) { + *__p++ = '.'; + __p = __to_chars(__p, uint8_t(__addr >> (__i * 8))); + } + return __p - __begin; + }; __string_with<_Allocator> __str(__a); - __str.resize(INET_ADDRSTRLEN); - if (inet_ntop(AF_INET, &_M_addr, &__str.front(), __str.size())) - __str.erase(__str.find('\0')); - else - __str.resize(0); - return __str; +#if __cpp_lib_string_resize_and_overwrite + __str.resize_and_overwrite(15, __write); #else - std::__throw_system_error((int)__unsupported_err()); + __str.resize(15); + __str.resize(__write(&__str.front(), 15)); #endif + return __str; } // static members: diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc index df19b11804d..c40a8103664 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc @@ -22,6 +22,7 @@ #include <experimental/internet> #include <sstream> #include <testsuite_hooks.h> +#include <testsuite_allocator.h> using std::experimental::net::ip::address_v4; @@ -100,6 +101,16 @@ test04() VERIFY( address_v4::any().to_string() == "0.0.0.0" ); VERIFY( address_v4::loopback().to_string() == "127.0.0.1" ); VERIFY( address_v4::broadcast().to_string() == "255.255.255.255" ); + using b = address_v4::bytes_type; + VERIFY( address_v4(b(1, 23, 45, 67)).to_string() == "1.23.45.67" ); + VERIFY( address_v4(b(12, 34, 56, 78)).to_string() == "12.34.56.78" ); + VERIFY( address_v4(b(123, 4, 5, 6)).to_string() == "123.4.5.6" ); + VERIFY( address_v4(b(123, 234, 124, 235)).to_string() == "123.234.124.235" ); + + __gnu_test::uneq_allocator<char> alloc(123); + auto str = address_v4(b(12, 34, 56, 78)).to_string(alloc); + VERIFY(str.get_allocator().get_personality() == alloc.get_personality()); + VERIFY( str == "12.34.56.78" ); } void -- 2.39.2