Skip to content

[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

Merged
merged 4 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions libc/include/wchar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libc/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions libc/src/wchar/wmemchr.cpp
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions libc/src/wchar/wmemchr.h
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions libc/test/src/wchar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 92 additions & 0 deletions libc/test/src/wchar/wmemchr_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
Loading