* [committed] libstdc++: Include scope ID in net::internet::address_v6::to_string()
@ 2021-02-12 15:12 Jonathan Wakely
2021-02-12 15:21 ` Jonathan Wakely
0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2021-02-12 15:12 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 249 bytes --]
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.
Tested powerpc64le-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3148 bytes --]
commit d1a821b93c45bfe7606b5dee8d160c7172b37e3e
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Fri Feb 12 15:08:29 2021
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [committed] libstdc++: Include scope ID in net::internet::address_v6::to_string()
2021-02-12 15:12 [committed] libstdc++: Include scope ID in net::internet::address_v6::to_string() Jonathan Wakely
@ 2021-02-12 15:21 ` Jonathan Wakely
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-02-12 15:21 UTC (permalink / raw)
To: libstdc++, gcc-patches
On 12/02/21 15:12 +0000, Jonathan Wakely wrote:
>@@ -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}};
> }
Oops, this was supposed to be a separate commit. The previous version
was only constexpr in C++17 and later, which is why I changed it.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-02-12 15:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-12 15:12 [committed] libstdc++: Include scope ID in net::internet::address_v6::to_string() Jonathan Wakely
2021-02-12 15:21 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).