From 7886fd559977656efe5a552dc3c635ce267350c4 Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Mon, 26 Aug 2024 17:01:19 +0200 Subject: [PATCH] Added the 'n' functions (ie. strNcpy) to string.h and some minor changes --- include/c/_fileio.h | 3 ++- include/c/stdio.h | 1 + include/c/string.h | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/include/c/_fileio.h b/include/c/_fileio.h index 1a46528..9a9cde8 100644 --- a/include/c/_fileio.h +++ b/include/c/_fileio.h @@ -1,8 +1,9 @@ #ifndef _FILEIO_H #define _FILEIO_H -#include #include +#include +#include // Nobody should have to interface with the internals of this structure typedef struct _iobuf { // 24B diff --git a/include/c/stdio.h b/include/c/stdio.h index fab3b37..08255b1 100644 --- a/include/c/stdio.h +++ b/include/c/stdio.h @@ -4,6 +4,7 @@ #include #include #include +#include /* Declarations */ void putc(char); diff --git a/include/c/string.h b/include/c/string.h index 24878df..a3b25e0 100644 --- a/include/c/string.h +++ b/include/c/string.h @@ -3,6 +3,10 @@ #include +void* memcpy(void* src, void* dst, uint32_t length); +void* memcpy32(void* src, void* dst, uint32_t length); +void* memset(void* dst, uint8_t value, uint32_t length); + // Source: http://www.strudel.org.uk/itoa/ char* itoa(uint32_t val, uint8_t base) { static char buf[32] = { 0 }; @@ -47,6 +51,19 @@ char* strcpy(char* str1, const char* str2) { return str1; } +char* strncpy(char* str1, const char* str2, size_t n) { + char* dst = str1; + const char* src = str2; + while (n > 0) { + n--; + if ((*dst++ = *src++) == '\0') { + memset(dst, '\0', n); + break; + } + } + return str1; +} + char* strcat(char* str1, const char* str2) { char* s = str1; while (*s != '\0') { s++; } @@ -54,12 +71,21 @@ char* strcat(char* str1, const char* str2) { return str1; } +char* strncat(char* str1, const char* str2, size_t n) { + char* s = str1; + while (*s != '\0') { s++; } + strncpy(s, str2, n); + return str1; +} + char* strchr(const char* str, char c) { const char* s = str; while (*s != '\0' && *s != c) { s++; } return (*s == c) ? (char*)s : NULL; } +// char* strrchr(const char* str, char c); + int strcmp(const char* str1, const char* str2) { while (*str1 != '\0' && *str1 == *str2) { str1++; @@ -72,6 +98,20 @@ int strcmp(const char* str1, const char* str2) { return ((c1 < c2) ? -1 : (c1 > c2)); } +int strncmp(const char* str1, const char* str2, size_t n) { + if (n == 0) { return 0; } + while (n-- > 0 && *str1 == *str2) { + if (n == 0 || *str1 == '\0') { return 0; } + str1++; + str2++; + } + + uint8_t c1 = (*(uint8_t*)str1); + uint8_t c2 = (*(uint8_t*)str2); + + return ((c1 < c2) ? -1 : (c1 > c2)); +} + uint32_t strlen(const char* str) { const char* p = str; while (*++p != '\0');