From 7600bc55824921e1119fb0a72fd9fc5a63ca2c18 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Oct 2022 21:13:22 +0200 Subject: [PATCH] libc: Add strings.h --- libs/libc/include/string.h | 3 --- libs/libc/include/strings.h | 27 +++++++++++++++++++++++ libs/libc/src/string.cpp | 5 ----- libs/libc/src/strings.cpp | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 libs/libc/include/strings.h create mode 100644 libs/libc/src/strings.cpp diff --git a/libs/libc/include/string.h b/libs/libc/include/string.h index 88e133b2..854c2eab 100644 --- a/libs/libc/include/string.h +++ b/libs/libc/include/string.h @@ -80,9 +80,6 @@ extern "C" /* Returns the error string associated with the error number err. */ char* strerror(int err); - /* Clears n bytes of buf. */ - void* bzero(void* buf, size_t n); - /* Copies the string src into dest. This function is unsafe, use strlcpy instead. */ __lc_deprecated("strcpy is unsafe and should not be used; use strlcpy instead") char* strcpy(char* dest, const char* src); diff --git a/libs/libc/include/strings.h b/libs/libc/include/strings.h new file mode 100644 index 00000000..dd0952ad --- /dev/null +++ b/libs/libc/include/strings.h @@ -0,0 +1,27 @@ +#ifndef _STRINGS_H +#define _STRINGS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* Clears n bytes of buf. */ + void bzero(void* buf, size_t n); + + /* Copies n bytes of src into dest. */ + void bcopy(void* dest, const void* src, size_t n); + + /* Compares strings a and b while treating uppercase and lowercase characters as the same. */ + int strcasecmp(const char* a, const char* b); + + /* Compares at most max bytes of strings a and b while treating uppercase and lowercase characters as the same. */ + int strncasecmp(const char* a, const char* b, size_t max); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libs/libc/src/string.cpp b/libs/libc/src/string.cpp index 7ba70640..85106fd1 100644 --- a/libs/libc/src/string.cpp +++ b/libs/libc/src/string.cpp @@ -230,11 +230,6 @@ extern "C" return NULL; } - void* bzero(void* buf, size_t n) - { - return memset(buf, 0, n); - } - char* strstr(const char* haystack, const char* needle) { size_t needle_size = strlen(needle); diff --git a/libs/libc/src/strings.cpp b/libs/libc/src/strings.cpp new file mode 100644 index 00000000..5dee123f --- /dev/null +++ b/libs/libc/src/strings.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +static char fold(char c) +{ + if (isalpha(c)) return tolower(c); + return c; +} + +extern "C" +{ + void bzero(void* buf, size_t n) + { + memset(buf, 0, n); + } + + void bcopy(void* dest, const void* src, size_t n) + { + memcpy(dest, src, n); + } + + int strcasecmp(const char* a, const char* b) + { + while (*a && (fold(*a) == fold(*b))) + { + a++; + b++; + } + return (unsigned char)fold(*a) - (unsigned char)fold(*b); + } + + int strncasecmp(const char* a, const char* b, size_t max) + { + const char* base = a; + while (*a && (fold(*a) == fold(*b)) && (size_t)(a - base) < (max - 1)) + { + a++; + b++; + } + return (unsigned char)fold(*a) - (unsigned char)fold(*b); + } +} \ No newline at end of file