]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/strchr/: strrcspn(), stprcspn(): Add function and macro
authorAlejandro Colomar <alx@kernel.org>
Sat, 16 Nov 2024 13:33:34 +0000 (14:33 +0100)
committerSerge Hallyn <serge@hallyn.com>
Sun, 16 Feb 2025 19:22:51 +0000 (13:22 -0600)
These APIs are to strrspn(), like strcspn() is to strspn().
They are like strcspn(3), but search from the end of the string.

The function is meant for internal use, and consistency with libc.
The macro is meant for normal use, since it returns a pointer,
which is what algorithms using this need.

See also strspn(3) and strcspn(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/strchr/stprcspn.c [new file with mode: 0644]
lib/string/strchr/stprcspn.h [new file with mode: 0644]
lib/string/strchr/strrcspn.c [new file with mode: 0644]
lib/string/strchr/strrcspn.h [new file with mode: 0644]

index fe5032692768ab519daf6471fa2acf6655263ef2..31d09dced2aaf1830640546892f54dd839f2fa29 100644 (file)
@@ -200,8 +200,12 @@ libshadow_la_SOURCES = \
        string/strchr/strchrscnt.h \
        string/strchr/stpspn.c \
        string/strchr/stpspn.h \
+       string/strchr/stprcspn.c \
+       string/strchr/stprcspn.h \
        string/strchr/strnul.c \
        string/strchr/strnul.h \
+       string/strchr/strrcspn.c \
+       string/strchr/strrcspn.h \
        string/strchr/strrspn.c \
        string/strchr/strrspn.h \
        string/strcmp/strcaseeq.c \
diff --git a/lib/string/strchr/stprcspn.c b/lib/string/strchr/stprcspn.c
new file mode 100644 (file)
index 0000000..f9dda5c
--- /dev/null
@@ -0,0 +1,7 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strchr/stprcspn.h"
diff --git a/lib/string/strchr/stprcspn.h b/lib/string/strchr/stprcspn.h
new file mode 100644 (file)
index 0000000..4d50566
--- /dev/null
@@ -0,0 +1,24 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STPRCSPN_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STPRCSPN_H_
+
+
+#include <config.h>
+
+#include <string.h>
+
+#include "string/strchr/strrcspn.h"
+
+
+#define stprcspn(s, reject)                                                   \
+({                                                                            \
+       __auto_type  s_ = (s);                                                \
+                                                                              \
+       s_ + strrcspn(s_, reject);                                            \
+})
+
+
+#endif  // include guard
diff --git a/lib/string/strchr/strrcspn.c b/lib/string/strchr/strrcspn.c
new file mode 100644 (file)
index 0000000..1731650
--- /dev/null
@@ -0,0 +1,12 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/strchr/strrcspn.h"
+
+#include <stddef.h>
+
+
+extern inline size_t strrcspn(const char *s, const char *reject);
diff --git a/lib/string/strchr/strrcspn.h b/lib/string/strchr/strrcspn.h
new file mode 100644 (file)
index 0000000..e42a60a
--- /dev/null
@@ -0,0 +1,38 @@
+// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRCSPN_H_
+#define SHADOW_INCLUDE_LIB_STRING_STRCHR_STRRCSPN_H_
+
+
+#include <config.h>
+
+#include <stddef.h>
+#include <string.h>
+
+#include "attr.h"
+#include "string/strchr/strnul.h"
+
+
+ATTR_STRING(1)
+ATTR_STRING(2)
+inline size_t strrcspn(const char *s, const char *reject);
+
+
+inline size_t
+strrcspn(const char *s, const char *reject)
+{
+       char  *p;
+
+       p = strnul(s);
+       while (p > s) {
+               p--;
+               if (strchr(reject, *p) != NULL)
+                       return p + 1 - s;
+       }
+       return 0;
+}
+
+
+#endif  // include guard