From: Jonathan Wakely Date: Fri, 12 Feb 2021 15:08:29 +0000 (+0000) Subject: libstdc++: Include scope ID in net::internet::address_v6::to_string() X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d1a821b93c45bfe7606b5dee8d160c7172b37e3e;p=gcc.git libstdc++: Include scope ID in net::internet::address_v6::to_string() libstdc++-v3/ChangeLog: * include/experimental/internet (address_v6::to_string): Include scope ID in string. * testsuite/experimental/net/internet/address/v6/members.cc: Test to_string() results. --- diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 57831676a29..288c61ba25a 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -344,9 +344,23 @@ namespace ip to_string(const _Allocator& __a = _Allocator()) const { __string_with<_Allocator> __str(__a); - __str.resize(INET6_ADDRSTRLEN); - if (inet_ntop(AF_INET6, &_M_bytes, &__str.front(), __str.size())) - __str.erase(__str.find('\0')); + __str.resize(INET6_ADDRSTRLEN + (_M_scope_id ? 11 : 0)); + char* const __p = &__str.front(); + if (inet_ntop(AF_INET6, &_M_bytes, __p, __str.size())) + { + auto __end = __str.find('\0'); + if (unsigned long __scope = _M_scope_id) + { + __end += +#if _GLIBCXX_USE_C99_STDIO + __builtin_snprintf(__p + __end, __str.size() - __end, + "%%%lu", __scope); +#else + __builtin_sprintf(__p + __end, "%%%lu", __scope); +#endif + } + __str.erase(__end); + } else __str.resize(0); return __str; @@ -364,9 +378,7 @@ namespace ip static constexpr address_v6 loopback() noexcept { - address_v6 __addr; - __addr._M_bytes[15] = 1; - return __addr; + return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}}; } private: diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc index 506345dc990..b77d6a29e3d 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc @@ -86,16 +86,30 @@ static_assert(test02(), ""); void test03() { + // Assume these addresses use the preferred forms: VERIFY( address_v6::any().to_string() == "::" ); VERIFY( address_v6::loopback().to_string() == "::1" ); + + // Choose values with no leading zeros, so output is portable: + address_v6 a{address_v6::bytes_type{21,22,23,24,25,26,27,28,29}, 42}; + const std::string s = a.to_string(); + if (s.find("::") != s.npos) + VERIFY( s == "1516:1718:191a:1b1c:1d00::%42" ); + else + { + // Contiguous zeros were not shortened to "::" + VERIFY( s.substr(0, 25) == "1516:1718:191a:1b1c:1d00:" ); + VERIFY( s.substr(s.size() - 3) == "%42" ); + } } void test04() { + address_v6 a{address_v6::bytes_type{1,2,3,4,5,6,7,8,9}, 42}; std::ostringstream ss; - ss << address_v6::any() << ' ' << address_v6::loopback(); - VERIFY( ss.str() == ":: ::1" ); + ss << address_v6::any() << ' ' << address_v6::loopback() << ' ' << a; + VERIFY( ss.str() == (":: ::1 " + a.to_string()) ); } int