[libc-commits] [libc] 22dd9a2 - [libc] wmemchr implementation (#142640)
via libc-commits
libc-commits at lists.llvm.org
Tue Jun 3 17:05:17 PDT 2025
Author: sribee8
Date: 2025-06-03T17:05:14-07:00
New Revision: 22dd9a24830ef29c25a8634777772282a3f33598
URL: https://github.com/llvm/llvm-project/commit/22dd9a24830ef29c25a8634777772282a3f33598
DIFF: https://github.com/llvm/llvm-project/commit/22dd9a24830ef29c25a8634777772282a3f33598.diff
LOG: [libc] wmemchr implementation (#142640)
Implemented wmemchr and tests.
Fixes: #121183
---------
Co-authored-by: Sriya Pratipati <sriyap at google.com>
Added:
libc/src/wchar/wmemchr.cpp
libc/src/wchar/wmemchr.h
libc/test/src/wchar/wmemchr_test.cpp
Modified:
libc/config/linux/x86_64/entrypoints.txt
libc/include/wchar.yaml
libc/src/wchar/CMakeLists.txt
libc/test/src/wchar/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index ed2994d1273c4..959bdbf08dbea 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -379,6 +379,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.wchar.wcsstr
libc.src.wchar.wcsncat
libc.src.wchar.wcscpy
+ libc.src.wchar.wmemchr
# sys/uio.h entrypoints
libc.src.sys.uio.writev
diff --git a/libc/include/wchar.yaml b/libc/include/wchar.yaml
index 18b77a2711f7e..877be48b6a10f 100644
--- a/libc/include/wchar.yaml
+++ b/libc/include/wchar.yaml
@@ -86,6 +86,14 @@ functions:
- type: const wchar_t *
- type: const wchar_t *
- type: size_t
+ - name: wmemchr
+ standards:
+ - stdc
+ return_type: const wchar_t *
+ arguments:
+ - type: const wchar_t *
+ - type: wchar_t
+ - type: size_t
- name: wmempcpy
standards:
- gnu
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index b131d2feeda06..759f708c2247a 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -185,6 +185,17 @@ add_entrypoint_object(
libc.src.string.string_utils
)
+add_entrypoint_object(
+ wmemchr
+ SRCS
+ wmemchr.cpp
+ HDRS
+ wmemchr.h
+ DEPENDS
+ libc.hdr.types.size_t
+ libc.hdr.wchar_macros
+)
+
add_entrypoint_object(
wmempcpy
SRCS
diff --git a/libc/src/wchar/wmemchr.cpp b/libc/src/wchar/wmemchr.cpp
new file mode 100644
index 0000000000000..8a1cd6ce43890
--- /dev/null
+++ b/libc/src/wchar/wmemchr.cpp
@@ -0,0 +1,27 @@
+//===-- Implementation of wmemchr -----------------------------------------===//
+//
+// 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/wchar/wmemchr.h"
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(const wchar_t *, wmemchr,
+ (const wchar_t *s, wchar_t c, size_t n)) {
+ size_t i = 0;
+ for (; i < n; ++i)
+ if (s[i] == c)
+ return (s + i);
+ return nullptr;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wmemchr.h b/libc/src/wchar/wmemchr.h
new file mode 100644
index 0000000000000..55412907ddca6
--- /dev/null
+++ b/libc/src/wchar/wmemchr.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for wmemchr ---------------------------------===//
+//
+// 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_WCHAR_WMEMCHR_H
+#define LLVM_LIBC_SRC_WCHAR_WMEMCHR_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+const wchar_t *wmemchr(const wchar_t *s, wchar_t c, size_t n);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_WCHAR_WMEMCHR_H
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 89c3048ae262b..6293e8e3d55cf 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -105,6 +105,16 @@ add_libc_test(
libc.src.wchar.wcsspn
)
+add_libc_test(
+ wmemchr_test
+ SUITE
+ libc_wchar_unittests
+ SRCS
+ wmemchr_test.cpp
+ DEPENDS
+ libc.src.wchar.wmemchr
+)
+
add_libc_test(
wmemcmp_test
SUITE
diff --git a/libc/test/src/wchar/wmemchr_test.cpp b/libc/test/src/wchar/wmemchr_test.cpp
new file mode 100644
index 0000000000000..6b25bd83b16ee
--- /dev/null
+++ b/libc/test/src/wchar/wmemchr_test.cpp
@@ -0,0 +1,92 @@
+//===-- Unittests for wmemchr ---------------------------------------------===//
+//
+// 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 "hdr/types/wchar_t.h"
+#include "src/wchar/wmemchr.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcWMemChrTest, FindsCharacterAfterNullTerminator) {
+ // wmemchr should continue searching after a null terminator.
+ const size_t size = 5;
+ const wchar_t src[size] = {L'a', L'\0', L'b', L'c', L'\0'};
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'b', size), (src + 2));
+}
+
+TEST(LlvmLibcWMemChrTest, FindsCharacterInNonNullTerminatedCollection) {
+ const size_t size = 3;
+ const wchar_t src[size] = {L'a', L'b', L'c'};
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'b', size), (src + 1));
+}
+
+TEST(LlvmLibcWMemChrTest, FindsFirstCharacter) {
+ const size_t size = 6;
+ const wchar_t *src = L"abcde";
+ // Should return original array since 'a' is the first character.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'a', size), (src));
+}
+
+TEST(LlvmLibcWMemChrTest, FindsMiddleCharacter) {
+ const size_t size = 6;
+ const wchar_t *src = L"abcde";
+ // Should return characters after and including 'c'.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'c', size), (src + 2));
+}
+
+TEST(LlvmLibcWMemChrTest, FindsLastCharacterThatIsNotNullTerminator) {
+ const size_t size = 6;
+ const wchar_t *src = L"abcde";
+ // Should return 'e' and null terminator.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'e', size), (src + 4));
+}
+
+TEST(LlvmLibcWMemChrTest, FindsNullTerminator) {
+ const size_t size = 6;
+ const wchar_t *src = L"abcde";
+ // Should return null terminator.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'\0', size), (src + 5));
+}
+
+TEST(LlvmLibcWMemChrTest, CharacterNotWithinStringShouldReturnNullptr) {
+ const size_t size = 6;
+ const wchar_t *src = L"abcde";
+ // Should return nullptr.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'z', size), nullptr);
+}
+
+TEST(LlvmLibcWMemChrTest, CharacterNotWithinSizeShouldReturnNullptr) {
+ const size_t size = 3;
+ const wchar_t *src = L"abcde";
+ // Should return nullptr.
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'd', size), nullptr);
+}
+
+TEST(LlvmLibcWMemChrTest, TheSourceShouldNotChange) {
+ const size_t size = 3;
+ const wchar_t *src = L"ab";
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'a', size), src);
+ ASSERT_TRUE(src[0] == L'a');
+ ASSERT_TRUE(src[1] == L'b');
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'c', size), nullptr);
+ ASSERT_TRUE(src[0] == L'a');
+ ASSERT_TRUE(src[1] == L'b');
+}
+
+TEST(LlvmLibcWMemChrTest, EmptyStringShouldOnlyMatchNullTerminator) {
+ const size_t size = 1;
+ const wchar_t *src = L"";
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'\0', size), src);
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'c', size), nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'1', size), nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'?', size), nullptr);
+}
+
+TEST(LlvmLibcWMemChrTest, SingleRepeatedCharacterShouldReturnFirst) {
+ const size_t size = 6;
+ const wchar_t *src = L"XXXXX";
+ ASSERT_EQ(LIBC_NAMESPACE::wmemchr(src, L'X', size), src);
+}
More information about the libc-commits
mailing list