--- /dev/null
+//===-- Implementation of strnlen------------------------------------------===//
+//
+// 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/string/strnlen.h"
+
+#include "src/__support/common.h"
+#include "src/string/memchr.h"
+#include <stddef.h>
+
+namespace __llvm_libc {
+
+size_t LLVM_LIBC_ENTRYPOINT(strnlen)(const char *src, size_t n) {
+ const char *temp =
+ reinterpret_cast<char *>(__llvm_libc::memchr(src, '\0', n));
+ return temp ? temp - src : n;
+}
+
+} // namespace __llvm_libc
--- /dev/null
+//===-- Implementation header for strnlen ------------------------*- C++-*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRING_STRNLEN_H
+#define LLVM_LIBC_SRC_STRING_STRNLEN_H
+
+#include <stddef.h>
+
+namespace __llvm_libc {
+
+size_t strnlen(const char *src, size_t n);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRNLEN_H
--- /dev/null
+//===-- Unittests for strnlen----------------------------------------------===//
+//
+// 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/string/strnlen.h"
+#include "utils/UnitTest/Test.h"
+#include <stddef.h>
+
+TEST(StrNLenTest, EmptyString) {
+ const char *empty = "";
+ ASSERT_EQ(static_cast<size_t>(0), __llvm_libc::strnlen(empty, 0));
+ // If N is greater than string length, this should still return 0.
+ ASSERT_EQ(static_cast<size_t>(0), __llvm_libc::strnlen(empty, 1));
+}
+
+TEST(StrNLenTest, OneCharacterString) {
+ const char *single = "X";
+ ASSERT_EQ(static_cast<size_t>(1), __llvm_libc::strnlen(single, 1));
+ // If N is zero, this should return 0.
+ ASSERT_EQ(static_cast<size_t>(0), __llvm_libc::strnlen(single, 0));
+ // If N is greater than string length, this should still return 1.
+ ASSERT_EQ(static_cast<size_t>(1), __llvm_libc::strnlen(single, 2));
+}
+
+TEST(StrNLenTest, ManyCharacterString) {
+ const char *many = "123456789";
+ ASSERT_EQ(static_cast<size_t>(9), __llvm_libc::strnlen(many, 9));
+ // If N is smaller than the string length, it should return N.
+ ASSERT_EQ(static_cast<size_t>(3), __llvm_libc::strnlen(many, 3));
+ // If N is zero, this should return 0.
+ ASSERT_EQ(static_cast<size_t>(0), __llvm_libc::strnlen(many, 0));
+ // If N is greater than the string length, this should still return 9.
+ ASSERT_EQ(static_cast<size_t>(9), __llvm_libc::strnlen(many, 42));
+}
+
+TEST(StrNLenTest, CharactersAfterNullTerminatorShouldNotBeIncluded) {
+ const char str[5] = {'a', 'b', 'c', '\0', 'd'};
+ ASSERT_EQ(static_cast<size_t>(3), __llvm_libc::strnlen(str, 3));
+ // This should only read up to the null terminator.
+ ASSERT_EQ(static_cast<size_t>(3), __llvm_libc::strnlen(str, 4));
+ ASSERT_EQ(static_cast<size_t>(3), __llvm_libc::strnlen(str, 5));
+}