-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] wmemchr implementation #142640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] wmemchr implementation #142640
Conversation
Implemented wmemchr and tests.
@llvm/pr-subscribers-libc Author: None (sribee8) ChangesImplemented wmemchr and tests. Full diff: https://github.com/llvm/llvm-project/pull/142640.diff 7 Files Affected:
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a30ffbe0b2b91..cba24a379a74f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -377,6 +377,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 2b74721abda97..1b818191a6539 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 be62495bf8a0f..ee59f8d234bed 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -161,6 +161,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..5a2476ebf3fec
--- /dev/null
+++ b/libc/src/wchar/wmemchr.cpp
@@ -0,0 +1,28 @@
+//===-- 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 38ff8b8720504..65fccbd0da67c 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);
+}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
oops, fix the trailing space then this will be good to merge. |
LGTM |
Implemented wmemchr and tests. Fixes: llvm#121183 --------- Co-authored-by: Sriya Pratipati <[email protected]>
Implemented wmemchr and tests. Fixes: llvm#121183 --------- Co-authored-by: Sriya Pratipati <[email protected]>
Implemented wmemchr and tests.
Fixes: #121183