libluna+libc: Implement memchr() and strstr()
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
56f3d26969
commit
15199a2366
@ -23,6 +23,9 @@ extern "C"
|
|||||||
/* Copy n bytes of memory from src to dest, where src and dest may overlap. */
|
/* Copy n bytes of memory from src to dest, where src and dest may overlap. */
|
||||||
void* memmove(void* dest, const void* src, size_t n);
|
void* memmove(void* dest, const void* src, size_t n);
|
||||||
|
|
||||||
|
/* Find the character c in n bytes of memory.*/
|
||||||
|
void* memchr(const void* buf, int c, size_t n);
|
||||||
|
|
||||||
/* Return the length of a null-terminated string. */
|
/* Return the length of a null-terminated string. */
|
||||||
size_t strlen(const char* str);
|
size_t strlen(const char* str);
|
||||||
|
|
||||||
@ -80,6 +83,9 @@ extern "C"
|
|||||||
/* Copy up to max bytes of src into dest, adding a null terminator at the end. */
|
/* Copy up to max bytes of src into dest, adding a null terminator at the end. */
|
||||||
size_t strlcpy(char* dest, const char* src, size_t max);
|
size_t strlcpy(char* dest, const char* src, size_t max);
|
||||||
|
|
||||||
|
/* Locate a string (needle) in another one (haystack). */
|
||||||
|
char* strstr(const char* haystack, const char* needle);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -7,6 +7,7 @@ extern "C"
|
|||||||
void* memset(void* buf, int c, usize n);
|
void* memset(void* buf, int c, usize n);
|
||||||
int memcmp(const void* a, const void* b, usize n);
|
int memcmp(const void* a, const void* b, usize n);
|
||||||
void* memmove(void* dest, const void* src, usize n);
|
void* memmove(void* dest, const void* src, usize n);
|
||||||
|
void* memchr(const void* buf, int c, size_t n);
|
||||||
|
|
||||||
usize strlen(const char* str);
|
usize strlen(const char* str);
|
||||||
usize strnlen(const char* str, usize max);
|
usize strnlen(const char* str, usize max);
|
||||||
@ -38,4 +39,6 @@ extern "C"
|
|||||||
char* strrchr(const char* str, int c);
|
char* strrchr(const char* str, int c);
|
||||||
|
|
||||||
char* strpbrk(const char* s, const char* accept);
|
char* strpbrk(const char* s, const char* accept);
|
||||||
|
|
||||||
|
char* strstr(const char* haystack, const char* needle);
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,15 @@ extern "C"
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void* memchr(const void* buf, int c, usize n)
|
||||||
|
{
|
||||||
|
for (usize i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
if (*((const u8*)buf + i) == (u8)c) return const_cast<void*>(buf);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
int memcmp(const void* a, const void* b, usize n)
|
int memcmp(const void* a, const void* b, usize n)
|
||||||
{
|
{
|
||||||
if (!n) return 0;
|
if (!n) return 0;
|
||||||
@ -264,4 +273,28 @@ extern "C"
|
|||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char* strstr(const char* haystack, const char* needle)
|
||||||
|
{
|
||||||
|
const char* i = haystack; // keeps track of the current haystack position
|
||||||
|
const char* j = needle; // keeps track of the current needle position
|
||||||
|
const char* k = haystack; // keeps track of the first matched character
|
||||||
|
|
||||||
|
while (*i)
|
||||||
|
{
|
||||||
|
if (*j == 0) return const_cast<char*>(k);
|
||||||
|
if (*i != *j)
|
||||||
|
{
|
||||||
|
j = needle;
|
||||||
|
k = i + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
j++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*j == 0) return const_cast<char*>(k);
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ luna_test(libluna/TestFormat.cpp TestFormat)
|
|||||||
luna_test(libluna/TestHashTable.cpp TestHashTable)
|
luna_test(libluna/TestHashTable.cpp TestHashTable)
|
||||||
luna_test(libluna/TestCPath.cpp TestCPath)
|
luna_test(libluna/TestCPath.cpp TestCPath)
|
||||||
luna_test(libc/TestScanf.cpp TestScanf)
|
luna_test(libc/TestScanf.cpp TestScanf)
|
||||||
|
luna_test(libc/TestString.cpp TestString)
|
||||||
|
|
||||||
luna_app(run-tests.cpp run-tests)
|
luna_app(run-tests.cpp run-tests)
|
||||||
endif()
|
endif()
|
||||||
|
24
tests/libc/TestString.cpp
Normal file
24
tests/libc/TestString.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <test.h>
|
||||||
|
|
||||||
|
TestResult test_strstr()
|
||||||
|
{
|
||||||
|
const char* find = "Hello, world!";
|
||||||
|
|
||||||
|
validate(strstr(find, "Hello") == find);
|
||||||
|
validate(strstr(find, "!") == find + 12);
|
||||||
|
validate(strstr(find, "world") == find + 7);
|
||||||
|
validate(strstr(find, "Hullo") == nullptr);
|
||||||
|
validate(strstr(find, "notfound") == nullptr);
|
||||||
|
|
||||||
|
test_success;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<void> test_main()
|
||||||
|
{
|
||||||
|
test_prelude;
|
||||||
|
|
||||||
|
run_test(test_strstr);
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
@ -7,6 +7,6 @@ cd $LUNA_ROOT
|
|||||||
|
|
||||||
fakeroot -u -s $LUNA_ROOT/.fakeroot -- tools/install.sh
|
fakeroot -u -s $LUNA_ROOT/.fakeroot -- tools/install.sh
|
||||||
|
|
||||||
fakeroot -u -i $LUNA_ROOT/.fakeroot -- genext2fs -d base -B 4096 -b 1024 -L luna-rootfs -N 1024 build/ext2fs.bin
|
fakeroot -u -i $LUNA_ROOT/.fakeroot -- genext2fs -d base -B 4096 -b 2048 -L luna-rootfs -N 2048 build/ext2fs.bin
|
||||||
|
|
||||||
mkbootimg luna.json Luna.iso
|
mkbootimg luna.json Luna.iso
|
||||||
|
@ -14,9 +14,9 @@ tools/install-headers.sh
|
|||||||
cmake -S . -B $LUNA_BUILD_DIR -G "$LUNA_CMAKE_GENERATOR_NAME" -DBUILD_TESTS=ON
|
cmake -S . -B $LUNA_BUILD_DIR -G "$LUNA_CMAKE_GENERATOR_NAME" -DBUILD_TESTS=ON
|
||||||
cmake --build $LUNA_BUILD_DIR
|
cmake --build $LUNA_BUILD_DIR
|
||||||
|
|
||||||
rm initrd/etc/init/*
|
rm base/etc/init/*
|
||||||
|
|
||||||
printf "Name=test\nCommand=/bin/run-tests\nWait=true\n" > initrd/etc/init/00-tests
|
printf "Name=test\nCommand=/bin/run-tests\nWait=true\n" > base/etc/init/00-tests
|
||||||
|
|
||||||
tools/make-iso.sh
|
tools/make-iso.sh
|
||||||
|
|
||||||
@ -24,6 +24,6 @@ set +e
|
|||||||
tools/fast-run.sh
|
tools/fast-run.sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
rm initrd/etc/init/00-tests
|
rm base/etc/init/00-tests
|
||||||
|
|
||||||
git checkout initrd/etc/init/
|
git checkout base/etc/init/
|
||||||
|
Loading…
Reference in New Issue
Block a user