feat(libc): implement memrchr

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MarcZX100 2026-10-11 01:16:53 +02:00 committed by LeandroLP23
parent 5b4709f283
commit 890169e094
3 changed files with 36 additions and 0 deletions

View File

@ -233,6 +233,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_hostname PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_memrchr_tests ${LIBS_INCLUDE_DIR}/tests/GuestMemrchr.cpp)
target_include_directories(guest_memrchr_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_memrchr_tests PRIVATE libc)
configure_windows_unwind(guest_memrchr_tests)
add_test(NAME guest_memrchr COMMAND guest_memrchr_tests)
if(WIN32)
set_tests_properties(guest_memrchr PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test_executable(guest_lifecycle_tests ${LIBS_INCLUDE_DIR}/tests/GuestLifecycle.cpp)
target_include_directories(guest_lifecycle_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_lifecycle_tests PRIVATE libc)

View File

@ -73,6 +73,16 @@ const void* APS5_VABI memchr_nid_postfix(const void* s, int c, size_t n) {
return std::memchr(s, c, n);
}
void* APS5_VABI memrchr_nid_postfix(const void* s, int c, size_t n) {
const auto* bytes = static_cast<const unsigned char*>(s);
const auto value = static_cast<unsigned char>(c);
while (n != 0) {
--n;
if (bytes[n] == value) return const_cast<unsigned char*>(bytes + n);
}
return nullptr;
}
int APS5_VABI strcmp_nid_postfix(const char* s1, const char* s2) {
return std::strcmp(s1, s2);
}

View File

@ -0,0 +1,17 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdlib>
extern "C" {
void* APS5_VABI memrchr_nid_postfix(const void*, int, std::size_t);
}
int main() {
const unsigned char bytes[] = {0xff, 0x01, 0xff, 0x00};
const auto* found = static_cast<const unsigned char*>(memrchr_nid_postfix(bytes, 0xff, sizeof(bytes)));
if (found != bytes + 2) std::abort();
found = static_cast<const unsigned char*>(memrchr_nid_postfix(bytes, 0x1ff, sizeof(bytes)));
if (found != bytes + 2) std::abort();
found = static_cast<const unsigned char*>(memrchr_nid_postfix(bytes, 0xff, 2));
if (found != bytes) std::abort();
if (memrchr_nid_postfix(bytes, 0x02, sizeof(bytes)) != nullptr) std::abort();
}