[hackers] [sbase] Implement strmem() and use it in join(1) || FRIGN

From: <git_AT_suckless.org>
Date: Fri, 26 Feb 2016 10:54:55 +0100 (CET)

commit 33960886668653dbb35bbba26d8d993aaf4f4d06
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Thu Feb 25 21:44:46 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Fri Feb 26 09:54:46 2016 +0000

    Implement strmem() and use it in join(1)
    
    We want our delimiters to also contain 0 characters and have them
    handled gracefully.
    To accomplish this, I wrote a function strmem(), which looks for a
    certain, arbitrarily long memory subset in a given string.
    memmem() is a GNU extension and forces you to call strlen every time.

diff --git a/Makefile b/Makefile
index 5fd0b50..266ba86 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -75,6 +75,7 @@ LIBUTILSRC =\
         libutil/strcasestr.c\
         libutil/strlcat.c\
         libutil/strlcpy.c\
+ libutil/strmem.c\
         libutil/strsep.c\
         libutil/strtonum.c\
         libutil/unescape.c
diff --git a/join.c b/join.c
index caf592f..3c05a6e 100644
--- a/join.c
+++ b/join.c
_AT_@ -225,7 +225,7 @@ makeline(char *s, size_t len)
                 beg = sp;
 
                 if (sep) {
- if (!(end = utfutf(sp, sep)))
+ if (!(end = strmem(sp, sep, seplen)))
                                 eol = 1;
 
                         if (!eol) {
diff --git a/libutil/strmem.c b/libutil/strmem.c
new file mode 100644
index 0000000..597d5ca
--- /dev/null
+++ b/libutil/strmem.c
_AT_@ -0,0 +1,23 @@
+/* See LICENSE file for copyright and license details. */
+#include <stddef.h>
+#include <string.h>
+
+char *
+strmem(char *haystack, char *needle, size_t needlelen)
+{
+ size_t i;
+
+ for (i = 0; i < needlelen; i++) {
+ if (haystack[i] == '\0') {
+ return NULL;
+ }
+ }
+
+ for (; haystack[i]; i++) {
+ if (!(memcmp(haystack + i - needlelen, needle, needlelen))) {
+ return (haystack + i - needlelen);
+ }
+ }
+
+ return NULL;
+}
diff --git a/util.h b/util.h
index 4c973d1..f10bf46 100644
--- a/util.h
+++ b/util.h
_AT_@ -58,6 +58,8 @@ size_t estrlcpy(char *, const char *, size_t);
 #undef strsep
 char *strsep(char **, const char *);
 
+char *strmem(char *, char *, size_t);
+
 /* regex */
 int enregcomp(int, regex_t *, const char *, int);
 int eregcomp(regex_t *, const char *, int);
Received on Fri Feb 26 2016 - 10:54:55 CET

This archive was generated by hypermail 2.3.0 : Fri Feb 26 2016 - 11:00:19 CET