From d71f1f24e4253ff0c45493f3b7fab83d6795bcaf Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 2 Apr 2023 00:00:29 +0300 Subject: [PATCH] LibC: Combine string.h functions definitions to single file --- libc/Makefile | 12 +-- libc/string.cpp | 163 +++++++++++++++++++++++++++++++++++++++ libc/string/memcmp.cpp | 13 ---- libc/string/memcpy.cpp | 10 --- libc/string/memmove.cpp | 20 ----- libc/string/memset.cpp | 9 --- libc/string/strcmp.cpp | 11 --- libc/string/strcpy.cpp | 10 --- libc/string/strerror.cpp | 60 -------------- libc/string/strlen.cpp | 9 --- libc/string/strncmp.cpp | 11 --- libc/string/strncpy.cpp | 11 --- libc/string/strstr.cpp | 9 --- 13 files changed, 164 insertions(+), 184 deletions(-) create mode 100644 libc/string.cpp delete mode 100644 libc/string/memcmp.cpp delete mode 100644 libc/string/memcpy.cpp delete mode 100644 libc/string/memmove.cpp delete mode 100644 libc/string/memset.cpp delete mode 100644 libc/string/strcmp.cpp delete mode 100644 libc/string/strcpy.cpp delete mode 100644 libc/string/strerror.cpp delete mode 100644 libc/string/strlen.cpp delete mode 100644 libc/string/strncmp.cpp delete mode 100644 libc/string/strncpy.cpp delete mode 100644 libc/string/strstr.cpp diff --git a/libc/Makefile b/libc/Makefile index d8d5b2e856..2ceda71da8 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -36,17 +36,7 @@ stdio/printf.o \ stdio/putchar.o \ stdio/puts.o \ stdlib/abort.o \ -string/memcmp.o \ -string/memcpy.o \ -string/memmove.o \ -string/memset.o \ -string/strcmp.o \ -string/strcpy.o \ -string/strerror.o \ -string/strlen.o \ -string/strncmp.o \ -string/strncpy.o \ -string/strstr.o \ +string.o \ HOSTEDOBJS=\ $(ARCH_HOSTEDOBJS) \ diff --git a/libc/string.cpp b/libc/string.cpp new file mode 100644 index 0000000000..08dc484470 --- /dev/null +++ b/libc/string.cpp @@ -0,0 +1,163 @@ +#include +#include + +int errno = 0; + +int memcmp(const void* s1, const void* s2, size_t n) +{ + const unsigned char* a = static_cast(s1); + const unsigned char* b = static_cast(s2); + + for (size_t i = 0; i < n; i++) + if (a[i] != b[i]) + return a[i] - b[i]; + + return 0; +} + +void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n) +{ + unsigned char* dst = static_cast(dstp); + const unsigned char* src = static_cast(srcp); + for (size_t i = 0; i < n; i++) + dst[i] = src[i]; + return dstp; +} + +void* memmove(void* destp, const void* srcp, size_t n) +{ + unsigned char* dest = static_cast(destp); + const unsigned char* src = static_cast(srcp); + + if (dest < src) + { + for (size_t i = 0; i < n; i++) + dest[i] = src[i]; + } + else + { + for (size_t i = 1; i <= n; i++) + dest[n - i] = src[n - i]; + } + + return destp; +} + +void* memset(void* s, int c, size_t n) +{ + unsigned char* p = static_cast(s); + for (size_t i = 0; i < n; i++) + p[i] = c; + return s; +} + +int strcmp(const char* s1, const char* s2) +{ + const unsigned char* u1 = (unsigned char*)s1; + const unsigned char* u2 = (unsigned char*)s2; + for (; *u1 && *u2; u1++, u2++) + if (*u1 != *u2) + break; + return *u1 - *u2; +} + +char* strcpy(char* __restrict dest, const char* __restrict src) +{ + size_t i; + for (i = 0; src[i]; i++) + dest[i] = src[i]; + dest[i] = '\0'; + return dest; +} + +char* strerror(int error) +{ + static char buffer[100]; + buffer[0] = 0; + + switch (error) + { + case ENOMEM: + strcpy(buffer, "Cannot allocate memory"); + break; + case EINVAL: + strcpy(buffer, "Invalid argument"); + break; + case EISDIR: + strcpy(buffer, "Is a directory"); + break; + case ENOTDIR: + strcpy(buffer, "Not a directory"); + break; + case ENOENT: + strcpy(buffer, "No such file or directory"); + break; + case EIO: + strcpy(buffer, "Input/output error"); + break; + case ENOTSUP: + strcpy(buffer, "Operation not supported"); + break; + case EBADF: + strcpy(buffer, "Bad file descriptor"); + break; + case EEXISTS: + strcpy(buffer, "File exists"); + break; + case ENOTEMPTY: + strcpy(buffer, "Directory not empty"); + break; + case ENAMETOOLONG: + strcpy(buffer, "Filename too long"); + break; + case ENOBUFS: + strcpy(buffer, "No buffer space available"); + break; + default: + { + // FIXME: sprintf + //sprintf(buffer, "Unknown error %d", error); + strcpy(buffer, "Unknown error"); + errno = EINVAL; + break; + } + } + + return buffer; +} + +size_t strlen(const char* str) +{ + size_t len = 0; + while (str[len]) + len++; + return len; +} + +int strncmp(const char* s1, const char* s2, size_t n) +{ + const unsigned char* u1 = (unsigned char*)s1; + const unsigned char* u2 = (unsigned char*)s2; + for (; --n && *u1 && *u2; u1++, u2++) + if (*u1 != *u2) + break; + return *u1 - *u2; +} + +char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) +{ + size_t i; + for (i = 0; src[i] && i < n; i++) + dest[i] = src[i]; + for (; i < n; i++) + dest[i] = '\0'; + return dest; +} + +char* strstr(const char* haystack, const char* needle) +{ + for (size_t i = 0; haystack[i]; i++) + if (memcmp(haystack + i, needle, strlen(needle)) == 0) + return (char*)haystack + i; + return NULL; +} diff --git a/libc/string/memcmp.cpp b/libc/string/memcmp.cpp deleted file mode 100644 index 8e52d0c550..0000000000 --- a/libc/string/memcmp.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include - -int memcmp(const void* s1, const void* s2, size_t n) -{ - const unsigned char* a = static_cast(s1); - const unsigned char* b = static_cast(s2); - - for (size_t i = 0; i < n; i++) - if (a[i] != b[i]) - return a[i] - b[i]; - - return 0; -} diff --git a/libc/string/memcpy.cpp b/libc/string/memcpy.cpp deleted file mode 100644 index 608caf2551..0000000000 --- a/libc/string/memcpy.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -void* memcpy(void* __restrict dstp, const void* __restrict srcp, size_t n) -{ - unsigned char* dst = static_cast(dstp); - const unsigned char* src = static_cast(srcp); - for (size_t i = 0; i < n; i++) - dst[i] = src[i]; - return dstp; -} \ No newline at end of file diff --git a/libc/string/memmove.cpp b/libc/string/memmove.cpp deleted file mode 100644 index dfd3d49880..0000000000 --- a/libc/string/memmove.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include - -void* memmove(void* destp, const void* srcp, size_t n) -{ - unsigned char* dest = static_cast(destp); - const unsigned char* src = static_cast(srcp); - - if (dest < src) - { - for (size_t i = 0; i < n; i++) - dest[i] = src[i]; - } - else - { - for (size_t i = 1; i <= n; i++) - dest[n - i] = src[n - i]; - } - - return destp; -} diff --git a/libc/string/memset.cpp b/libc/string/memset.cpp deleted file mode 100644 index 9075d949ef..0000000000 --- a/libc/string/memset.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -void* memset(void* s, int c, size_t n) -{ - unsigned char* p = static_cast(s); - for (size_t i = 0; i < n; i++) - p[i] = c; - return s; -} \ No newline at end of file diff --git a/libc/string/strcmp.cpp b/libc/string/strcmp.cpp deleted file mode 100644 index 343d836910..0000000000 --- a/libc/string/strcmp.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -int strcmp(const char* s1, const char* s2) -{ - const unsigned char* u1 = (unsigned char*)s1; - const unsigned char* u2 = (unsigned char*)s2; - for (; *u1 && *u2; u1++, u2++) - if (*u1 != *u2) - break; - return *u1 - *u2; -} diff --git a/libc/string/strcpy.cpp b/libc/string/strcpy.cpp deleted file mode 100644 index 14fcec6d54..0000000000 --- a/libc/string/strcpy.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include - -char* strcpy(char* __restrict dest, const char* __restrict src) -{ - size_t i; - for (i = 0; src[i]; i++) - dest[i] = src[i]; - dest[i] = '\0'; - return dest; -} \ No newline at end of file diff --git a/libc/string/strerror.cpp b/libc/string/strerror.cpp deleted file mode 100644 index 0d8a9c0f39..0000000000 --- a/libc/string/strerror.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include - -int errno = 0; - -char* strerror(int error) -{ - static char buffer[100]; - buffer[0] = 0; - - switch (error) - { - case ENOMEM: - strcpy(buffer, "Cannot allocate memory"); - break; - case EINVAL: - strcpy(buffer, "Invalid argument"); - break; - case EISDIR: - strcpy(buffer, "Is a directory"); - break; - case ENOTDIR: - strcpy(buffer, "Not a directory"); - break; - case ENOENT: - strcpy(buffer, "No such file or directory"); - break; - case EIO: - strcpy(buffer, "Input/output error"); - break; - case ENOTSUP: - strcpy(buffer, "Operation not supported"); - break; - case EBADF: - strcpy(buffer, "Bad file descriptor"); - break; - case EEXISTS: - strcpy(buffer, "File exists"); - break; - case ENOTEMPTY: - strcpy(buffer, "Directory not empty"); - break; - case ENAMETOOLONG: - strcpy(buffer, "Filename too long"); - break; - case ENOBUFS: - strcpy(buffer, "No buffer space available"); - break; - default: - { - // FIXME: sprintf - //sprintf(buffer, "Unknown error %d", error); - strcpy(buffer, "Unknown error"); - errno = EINVAL; - break; - } - } - - return buffer; -} \ No newline at end of file diff --git a/libc/string/strlen.cpp b/libc/string/strlen.cpp deleted file mode 100644 index 22b301dc10..0000000000 --- a/libc/string/strlen.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -size_t strlen(const char* str) -{ - size_t len = 0; - while (str[len]) - len++; - return len; -} \ No newline at end of file diff --git a/libc/string/strncmp.cpp b/libc/string/strncmp.cpp deleted file mode 100644 index b4cf3b797f..0000000000 --- a/libc/string/strncmp.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -int strncmp(const char* s1, const char* s2, size_t n) -{ - const unsigned char* u1 = (unsigned char*)s1; - const unsigned char* u2 = (unsigned char*)s2; - for (; --n && *u1 && *u2; u1++, u2++) - if (*u1 != *u2) - break; - return *u1 - *u2; -} diff --git a/libc/string/strncpy.cpp b/libc/string/strncpy.cpp deleted file mode 100644 index 729a3c59b6..0000000000 --- a/libc/string/strncpy.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -char* strncpy(char* __restrict dest, const char* __restrict src, size_t n) -{ - size_t i; - for (i = 0; src[i] && i < n; i++) - dest[i] = src[i]; - for (; i < n; i++) - dest[i] = '\0'; - return dest; -} \ No newline at end of file diff --git a/libc/string/strstr.cpp b/libc/string/strstr.cpp deleted file mode 100644 index 3094eb9cbc..0000000000 --- a/libc/string/strstr.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include - -char* strstr(const char* haystack, const char* needle) -{ - for (size_t i = 0; haystack[i]; i++) - if (memcmp(haystack + i, needle, strlen(needle)) == 0) - return (char*)haystack + i; - return NULL; -} \ No newline at end of file