From c2a634efa78e5e9c92d68423afa761e6bc1b7130 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Sat, 16 Nov 2024 14:33:34 +0100 Subject: [PATCH] lib/string/strchr/: strrcspn(), stprcspn(): Add function and macro These APIs are to strrspn(), like strcspn() is to strspn(). They are like strcspn(3), but search from the end of the string. The function is meant for internal use, and consistency with libc. The macro is meant for normal use, since it returns a pointer, which is what algorithms using this need. See also strspn(3) and strcspn(3). Signed-off-by: Alejandro Colomar --- lib/Makefile.am | 4 ++++ lib/string/strchr/stprcspn.c | 7 +++++++ lib/string/strchr/stprcspn.h | 24 +++++++++++++++++++++++ lib/string/strchr/strrcspn.c | 12 ++++++++++++ lib/string/strchr/strrcspn.h | 38 ++++++++++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 lib/string/strchr/stprcspn.c create mode 100644 lib/string/strchr/stprcspn.h create mode 100644 lib/string/strchr/strrcspn.c create mode 100644 lib/string/strchr/strrcspn.h diff --git a/lib/Makefile.am b/lib/Makefile.am index fe5032692..31d09dced 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -200,8 +200,12 @@ libshadow_la_SOURCES = \ string/strchr/strchrscnt.h \ string/strchr/stpspn.c \ string/strchr/stpspn.h \ + string/strchr/stprcspn.c \ + string/strchr/stprcspn.h \ string/strchr/strnul.c \ string/strchr/strnul.h \ + string/strchr/strrcspn.c \ + string/strchr/strrcspn.h \ string/strchr/strrspn.c \ string/strchr/strrspn.h \ string/strcmp/strcaseeq.c \ diff --git a/lib/string/strchr/stprcspn.c b/lib/string/strchr/stprcspn.c new file mode 100644 index 000000000..f9dda5c4e --- /dev/null +++ b/lib/string/strchr/stprcspn.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/stprcspn.h" diff --git a/lib/string/strchr/stprcspn.h b/lib/string/strchr/stprcspn.h new file mode 100644 index 000000000..4d50566ac --- /dev/null +++ b/lib/string/strchr/stprcspn.h @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STPRCSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STPRCSPN_H_ + + +#include + +#include + +#include "string/strchr/strrcspn.h" + + +#define stprcspn(s, reject) \ +({ \ + __auto_type s_ = (s); \ + \ + s_ + strrcspn(s_, reject); \ +}) + + +#endif // include guard diff --git a/lib/string/strchr/strrcspn.c b/lib/string/strchr/strrcspn.c new file mode 100644 index 000000000..17316500d --- /dev/null +++ b/lib/string/strchr/strrcspn.c @@ -0,0 +1,12 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/strrcspn.h" + +#include + + +extern inline size_t strrcspn(const char *s, const char *reject); diff --git a/lib/string/strchr/strrcspn.h b/lib/string/strchr/strrcspn.h new file mode 100644 index 000000000..e42a60acd --- /dev/null +++ b/lib/string/strchr/strrcspn.h @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRCSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRCSPN_H_ + + +#include + +#include +#include + +#include "attr.h" +#include "string/strchr/strnul.h" + + +ATTR_STRING(1) +ATTR_STRING(2) +inline size_t strrcspn(const char *s, const char *reject); + + +inline size_t +strrcspn(const char *s, const char *reject) +{ + char *p; + + p = strnul(s); + while (p > s) { + p--; + if (strchr(reject, *p) != NULL) + return p + 1 - s; + } + return 0; +} + + +#endif // include guard -- 2.47.2