From: Alejandro Colomar Date: Sat, 8 Feb 2025 09:57:51 +0000 (+0100) Subject: lib/string/strcmp/: strcaseprefix(): Add API X-Git-Tag: 4.18.0-rc1~44 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=93493077a1497f4e1a3f76b1eac3fc119a367437;p=thirdparty%2Fshadow.git lib/string/strcmp/: strcaseprefix(): Add API Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 04e6c251c..e9ffd7e1f 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -199,6 +199,8 @@ libshadow_la_SOURCES = \ string/strchr/strnul.h \ string/strcmp/strcaseeq.c \ string/strcmp/strcaseeq.h \ + string/strcmp/strcaseprefix.c \ + string/strcmp/strcaseprefix.h \ string/strcmp/streq.c \ string/strcmp/streq.h \ string/strcmp/strprefix.c \ diff --git a/lib/string/strcmp/strcaseprefix.c b/lib/string/strcmp/strcaseprefix.c new file mode 100644 index 000000000..7fffa4ee8 --- /dev/null +++ b/lib/string/strcmp/strcaseprefix.c @@ -0,0 +1,10 @@ +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/strcmp/strcaseprefix.h" + + +extern inline const char *strcaseprefix_(const char *s, const char *prefix); diff --git a/lib/string/strcmp/strcaseprefix.h b/lib/string/strcmp/strcaseprefix.h new file mode 100644 index 000000000..37ca4d89d --- /dev/null +++ b/lib/string/strcmp/strcaseprefix.h @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_ +#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_ + + +#include + +#include +#include + +#include "attr.h" +#include "cast.h" + + +// string case-insensitive prefix +#define strcaseprefix(s, prefix) \ +({ \ + const char *p_; \ + \ + p_ = strcaseprefix_(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 *strcaseprefix_(const char *s, const char *prefix); + + +// strprefix_(), but case-insensitive. +inline const char * +strcaseprefix_(const char *s, const char *prefix) +{ + if (strncasecmp(s, prefix, strlen(prefix)) != 0) + return NULL; + + return s + strlen(prefix); +} + + +#endif // include guard