]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strcmp/: strprefix(): Add API
authorAlejandro Colomar <alx@kernel.org>
Fri, 26 Jul 2024 09:13:37 +0000 (11:13 +0200)
committerSerge Hallyn <serge@hallyn.com>
Mon, 26 May 2025 16:29:26 +0000 (11:29 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strcmp/strprefix.c [new file with mode: 0644]
lib/string/strcmp/strprefix.h [new file with mode: 0644]

index 307e70469d223e1dcd12918a8f9dab588dbf9889..27efaa11903033d1f3ba1ec1a542b026211e647a 100644 (file)
@@ -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 (file)
index 0000000..31209cf
--- /dev/null
@@ -0,0 +1,10 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#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 (file)
index 0000000..245b5bf
--- /dev/null
@@ -0,0 +1,53 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCMP_STRPREFIX_H_
+
+
+#include <config.h>
+
+#include <stddef.h>
+#include <string.h>
+
+#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