str_extra.c

Extra string functions library for C
git clone git://git.finwo.net/lib/str_extra.c
Log | Files | Refs | README

commit 9ddfa86916806e26a5fd1662e0720a3bfc13822b
Author: finwo <finwo@pm.me>
Date:   Sun, 13 Aug 2023 21:39:09 +0200

Initial commit

Diffstat:
A.gitignore | 1+
Aconfig.mk | 1+
Apackage.ini | 7+++++++
Asrc/strnstr.c | 26++++++++++++++++++++++++++
Asrc/strnstr.h | 3+++
5 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/config.mk b/config.mk @@ -0,0 +1 @@ +SRC+=__DIRNAME/src/strnstr.c diff --git a/package.ini b/package.ini @@ -0,0 +1,7 @@ +[export] +config.mk=config.mk +include/finwo/strnstr.h=src/strnstr.h + +[package] +deps=lib +name=finwo/str_extra diff --git a/src/strnstr.c b/src/strnstr.c @@ -0,0 +1,26 @@ +#include "string.h" + +#include "strnstr.h" + +// Origin: https://stackoverflow.com/a/25705264/2928176 +// Author: Chris Dodd (https://stackoverflow.com/users/16406/chris-dodd) +char *strnstr(const char *haystack, const char *needle, size_t len) { + int i; + size_t needle_len; + + if (0 == (needle_len = strnlen(needle, len))) { + return NULL; + } + + for (i=0; i<=(int)(len-needle_len); i++) { + if ( + (haystack[0] == needle[0]) && + (0 == strncmp(haystack, needle, needle_len)) + ) { + return (char *)haystack; + } + haystack++; + } + + return NULL; +} diff --git a/src/strnstr.h b/src/strnstr.h @@ -0,0 +1,3 @@ +#include <stdlib.h> + +char *strnstr(const char *haystack, const char *needle, size_t len);