From: Alejandro Colomar Date: Sat, 18 May 2024 18:41:30 +0000 (+0200) Subject: lib/string/strchr/: stp[c]spn(), strrspn(), strnul(): Add macros and functions X-Git-Tag: 4.17.0-rc1~149 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2fcf5201849d77e4782dc1b41393d89711cd8d90;p=thirdparty%2Fshadow.git lib/string/strchr/: stp[c]spn(), strrspn(), strnul(): Add macros and functions Often, a pointer is more useful than a length when calling these. Link: Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index fb509555d..8cbb6dfdb 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -162,6 +162,14 @@ libshadow_la_SOURCES = \ string/sprintf/stpeprintf.h \ string/sprintf/xasprintf.c \ string/sprintf/xasprintf.h \ + string/strchr/stpcspn.c \ + string/strchr/stpcspn.h \ + string/strchr/stpspn.c \ + string/strchr/stpspn.h \ + string/strchr/strnul.c \ + string/strchr/strnul.h \ + string/strchr/strrspn.c \ + string/strchr/strrspn.h \ string/strcpy/stpecpy.c \ string/strcpy/stpecpy.h \ string/strcpy/strncat.c \ diff --git a/lib/string/strchr/stpcspn.c b/lib/string/strchr/stpcspn.c new file mode 100644 index 000000000..7094f18bc --- /dev/null +++ b/lib/string/strchr/stpcspn.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/stpcspn.h" diff --git a/lib/string/strchr/stpcspn.h b/lib/string/strchr/stpcspn.h new file mode 100644 index 000000000..9fa78d3af --- /dev/null +++ b/lib/string/strchr/stpcspn.h @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STPCSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STPCSPN_H_ + + +#include + +#include + +#include "attr.h" + + +// Similar to strcspn(3), but return a pointer instead of an offset. +// Similar to strchrnul(3), but search for several delimiters. +// Similar to strpbrk(3), but return 's + strlen(s)' if not found. +#define stpcspn(s, reject) \ +({ \ + __auto_type s_ = s; \ + \ + s_ + strcspn(s_, reject); \ +}) + + +#endif // include guard diff --git a/lib/string/strchr/stpspn.c b/lib/string/strchr/stpspn.c new file mode 100644 index 000000000..b94ffe15c --- /dev/null +++ b/lib/string/strchr/stpspn.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/stpspn.h" diff --git a/lib/string/strchr/stpspn.h b/lib/string/strchr/stpspn.h new file mode 100644 index 000000000..97edfdf91 --- /dev/null +++ b/lib/string/strchr/stpspn.h @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STPSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STPSPN_H_ + + +#include + +#include + +#include "attr.h" + + +// Similar to strspn(3), but return a pointer instead of an offset. +// Similar to strchrnul(3), but search for any bytes not in 'accept'. +#define stpspn(s, accept) \ +({ \ + __auto_type s_ = s; \ + \ + s_ + strspn(s_, accept); \ +}) + + +#endif // include guard diff --git a/lib/string/strchr/strnul.c b/lib/string/strchr/strnul.c new file mode 100644 index 000000000..93b3ab7c6 --- /dev/null +++ b/lib/string/strchr/strnul.c @@ -0,0 +1,7 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/strnul.h" diff --git a/lib/string/strchr/strnul.h b/lib/string/strchr/strnul.h new file mode 100644 index 000000000..5754ad055 --- /dev/null +++ b/lib/string/strchr/strnul.h @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRNUL_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRNUL_H_ + + +#include + +#include + +#include "attr.h" + + +// Similar to strlen(3), but return a pointer instead of an offset. +#define strnul(s) \ +({ \ + __auto_type s_ = s; \ + \ + s_ + strlen(s_); \ +}) + + +#endif // include guard diff --git a/lib/string/strchr/strrspn.c b/lib/string/strchr/strrspn.c new file mode 100644 index 000000000..7ba254799 --- /dev/null +++ b/lib/string/strchr/strrspn.c @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strchr/strrspn.h" + + +extern inline char *strrspn(char *restrict s, const char *restrict accept); diff --git a/lib/string/strchr/strrspn.h b/lib/string/strchr/strrspn.h new file mode 100644 index 000000000..c05dc56dd --- /dev/null +++ b/lib/string/strchr/strrspn.h @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRSPN_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRSPN_H_ + + +#include + +#include + +#include "attr.h" +#include "string/strchr/strnul.h" + + +ATTR_STRING(2) +inline char *strrspn(char *restrict s, const char *restrict accept); + + +// Available in Oracle Solaris: strrspn(3GEN). +// +inline char * +strrspn(char *restrict s, const char *restrict accept) +{ + char *p; + + p = strnul(s); + while (p > s) { + p--; + if (strchr(accept, *p) == NULL) + return p + 1; + } + return s; +} + + +#endif // include guard