summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2026-02-28 13:37:40 +0100
committerCarlos Maiolino <[email protected]>2026-02-28 13:37:40 +0100
commitd1a48fa371378ff7cc5833958a6f453ee46c4488 (patch)
tree033c5949721e7a0ccb387eaf28f9cfee88190c37
parent7b3f6a5a507a59b613998ef9ad2f14252d993d42 (diff)
Add strnlen helper
Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--src/include/toxic/string.h2
-rw-r--r--src/lib/string.c12
2 files changed, 14 insertions, 0 deletions
diff --git a/src/include/toxic/string.h b/src/include/toxic/string.h
index 3cb4a30..b43942b 100644
--- a/src/include/toxic/string.h
+++ b/src/include/toxic/string.h
@@ -2,6 +2,7 @@
#define STRING_H
#include <stddef.h>
+#include <stdbool.h>
#define NULL ((void *)0)
@@ -9,6 +10,7 @@ bool isdigit(char c);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);
char *strcpy(char *restrict dst, const char *restrict src);
+int strncmp(const char *s1, const char *s2, size_t n);
void *memset(void *s, int c, size_t n);
#endif /* STRING_H */
diff --git a/src/lib/string.c b/src/lib/string.c
index 365381b..d50435f 100644
--- a/src/lib/string.c
+++ b/src/lib/string.c
@@ -37,6 +37,18 @@ char *strcpy(char *restrict dst, const char *restrict src)
return dst;
}
+int strncmp(const char *s1, const char *s2, size_t n)
+{
+ int i = 0;
+
+ for (i = 0; i < n; i++) {
+ if (s1[i] != s2[i])
+ return s1[i] - s2[i];
+ }
+
+ return 0;
+}
+
void *memset(void *s, int c, size_t n) {
char *cur = (char*) s;