diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2014-09-18 18:55:44 +0000 |
commit | 3534b428629be185e096be99e3bd5fdfe32d5544 (patch) | |
tree | 210ef3198ed66bc7f7b7bf6a85e4579f455e5a36 /src/libstddjb/str_strn.c | |
download | skalibs-3534b428629be185e096be99e3bd5fdfe32d5544.tar.xz |
initial commit with rc for skalibs-2.0.0.0
Diffstat (limited to 'src/libstddjb/str_strn.c')
-rw-r--r-- | src/libstddjb/str_strn.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/libstddjb/str_strn.c b/src/libstddjb/str_strn.c new file mode 100644 index 0000000..7c2e425 --- /dev/null +++ b/src/libstddjb/str_strn.c @@ -0,0 +1,36 @@ +/* ISC license. */ + +#include <skalibs/config.h> +#include <skalibs/bytestr.h> + +#ifdef SKALIBS_FLAG_REPLACE_LIBC + +/* Very naive implementation, but it's small. */ + +unsigned int str_strn (char const *haystack, unsigned int hlen, char const *needle, unsigned int nlen) +{ + register unsigned int i = 0 ; + if (!nlen) return 0 ; + if (hlen < nlen) return hlen ; + hlen -= nlen ; + for (; i <= hlen ; i++) + if (!byte_diff(haystack+i, nlen, needle)) return i ; + return hlen+nlen ; +} + +#else + +#include <string.h> + +unsigned int str_strn (char const *haystack, unsigned int hlen, char const *needle, unsigned int nlen) +{ + char haystack2[hlen+1] ; + char needle2[nlen+1] ; + register char *p ; + byte_copy(haystack2, hlen, haystack) ; haystack2[hlen] = 0 ; + byte_copy(needle2, nlen, needle) ; needle2[nlen] = 0 ; + p = strstr(haystack2, needle2) ; + return p ? p - haystack2 : hlen ; +} + +#endif |