From d1a48fa371378ff7cc5833958a6f453ee46c4488 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 28 Feb 2026 13:37:40 +0100 Subject: Add strnlen helper Signed-off-by: Carlos Maiolino --- src/include/toxic/string.h | 2 ++ src/lib/string.c | 12 ++++++++++++ 2 files changed, 14 insertions(+) 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 +#include #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; -- cgit v1.2.3