]> git.sur5r.net Git - u-boot/commitdiff
string: Add strcspn()
authorSimon Glass <sjg@chromium.org>
Fri, 19 May 2017 02:09:29 +0000 (20:09 -0600)
committerSimon Glass <sjg@chromium.org>
Thu, 1 Jun 2017 13:03:12 +0000 (07:03 -0600)
Add an implementation of strcspn() which returns the number of initial
characters that do not match any in a rejection list.

Signed-off-by: Simon Glass <sjg@chromium.org>
include/linux/string.h
lib/string.c

index 718c3720a12be743a08bd4087f921b9faa5f797e..36066207392e9c478b1e7a08c389ba34196827a3 100644 (file)
@@ -76,6 +76,21 @@ extern __kernel_size_t strlen(const char *);
 #ifndef __HAVE_ARCH_STRNLEN
 extern __kernel_size_t strnlen(const char *,__kernel_size_t);
 #endif
+
+#ifndef __HAVE_ARCH_STRCSPN
+/**
+ * strcspn() - find span of string without given characters
+ *
+ * Calculates the length of the initial segment of @s which consists entirely
+ * of bsytes not in reject.
+ *
+ * @s: string to search
+ * @reject: strings which cause the search to halt
+ * @return number of characters at the start of @s which are not in @reject
+ */
+size_t strcspn(const char *s, const char *reject);
+#endif
+
 #ifndef __HAVE_ARCH_STRDUP
 extern char * strdup(const char *);
 #endif
index e6e749b80b208ed8889dbb226433687e3905054f..c4ca944bb42c0cabcea15a3cce3aca4f6fbf9029 100644 (file)
@@ -286,6 +286,30 @@ size_t strnlen(const char * s, size_t count)
 }
 #endif
 
+#ifndef __HAVE_ARCH_STRCSPN
+/**
+ * strcspn - Calculate the length of the initial substring of @s which does
+ * not contain letters in @reject
+ * @s: The string to be searched
+ * @reject: The string to avoid
+ */
+size_t strcspn(const char *s, const char *reject)
+{
+       const char *p;
+       const char *r;
+       size_t count = 0;
+
+       for (p = s; *p != '\0'; ++p) {
+               for (r = reject; *r != '\0'; ++r) {
+                       if (*p == *r)
+                               return count;
+               }
+               ++count;
+       }
+       return count;
+}
+#endif
+
 #ifndef __HAVE_ARCH_STRDUP
 char * strdup(const char *s)
 {