[libc-commits] [libc] [libc] Implement IPv6 (AF_INET6) support in inet_pton (PR #226550)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Tue Sep 29 05:17:20 PDT 2026


================
@@ -67,6 +69,109 @@ namespace net {
   return true;
 }
 
+[[nodiscard]] bool str_to_ipv6(cpp::string_view src, struct in6_addr &dst) {
+  if (src.empty())
+    return false;
+
+  bool has_double_colon = false;
+  FixedVector<uint16_t, 8> parts[2];
+  size_t part_idx = 0;
+
+  if (src.starts_with("::")) {
+    has_double_colon = true;
+    part_idx = 1;
+    src.remove_prefix(2);
+    if (src.empty()) {
+      inline_bzero(&dst.s6_addr, 16);
+      return true;
+    }
+    if (src.starts_with(':'))
+      return false;
+  } else if (src.starts_with(':')) {
+    return false;
+  }
+
+  while (!src.empty()) {
+    if (src.starts_with("::")) {
----------------
labath wrote:

This is very similar to the loop preamble above (and it's still a bit more branchy that I'd like).

Would something like this work?
```
// No preamble
while (!src.empty()) {
  switch(src.find_first_not_of(':')) {
  case 3:
    return false; // Too many colons
  case 2:
    // Handle "::"
  case 1:
    src.remove_prefix(1);
    break;
  case 0:
    if (part_idx > 0 || parts[0].size() > 0)
      return false; // Only the first component can lack a colon.
    break;
  }
  // parse the number...
}
```

https://github.com/llvm/llvm-project/pull/226550


More information about the libc-commits mailing list