| 10986 | } |
| 10987 | |
| 10988 | std::string ipv6(const std::array<uint16_t, 8>& address) noexcept { |
| 10989 | size_t compress_length = 0; // The length of a long sequence of zeros. |
| 10990 | size_t compress = 0; // The start of a long sequence of zeros. |
| 10991 | find_longest_sequence_of_ipv6_pieces(address, compress, compress_length); |
| 10992 | |
| 10993 | if (compress_length <= 1) { |
| 10994 | // Optimization opportunity: Find a faster way then snprintf for imploding |
| 10995 | // and return here. |
| 10996 | compress = compress_length = 8; |
| 10997 | } |
| 10998 | |
| 10999 | std::string output(4 * 8 + 7 + 2, '\0'); |
| 11000 | size_t piece_index = 0; |
| 11001 | char* point = output.data(); |
| 11002 | char* point_end = output.data() + output.size(); |
| 11003 | *point++ = '['; |
| 11004 | while (true) { |
| 11005 | if (piece_index == compress) { |
| 11006 | *point++ = ':'; |
| 11007 | // If we skip a value initially, we need to write '::', otherwise |
| 11008 | // a single ':' will do since it follows a previous ':'. |
| 11009 | if (piece_index == 0) { |
| 11010 | *point++ = ':'; |
| 11011 | } |
| 11012 | piece_index += compress_length; |
| 11013 | if (piece_index == 8) { |
| 11014 | break; |
| 11015 | } |
| 11016 | } |
| 11017 | point = std::to_chars(point, point_end, address[piece_index], 16).ptr; |
| 11018 | piece_index++; |
| 11019 | if (piece_index == 8) { |
| 11020 | break; |
| 11021 | } |
| 11022 | *point++ = ':'; |
| 11023 | } |
| 11024 | *point++ = ']'; |
| 11025 | output.resize(point - output.data()); |
| 11026 | return output; |
| 11027 | } |
| 11028 | |
| 11029 | std::string ipv4(const uint64_t address) noexcept { |
| 11030 | std::string output(15, '\0'); |
no test coverage detected