[libc-commits] [libc] [libc] implement `inet_aton` (PR #162651)
    Michael Jones via libc-commits 
    libc-commits at lists.llvm.org
       
    Fri Oct 10 10:47:58 PDT 2025
    
    
  
================
@@ -0,0 +1,54 @@
+//===-- Implementation of inet_aton function ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/arpa/inet/inet_aton.h"
+#include "src/__support/common.h"
+#include "src/__support/endian_internal.h"
+#include "src/__support/str_to_integer.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inet_aton, (const char *cp, in_addr *inp)) {
+  unsigned long parts[4] = {0};
+  int dot_num = 0;
+
+  for (; dot_num < 4; ++dot_num) {
+    auto result = internal::strtointeger<unsigned long>(cp, 0);
+    parts[dot_num] = result;
+
+    if (result.has_error() || result.parsed_len == 0)
+      return 0;
+    char next_char = *(cp + result.parsed_len);
+    if (next_char != '.' && next_char != '\0')
+      return 0;
+    else if (next_char == '\0')
+      break;
+    else
+      cp += (result.parsed_len + 1);
+  }
+
+  if (dot_num > 3)
+    return 0;
+
+  unsigned long result = 0;
+  for (int i = 0; i <= dot_num; ++i) {
+    unsigned long max_part =
+        i == dot_num ? (0xffffffffUL >> (8 * dot_num)) : 0xffUL;
+    if (parts[i] > max_part)
+      return 0;
+    int shift = i == dot_num ? 0 : 8 * (3 - i);
----------------
michaelrj-google wrote:
to avoid magic constants, could you explain what the numbers here are representing? Specifically, why is this `3 - i`?
https://github.com/llvm/llvm-project/pull/162651
    
    
More information about the libc-commits
mailing list