public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r9-9373] libstdc++: Include scope ID in net::internet::address_v6::to_string() Date: Tue, 20 Apr 2021 18:52:39 +0000 (GMT) [thread overview] Message-ID: <20210420185239.927BC388A43A@sourceware.org> (raw) https://gcc.gnu.org/g:d7bc0ab9700ad2e235f52eac58c36848d67be455 commit r9-9373-gd7bc0ab9700ad2e235f52eac58c36848d67be455 Author: Jonathan Wakely <jwakely@redhat.com> Date: Fri Feb 12 15:08:29 2021 +0000 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. (address_v6::loopback): Fix to be constexpr in C++14. * testsuite/experimental/net/internet/address/v6/members.cc: Test to_string() results. (cherry picked from commit d1a821b93c45bfe7606b5dee8d160c7172b37e3e) Diff: --- libstdc++-v3/include/experimental/internet | 24 ++++++++++++++++------ .../net/internet/address/v6/members.cc | 18 ++++++++++++++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 3ae2e361e1e..6eeb27b74e5 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
reply other threads:[~2021-04-20 18:52 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210420185239.927BC388A43A@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).