From: Alejandro Colomar Date: Thu, 4 Jul 2024 13:30:58 +0000 (+0200) Subject: lib/string/strchr/: strchrcnt(): Add function X-Git-Tag: 4.17.0-rc1~33 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9efce1ac859c4be28761223109b6c7f81a9960f0;p=thirdparty%2Fshadow.git lib/string/strchr/: strchrcnt(): Add function Signed-off-by: Alejandro Colomar --- 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