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;
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:
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