Skip to content

[libc] Implement search/lsearch #131431

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
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.search.hsearch_r
libc.src.search.insque
libc.src.search.lfind
libc.src.search.lsearch
libc.src.search.remque

# threads.h entrypoints
Expand Down
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 @@ -1113,6 +1113,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.search.hsearch_r
libc.src.search.insque
libc.src.search.lfind
libc.src.search.lsearch
libc.src.search.remque

# threads.h entrypoints
Expand Down
10 changes: 10 additions & 0 deletions libc/include/search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ functions:
- type: size_t *
- type: size_t
- type: __lsearchcompare_t
- name: lsearch
standards:
- POSIX
return_type: void *
arguments:
- type: const void *
- type: void *
- type: size_t *
- type: size_t
- type: __lsearchcompare_t
13 changes: 13 additions & 0 deletions libc/src/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,16 @@ add_entrypoint_object(
libc.src.__support.CPP.cstddef
libc.src.__support.memory_size
)

add_entrypoint_object(
lsearch
SRCS
lsearch.cpp
HDRS
lsearch.h
DEPENDS
libc.include.search
libc.src.__support.CPP.cstddef
libc.src.__support.memory_size
libc.src.string.memory_utils.inline_memcpy
)
2 changes: 1 addition & 1 deletion libc/src/search/lfind.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===-- Implementation of lfind -------------------------------*- C++ -*-===//
//===-- Implementation of lfind ---------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down
39 changes: 39 additions & 0 deletions libc/src/search/lsearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===-- Implementation of lsearch -------------------------------*- 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
//
//===----------------------------------------------------------------------===//

#include "src/search/lsearch.h"
#include "src/__support/CPP/cstddef.h" // cpp::byte
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/memory_size.h"
#include "src/string/memory_utils/inline_memcpy.h"

namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void *, lsearch,
(const void *key, void *base, size_t *nmemb, size_t size,
int (*compar)(const void *, const void *))) {
if (key == nullptr || base == nullptr || nmemb == nullptr ||
compar == nullptr)
return nullptr;

size_t byte_len = 0;
if (internal::mul_overflow(*nmemb, size, &byte_len))
return nullptr;

const cpp::byte *next = reinterpret_cast<const cpp::byte *>(base);
const cpp::byte *end = next + byte_len;
for (; next < end; next += size)
if (compar(key, next) == 0)
return const_cast<cpp::byte *>(next);

*nmemb += 1;
inline_memcpy(const_cast<cpp::byte *>(end), key, size);
return const_cast<cpp::byte *>(end);
}

} // namespace LIBC_NAMESPACE_DECL
20 changes: 20 additions & 0 deletions libc/src/search/lsearch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for lsearch -----------------------*- 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_SEARCH_LSEARCH_H
#define LLVM_LIBC_SRC_SEARCH_LSEARCH_H

#include "src/__support/macros/config.h"
#include <stddef.h> // size_t

namespace LIBC_NAMESPACE_DECL {
void *lsearch(const void *key, void *base, size_t *nmemb, size_t size,
int (*compar)(const void *, const void *));
} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_SEARCH_LSEARCH_H
10 changes: 10 additions & 0 deletions libc/test/src/search/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ add_libc_unittest(
DEPENDS
libc.src.search.lfind
)

add_libc_unittest(
lsearch_test
SUITE
libc_search_unittests
SRCS
lsearch_test.cpp
DEPENDS
libc.src.search.lsearch
)
70 changes: 70 additions & 0 deletions libc/test/src/search/lsearch_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//===-- Unittests for lsearch ---------------------------------------------===//
//
// 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/search/lsearch.h"
#include "test/UnitTest/Test.h"

int compar(const void *a, const void *b) {
return *reinterpret_cast<const int *>(a) != *reinterpret_cast<const int *>(b);
}

TEST(LlvmLibcLsearchTest, SearchHead) {
int list[3] = {1, 2, 3};
size_t len = 3;
int key = 1;
void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
ASSERT_TRUE(ret == &list[0]);
}

TEST(LlvmLibcLsearchTest, SearchMiddle) {
int list[3] = {1, 2, 3};
size_t len = 3;
int key = 2;
void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
ASSERT_TRUE(ret == &list[1]);
}

TEST(LlvmLibcLsearchTest, SearchTail) {
int list[3] = {1, 2, 3};
size_t len = 3;
int key = 3;
void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
ASSERT_TRUE(ret == &list[2]);
}

TEST(LlvmLibcLsearchTest, SearchNonExistent) {
int list[4] = {1, 2, 3, 0};
size_t len = 3;
int key = 4;
void *ret = LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), compar);
ASSERT_TRUE(ret == &list[3]);
ASSERT_EQ(key, list[3]);
ASSERT_EQ(len, 4UL);
}

TEST(LlvmLibcLsearchTest, SearchExceptional) {
int list[3] = {1, 2, 3};
size_t len = 3;
size_t max_len = ~0;
int key = 3;
void *ret_key =
LIBC_NAMESPACE::lsearch(nullptr, list, &len, sizeof(int), compar);
ASSERT_TRUE(ret_key == nullptr);
void *ret_base =
LIBC_NAMESPACE::lsearch(&key, nullptr, &len, sizeof(int), compar);
ASSERT_TRUE(ret_base == nullptr);
void *ret_nmemb =
LIBC_NAMESPACE::lsearch(&key, list, nullptr, sizeof(int), compar);
ASSERT_TRUE(ret_nmemb == nullptr);
void *ret_size =
LIBC_NAMESPACE::lsearch(&key, list, &max_len, sizeof(int), compar);
ASSERT_TRUE(ret_size == nullptr);
void *ret_compar =
LIBC_NAMESPACE::lsearch(&key, list, &len, sizeof(int), nullptr);
ASSERT_TRUE(ret_compar == nullptr);
}
Loading