changeset 499:c49e2a991b17

lib/string: strsep and strpbrk Taken from the Linux kernel. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Fri, 22 Apr 2011 13:01:01 -0400
parents b42f98fb669d
children de574d051887
files include/string.h lib/string.c
diffstat 2 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/string.h	Fri Apr 22 13:00:00 2011 -0400
+++ b/include/string.h	Fri Apr 22 13:01:01 2011 -0400
@@ -19,6 +19,8 @@
 extern int strncmp(const char *cs, const char *ct, int len);
 extern int strcasecmp(const char *s1, const char *s2);
 extern char *strncpy(char *dest, const char *src, size_t count);
+extern char *strpbrk(const char *cs, const char *ct);
+extern char *strsep(char **s, const char *ct);
 
 static inline int toupper(int c)
 {
--- a/lib/string.c	Fri Apr 22 13:00:00 2011 -0400
+++ b/lib/string.c	Fri Apr 22 13:01:01 2011 -0400
@@ -127,6 +127,50 @@
 	return dest;
 }
 
+/**
+ * strpbrk - Find the first occurrence of a set of characters
+ * @cs: The string to be searched
+ * @ct: The characters to search for
+ */
+char *strpbrk(const char *cs, const char *ct)
+{
+	const char *sc1, *sc2;
+
+	for (sc1 = cs; *sc1 != '\0'; ++sc1) {
+		for (sc2 = ct; *sc2 != '\0'; ++sc2) {
+			if (*sc1 == *sc2)
+				return (char *)sc1;
+		}
+	}
+	return NULL;
+}
+
+/**
+ * strsep - Split a string into tokens
+ * @s: The string to be searched
+ * @ct: The characters to search for
+ *
+ * strsep() updates @s to point after the token, ready for the next call.
+ *
+ * It returns empty tokens, too, behaving exactly like the libc function
+ * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
+ * Same semantics, slimmer shape. ;)
+ */
+char *strsep(char **s, const char *ct)
+{
+	char *sbegin = *s;
+	char *end;
+
+	if (sbegin == NULL)
+		return NULL;
+
+	end = strpbrk(sbegin, ct);
+	if (end)
+		*end++ = '\0';
+	*s = end;
+	return sbegin;
+}
+
 /* ASCII character info */
 unsigned char _ascii_ctype[] = {
 _C,_C,_C,_C,_C,_C,_C,_C,                        /* 0-7 */