[libc-commits] [libc] [libc] Add inet_ntop (PR #204143)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Tue Jun 23 06:46:06 PDT 2026
================
@@ -61,5 +68,160 @@ cpp::optional<in_addr_t> inet_addr(const char *cp) {
return Endian::to_big_endian(result);
}
+namespace {
+
+size_t ipv4_num_bytes(cpp::span<const uint8_t> src) {
+ size_t result = 8; // four digits, three dots and '\0'
+ for (unsigned i = 0; i < 4; ++i)
+ result += (src[i] >= 10) + (src[i] >= 100);
+ return result;
+}
+
+size_t ipv4_to_str_unchecked(cpp::span<const uint8_t> src,
+ cpp::span<char> dst) {
+ size_t pos = 0;
+ for (unsigned i = 0; i < 4; ++i) {
+ uint8_t val = src[i];
+ if (val >= 100) {
+ uint8_t cent = val / 100;
+ val -= cent * 100;
+ uint8_t dec = val / 10;
+ dst[pos++] = '0' + cent;
+ dst[pos++] = '0' + dec;
+ dst[pos++] = '0' + val % 10;
+ } else if (val >= 10) {
+ uint8_t dec = val / 10;
+ dst[pos++] = '0' + dec;
+ dst[pos++] = '0' + val % 10;
+ } else {
+ dst[pos++] = '0' + val;
+ }
+ dst[pos++] = i < 3 ? '.' : '\0';
+ }
+ return pos;
+}
+
+size_t ipv6_to_str_unchecked(const struct in6_addr &src, cpp::span<char> dst) {
+ // Find the longest run of zeroes to compress to "::"
+ struct Run {
+ unsigned start = 0;
+ unsigned len = 0;
+ };
+ Run best, current;
+ for (unsigned i = 0; i < 8; ++i) {
+ uint16_t val = src.s6_addr16[i];
+ if (val == 0) {
+ ++current.len;
+ } else {
+ // In case of ties, the first sequence wins.
+ if (current.len > best.len)
+ best = current;
+ current = {i + 1, 0};
+ }
+ }
+ if (current.len > best.len)
+ best = current;
+
+ bool is_mapped =
+ best.start == 0 &&
+ (best.len == 6 || (best.len == 5 && src.s6_addr16[5] == 0xffff));
+ unsigned num_words = is_mapped ? 6 : 8;
+
+ char *pos = dst.data();
+ auto append_word = [&](unsigned i) {
+ uint16_t word = Endian::from_big_endian(src.s6_addr16[i]);
+ static constexpr char DIGITS[] = "0123456789abcdef";
+ if (word >= 0x1000) {
+ pos[0] = DIGITS[word >> 12];
----------------
labath wrote:
I was getting (slightly) better numbers this way, though it seems that was just a side-effect of different code layout. Trying this again with the latest code changes, it number are actually (again, slightly) better with the index version. Going with that.
https://github.com/llvm/llvm-project/pull/204143
More information about the libc-commits
mailing list