From 9efce1ac859c4be28761223109b6c7f81a9960f0 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Thu, 4 Jul 2024 15:30:58 +0200 Subject: [PATCH] lib/string/strchr/: strchrcnt(): Add function Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 2 ++ lib/string/strchr/strchrcnt.c | 12 ++++++++++++ lib/string/strchr/strchrcnt.h | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 lib/string/strchr/strchrcnt.c create mode 100644 lib/string/strchr/strchrcnt.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 96393354b..e76e7446a 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 index 000000000..793b2add2 --- /dev/null +++ b/lib/string/strchr/strchrcnt.c @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/strchrcnt.h" + +#include + + +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 index 000000000..1a1923eac --- /dev/null +++ b/lib/string/strchr/strchrcnt.h @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRCHRCNT_H_ + + +#include + +#include + +#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 -- 2.47.3