From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH v2 04/13] string: implement strv_length helper
Date: Wed, 13 Aug 2025 16:33:36 +0200 [thread overview]
Message-ID: <20250813143345.3758653-5-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250813143345.3758653-1-a.fatoum@barebox.org>
For calculating the length of an array of strings with a NULL sentinel,
introduce a strv_length helper.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
v1 -> v2:
- no change
---
include/string.h | 7 +++++++
lib/string.c | 17 +++++++++++++++++
2 files changed, 24 insertions(+)
diff --git a/include/string.h b/include/string.h
index b54a8fc3f38a..71affe48b640 100644
--- a/include/string.h
+++ b/include/string.h
@@ -57,4 +57,11 @@ static inline bool is_nul_terminated(const char *val, size_t len)
return strnlen(val, len) != len;
}
+typedef union {
+ char * const *sl;
+ const char * const *sl_const;
+} strv_t __attribute__((transparent_union));
+
+size_t strv_length(strv_t) __pure;
+
#endif /* __STRING_H */
diff --git a/lib/string.c b/lib/string.c
index 96eac6d1daf6..73637cd9710a 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -1137,3 +1137,20 @@ char *strreplace(char *str, char old, char new)
return str;
}
EXPORT_SYMBOL(strreplace);
+
+/**
+ * strv_length - calculate length of string vector
+ * @strv: NULL-terminated array of string
+ *
+ * Return: size of the vector
+ */
+size_t strv_length(const char * const *l)
+{
+ size_t n = 0;
+
+ for (const char *const *s, *const *i = (l); (s = i) && *i; i++)
+ n++;
+
+ return n;
+}
+EXPORT_SYMBOL(strv_length);
--
2.39.5
next prev parent reply other threads:[~2025-08-13 14:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-13 14:33 [PATCH v2 00/13] commands: add bfetch/buds of command redirection Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 01/13] common: introduce structured I/O Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 02/13] ARM: cpuinfo: support structio output Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 03/13] commands: uptime: enable structured I/O Ahmad Fatoum
2025-08-13 14:33 ` Ahmad Fatoum [this message]
2025-08-13 14:33 ` [PATCH v2 05/13] ARM: psci: client: add PSCI version/method parameters Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 06/13] net: move netmask_to_prefix into header Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 07/13] optee: add revision info to tee devinfo output Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 08/13] tee: enable structured I/O in devinfo handler Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 09/13] security: blobgen: add easy way to check for existent providers Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 10/13] clk: implement clk_have_nonfixed_providers Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 11/13] commands: introduce bfetch command Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 12/13] configs: enable bfetch in some popular defconfigs Ahmad Fatoum
2025-08-13 14:33 ` [PATCH v2 13/13] hush: structio: silence missing command error message Ahmad Fatoum
2025-08-14 10:53 ` [PATCH v2 00/13] commands: add bfetch/buds of command redirection Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250813143345.3758653-5-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox