]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strchr/: strchrcnt(): Add function
authorAlejandro Colomar <alx@kernel.org>
Thu, 4 Jul 2024 13:30:58 +0000 (15:30 +0200)
committerSerge Hallyn <serge@hallyn.com>
Mon, 11 Nov 2024 05:07:19 +0000 (23:07 -0600)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strchr/strchrcnt.c [new file with mode: 0644]
lib/string/strchr/strchrcnt.h [new file with mode: 0644]

index 96393354b51dc2ca05335a919bdd1ff08a9a742d..e76e7446a90ed8decfb7055bbe2d205d65ce8f0a 100644 (file)
@@ -184,6 +184,8 @@ libshadow_la_SOURCES = \
        string/sprintf/stpeprintf.h \
        string/sprintf/xasprintf.c \
        string/sprintf/xasprintf.h \
+       string/strchr/strchrcnt.c \
+       string/strchr/strchrcnt.h \
        string/strchr/stpspn.c \
        string/strchr/stpspn.h \
        string/strchr/strnul.c \
diff --git a/lib/string/strchr/strchrcnt.c b/lib/string/strchr/strchrcnt.c
new file mode 100644 (file)
index 0000000..793b2ad
--- /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/strchrcnt.h"
+
+#include <stddef.h>
+
+
+extern inline size_t strchrcnt(const char *s, char c);
diff --git a/lib/string/strchr/strchrcnt.h b/lib/string/strchr/strchrcnt.h
new file mode 100644 (file)
index 0000000..1a1923e
--- /dev/null
@@ -0,0 +1,35 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_
+
+
+#include <config.h>
+
+#include <stddef.h>
+
+#include "attr.h"
+#include "string/strcmp/streq.h"
+
+
+ATTR_STRING(1)
+inline size_t strchrcnt(const char *s, char c);
+
+
+inline size_t
+strchrcnt(const char *s, char c)
+{
+       size_t  n = 0;
+
+       for (; !streq(s, ""); s++) {
+               if (*s == c)
+                       n++;
+       }
+
+       return n;
+}
+
+
+#endif  // include guard