]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strcmp/: strcaseprefix(): Add API
authorAlejandro Colomar <alx@kernel.org>
Sat, 8 Feb 2025 09:57:51 +0000 (10:57 +0100)
committerSerge Hallyn <serge@hallyn.com>
Tue, 3 Jun 2025 01:08:57 +0000 (20:08 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strcmp/strcaseprefix.c [new file with mode: 0644]
lib/string/strcmp/strcaseprefix.h [new file with mode: 0644]

index 04e6c251c682554bbe02bf3d777a3b0025f29c39..e9ffd7e1ff7f5a4cff773ad7393660192695d6e9 100644 (file)
@@ -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 (file)
index 0000000..7fffa4e
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#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 (file)
index 0000000..37ca4d8
--- /dev/null
@@ -0,0 +1,49 @@
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRCASEPREFIX_H_
+
+
+#include <config.h>
+
+#include <stddef.h>
+#include <string.h>
+
+#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