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.
This commit is contained in:
parent
e8eeb19fcd
commit
3396088666
1
Makefile
1
Makefile
|
@ -75,6 +75,7 @@ LIBUTILSRC =\
|
||||||
libutil/strcasestr.c\
|
libutil/strcasestr.c\
|
||||||
libutil/strlcat.c\
|
libutil/strlcat.c\
|
||||||
libutil/strlcpy.c\
|
libutil/strlcpy.c\
|
||||||
|
libutil/strmem.c\
|
||||||
libutil/strsep.c\
|
libutil/strsep.c\
|
||||||
libutil/strtonum.c\
|
libutil/strtonum.c\
|
||||||
libutil/unescape.c
|
libutil/unescape.c
|
||||||
|
|
2
join.c
2
join.c
|
@ -225,7 +225,7 @@ makeline(char *s, size_t len)
|
||||||
beg = sp;
|
beg = sp;
|
||||||
|
|
||||||
if (sep) {
|
if (sep) {
|
||||||
if (!(end = utfutf(sp, sep)))
|
if (!(end = strmem(sp, sep, seplen)))
|
||||||
eol = 1;
|
eol = 1;
|
||||||
|
|
||||||
if (!eol) {
|
if (!eol) {
|
||||||
|
|
23
libutil/strmem.c
Normal file
23
libutil/strmem.c
Normal file
|
@ -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;
|
||||||
|
}
|
2
util.h
2
util.h
|
@ -58,6 +58,8 @@ size_t estrlcpy(char *, const char *, size_t);
|
||||||
#undef strsep
|
#undef strsep
|
||||||
char *strsep(char **, const char *);
|
char *strsep(char **, const char *);
|
||||||
|
|
||||||
|
char *strmem(char *, char *, size_t);
|
||||||
|
|
||||||
/* regex */
|
/* regex */
|
||||||
int enregcomp(int, regex_t *, const char *, int);
|
int enregcomp(int, regex_t *, const char *, int);
|
||||||
int eregcomp(regex_t *, const char *, int);
|
int eregcomp(regex_t *, const char *, int);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user