[U-Boot] [PATCH v3 39/72] string: Add strcspn()

Simon Glass sjg at chromium.org
Fri May 19 02:09:29 UTC 2017


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 at chromium.org>
---

Changes in v3: None
Changes in v2: None

 include/linux/string.h | 15 +++++++++++++++
 lib/string.c           | 24 ++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index 718c3720a1..3606620739 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -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
diff --git a/lib/string.c b/lib/string.c
index e6e749b80b..c4ca944bb4 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -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)
 {
-- 
2.13.0.303.g4ebf302169-goog



More information about the U-Boot mailing list