diff --git a/lib/Makefile.am b/lib/Makefile.am index 96393354..e76e7446 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 00000000..793b2add --- /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 00000000..1a1923ea --- /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