From 17c6b30949ce7d4a293862ec8f9b873e17fe0cd8 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 17 Oct 2019 20:38:19 +0200 Subject: Optimized string functions --- src/kernel/lib/lib.h | 6 ++---- src/kernel/lib/string.c | 35 ++++++++++++++--------------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/kernel/lib/lib.h b/src/kernel/lib/lib.h index 03b4a3e..6963498 100644 --- a/src/kernel/lib/lib.h +++ b/src/kernel/lib/lib.h @@ -22,17 +22,15 @@ size_t strcmp(const char *s1, const char *s2); * Append the data of src to dest * @param dest The string destination pointer * @param src The string pointer that will get appended - * @return The modified dest pointer */ -char *strcat(char *dest, const char *src); +void strcat(char *dest, const char *src); /** * Copy the data of src to dest * @param dest The copying destination pointer (gets replaced) * @param src The string pointer that will get copied - * @return The modified dest pointer */ -char *strcpy(char *dest, const char *src); +void strcpy(char *dest, const char *src); /** * Copy n data from src to dest diff --git a/src/kernel/lib/string.c b/src/kernel/lib/string.c index 1e41d15..11e6021 100644 --- a/src/kernel/lib/string.c +++ b/src/kernel/lib/string.c @@ -8,29 +8,22 @@ size_t strlen(const char *str) { } size_t strcmp(const char *s1, const char *s2) { - while (*s1 && (*s1 == *s2)) { - s1++; - s2++; - } - return *(const unsigned char *) s1 - *(const unsigned char *) s2; + size_t s_a = strlen(s1); + for (size_t i = 0; i < s_a; i++) if (s1[i] != s2[i]) return 1; + return 0; } -char *strcat(char *dest, const char *src) { - unsigned int i = 0; - unsigned int j = 0; - for (i = 0; dest[i] != 0; i++) {} - for (j = 0; src[j] != 0; j++) { - dest[i + j] = src[j]; - } - dest[i + j] = 0; - return dest; +void strcat(char *dest, const char *src) { + size_t s_dest = strlen(dest); + size_t s_orig = strlen(src); + + for (size_t i = 0; i < s_orig; i++) dest[s_dest + i] = src[i]; + dest[s_dest + s_orig] = 0; } -char *strcpy(char *dest, const char *src) { - unsigned int i = 0; - for (i = 0; src[i] != 0; i++) { - dest[i] = src[i]; - } - dest[i] = 0; - return dest; +void strcpy(char *dest, const char *src) { + size_t s_orig = strlen(src); + + for (size_t i = 0; i < s_orig; i++) dest[i] = src[i]; + dest[s_orig] = 0; } -- cgit v1.2.3