[libc-commits] [libc] Implement inet_pton (PR #224644)

via libc-commits libc-commits at lists.llvm.org
Tue Sep 22 05:35:01 PDT 2026


================
@@ -26,8 +27,48 @@
 #include "src/string/memory_utils/inline_memcpy.h"
 
 namespace LIBC_NAMESPACE_DECL {
+
 namespace net {
 
+int inet_pton_v4(cpp::string_view src, void *dst) {
+  uint8_t bytes[4];
+  size_t idx = 0;
+  uint32_t current_val = 0;
+  size_t digits_in_octet = 0;
+
+  for (char c : src) {
+    if (c >= '0' && c <= '9') {
+      // Reject octals and leading zeros
+      if (digits_in_octet > 0 && current_val == 0)
+        return 0;
+
+      current_val = current_val * 10 + static_cast<uint32_t>(c - '0');
----------------
afnrow wrote:

Same on the ascii part and this is cleaner than if I were to use `internal::strtointeger` for example on a single character.

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


More information about the libc-commits mailing list