From: Alejandro Colomar Date: Fri, 26 Jul 2024 09:13:37 +0000 (+0200) Subject: lib/string/strcmp/: strprefix(): Add API X-Git-Tag: 4.18.0-rc1~80 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34e9e5a8f571362893d5e10080e529513845e32c;p=thirdparty%2Fshadow.git lib/string/strcmp/: strprefix(): Add API Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 307e70469..27efaa119 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -206,6 +206,8 @@ libshadow_la_SOURCES = \ string/strcmp/strcaseeq.h \ string/strcmp/streq.c \ string/strcmp/streq.h \ + string/strcmp/strprefix.c \ + string/strcmp/strprefix.h \ string/strcpy/stpecpy.c \ string/strcpy/stpecpy.h \ string/strcpy/strncat.c \ diff --git a/lib/string/strcmp/strprefix.c b/lib/string/strcmp/strprefix.c new file mode 100644 index 000000000..31209cfa4 --- /dev/null +++ b/lib/string/strcmp/strprefix.c @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strcmp/strprefix.h" + + +extern inline const char *strprefix_(const char *s, const char *prefix); diff --git a/lib/string/strcmp/strprefix.h b/lib/string/strcmp/strprefix.h new file mode 100644 index 000000000..245b5bf60 --- /dev/null +++ b/lib/string/strcmp/strprefix.h @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_ + + +#include + +#include +#include + +#include "attr.h" +#include "cast.h" + + +// string prefix +#define strprefix(s, prefix) \ +({ \ + const char *p_; \ + \ + p_ = strprefix_(s, prefix); \ + \ + _Generic(s, \ + const char *: p_, \ + const void *: p_, \ + char *: const_cast(char *, p_), \ + void *: const_cast(char *, p_) \ + ); \ +}) + + +ATTR_STRING(1) +ATTR_STRING(2) +inline const char *strprefix_(const char *s, const char *prefix); + + +/* + * Return NULL if s does not start with prefix. + * Return `s + strlen(prefix)` if s starts with prefix. + */ +inline const char * +strprefix_(const char *s, const char *prefix) +{ + if (strncmp(s, prefix, strlen(prefix)) != 0) + return NULL; + + return s + strlen(prefix); +} + + +#endif // include guard