[PATCH v4 08/14] lib: string: Add strlower()
Simon Glass
sjg at chromium.org
Wed Jul 1 13:42:28 CEST 2026
Add a helper that lower-cases an ASCII string in place. It returns
the input pointer so calls can be chained:
do_thing(strlower(name));
A new test in test/lib/string.c covers the basic cases.
Signed-off-by: Simon Glass <sjg at chromium.org>
---
(no changes since v1)
include/linux/string.h | 12 ++++++++++++
lib/string.c | 10 ++++++++++
test/lib/string.c | 27 +++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
diff --git a/include/linux/string.h b/include/linux/string.h
index 850356d7c3f..35bc747300f 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -49,6 +49,18 @@ int strcasecmp(const char *s1, const char *s2);
#ifndef __HAVE_ARCH_STRNCASECMP
extern int strncasecmp(const char *s1, const char *s2, __kernel_size_t len);
#endif
+
+/**
+ * strlower() - Lower-case an ASCII string in place
+ * @s: The string to convert
+ *
+ * Walks @s and replaces each character with its lower-case equivalent.
+ * Returns @s so the call can be chained, e.g. ``foo(strlower(buf))``.
+ *
+ * Return: @s.
+ */
+char *strlower(char *s);
+
#ifndef __HAVE_ARCH_STRCHR
extern char * strchr(const char *,int);
#endif
diff --git a/lib/string.c b/lib/string.c
index 37ea8c29561..7ef439f9f35 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -65,6 +65,16 @@ int strcasecmp(const char *s1, const char *s2)
return strncasecmp(s1, s2, -1U);
}
+char *strlower(char *s)
+{
+ char *p;
+
+ for (p = s; *p; p++)
+ *p = tolower(*p);
+
+ return s;
+}
+
char * ___strtok;
#ifndef __HAVE_ARCH_STRCPY
diff --git a/test/lib/string.c b/test/lib/string.c
index db6f28dbfdf..2e21ae5ccb8 100644
--- a/test/lib/string.c
+++ b/test/lib/string.c
@@ -332,3 +332,30 @@ static int lib_strim(struct unit_test_state *uts)
return 0;
}
LIB_TEST(lib_strim, 0);
+
+static int lib_strlower(struct unit_test_state *uts)
+{
+ char buf[32];
+ char *p;
+
+ /* Mixed case is fully lowered, returns @s */
+ strcpy(buf, "SHA256");
+ p = strlower(buf);
+ ut_asserteq_ptr(p, buf);
+ ut_asserteq_str("sha256", buf);
+
+ /* Already lower-case is unchanged */
+ strcpy(buf, "abc");
+ ut_asserteq_str("abc", strlower(buf));
+
+ /* Non-letters and digits pass through */
+ strcpy(buf, "Hello, World! 1234");
+ ut_asserteq_str("hello, world! 1234", strlower(buf));
+
+ /* Empty string */
+ strcpy(buf, "");
+ ut_asserteq_str("", strlower(buf));
+
+ return 0;
+}
+LIB_TEST(lib_strlower, 0);
--
2.43.0
More information about the U-Boot
mailing list