]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strchr/: strchrscnt(): Add function
authorAlejandro Colomar <alx@kernel.org>
Thu, 14 Nov 2024 18:54:33 +0000 (19:54 +0100)
committerSerge Hallyn <serge@hallyn.com>
Fri, 24 Jan 2025 13:58:13 +0000 (07:58 -0600)
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strchr/strchrscnt.c [new file with mode: 0644]
lib/string/strchr/strchrscnt.h [new file with mode: 0644]

index 6ae24fb95b731f7a52357d9ff7b1888cd7a986f4..3d3a96ae7b0c88a9152114ff04efe2ce00486e80 100644 (file)
@@ -192,6 +192,8 @@ libshadow_la_SOURCES = \
        string/sprintf/xasprintf.h \
        string/strchr/strchrcnt.c \
        string/strchr/strchrcnt.h \
+       string/strchr/strchrscnt.c \
+       string/strchr/strchrscnt.h \
        string/strchr/stpspn.c \
        string/strchr/stpspn.h \
        string/strchr/strnul.c \
diff --git a/lib/string/strchr/strchrscnt.c b/lib/string/strchr/strchrscnt.c
new file mode 100644 (file)
index 0000000..8b1a5ec
--- /dev/null
@@ -0,0 +1,12 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strchr/strchrscnt.h"
+
+#include <stddef.h>
+
+
+extern inline size_t strchrscnt(const char *s, const char *c);
diff --git a/lib/string/strchr/strchrscnt.h b/lib/string/strchr/strchrscnt.h
new file mode 100644 (file)
index 0000000..3d423bd
--- /dev/null
@@ -0,0 +1,36 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRSCNT_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRSCNT_H_
+
+
+#include <config.h>
+
+#include <stddef.h>
+
+#include "attr.h"
+#include "string/strchr/strchrcnt.h"
+#include "string/strcmp/streq.h"
+
+
+ATTR_STRING(1)
+ATTR_STRING(2)
+inline size_t strchrscnt(const char *s, const char *c);
+
+
+// Similar to strchrcnt(), but search for multiple characters.
+inline size_t
+strchrscnt(const char *s, const char *c)
+{
+       size_t  n = 0;
+
+       for (; !streq(c, ""); c++)
+               n += strchrcnt(s, *c);
+
+       return n;
+}
+
+
+#endif  // include guard