Skip to contentSkip to conversation

lib: Add strcspn function

PATCH In review Kevin Lampis 3 participants 5 messages May 6, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 lib: Add strcspn function

This will be used by future patches.

---
 xen/include/xen/string.h |  1 +
 xen/lib/Makefile         |  1 +
 xen/lib/strcspn.c        | 22 ++++++++++++++++++++++
 3 files changed, 24 insertions(+)
 create mode 100644 xen/lib/strcspn.c
xen/include/xen/string.h +1 -0
index bd4a8f48e9..70c231b690 100644
@@ -26,6 +26,7 @@ size_t strnlen(const char *s, size_t count);
 char *strpbrk(const char *cs,const char *ct);
 char *strsep(char **s, const char *ct);
 size_t strspn(const char *s, const char *accept);
+size_t strcspn(const char *s, const char *reject);
 
 void *memset(void *s, int c, size_t n);
 void *memcpy(void *dest, const void *src, size_t n);
xen/lib/Makefile +1 -0
index 76dc86fab0..5ccb1e5241 100644
@@ -22,6 +22,7 @@ lib-y += sort.o
 lib-y += strcasecmp.o
 lib-y += strchr.o
 lib-y += strcmp.o
+lib-y += strcspn.o
 lib-y += strlcat.o
 lib-y += strlcpy.o
 lib-y += strlen.o
xen/lib/strcspn.c new file +22 -0
new file mode 100644
index 0000000000..42e3308dac
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/string.h>
+
+/**
+ * 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;
+
+	for (p = s; *p != '\0'; ++p) {
+		if (strchr(reject, *p))
+			break;
+	}
+	return p - s;
+}
Signature · 1 line
2.42.0
#1 lore raw

xen/include/xen/string.h size_t strnlen(const char *s, size_t count); · line 32

Quoted 12 lines · Kevin Lampis
xen/include/xen/string.h +1 -0
index bd4a8f48e9..70c231b690 100644
@@ -26,6 +26,7 @@ size_t strnlen(const char *s, size_t count);
 char *strpbrk(const char *cs,const char *ct);
 char *strsep(char **s, const char *ct);
 size_t strspn(const char *s, const char *accept);
+size_t strcspn(const char *s, const char *reject);
 
 void *memset(void *s, int c, size_t n);
 void *memcpy(void *dest, const void *src, size_t n);

Lower down in this file, you also want a define aliasing to __builtin_strcspn() in the style of the others you'll see.  In turn, you'll need to...

Quoted 9 lines
xen/lib/strcspn.c new file +3 -0
new file mode 100644
index 0000000000..42e3308dac
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds

xen/lib/strcspn.c line 13

Quoted 10 lines
xen/lib/strcspn.c new file +10 -0
+ */
+
+#include <xen/string.h>
+
+/**
+ * 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)

... write this as "size_t (strcspn)(const ..."

to avoid the macro expansion in this one place.

~Andrew

#2 lore raw

PATCH v2 1/1 lib: Add strcspn function

From: Ross Lagerwall <[email protected]>

This will be used by future patches.

---
Changes in v2:
- Add alias to __builtin_strcspn

---
 xen/include/xen/string.h |  5 +++++
 xen/lib/Makefile         |  1 +
 xen/lib/strcspn.c        | 22 ++++++++++++++++++++++
 3 files changed, 28 insertions(+)
 create mode 100644 xen/lib/strcspn.c
xen/include/xen/string.h +5 -0
index bd4a8f48e9..0ad65303da 100644
@@ -26,6 +26,7 @@ size_t strnlen(const char *s, size_t count);
 char *strpbrk(const char *cs,const char *ct);
 char *strsep(char **s, const char *ct);
 size_t strspn(const char *s, const char *accept);
+size_t strcspn(const char *s, const char *reject);
 
 void *memset(void *s, int c, size_t n);
 void *memcpy(void *dest, const void *src, size_t n);
@@ -68,6 +69,10 @@ void *memchr_inv(const void *s, int c, size_t n);
 #define strlen(s1) __builtin_strlen(s1)
 #endif
 
+#ifndef __HAVE_ARCH_STRCSPN
+#define strcspn(s, r) __builtin_strcspn(s, r)
+#endif
+
 #ifndef __HAVE_ARCH_MEMSET
 #define memset(s, c, n) __builtin_memset(s, c, n)
 #endif
xen/lib/Makefile +1 -0
index 76dc86fab0..5ccb1e5241 100644
@@ -22,6 +22,7 @@ lib-y += sort.o
 lib-y += strcasecmp.o
 lib-y += strchr.o
 lib-y += strcmp.o
+lib-y += strcspn.o
 lib-y += strlcat.o
 lib-y += strlcpy.o
 lib-y += strlen.o
xen/lib/strcspn.c new file +22 -0
new file mode 100644
index 0000000000..d572931f54
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ *  Copyright (C) 1991, 1992  Linus Torvalds
+ */
+
+#include <xen/string.h>
+
+/**
+ * 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;
+
+       for (p = s; *p != '\0'; ++p) {
+               if (strchr(reject, *p))
+                       break;
+       }
+       return p - s;
+}
Signature · 1 line
2.42.0
#4 lore raw
Quoted 9 lines · Andrew Cooper
Quoted 6 lines · Kevin Lampis

From: Ross Lagerwall <[email protected]>

This will be used by future patches.

Like for the sha256 change - isn't this introducing a new Misra violation until a caller appears? Or are we deeming this okay here (unlike in the sha256 case) because the CU will only be included in the final image if a caller actually exists?

Jan