[libc-commits] [libc] b4ab398 - [libc] Implement strsep

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Thu Apr 6 10:49:09 PDT 2023


Author: Alex Brachet
Date: 2023-04-06T17:48:28Z
New Revision: b4ab398cb11b6adb39bcd13db315b59c763d1b08

URL: https://github.com/llvm/llvm-project/commit/b4ab398cb11b6adb39bcd13db315b59c763d1b08
DIFF: https://github.com/llvm/llvm-project/commit/b4ab398cb11b6adb39bcd13db315b59c763d1b08.diff

LOG: [libc] Implement strsep

Differential Revision: https://reviews.llvm.org/D147503

Added: 
    libc/src/string/strsep.cpp
    libc/src/string/strsep.h
    libc/test/src/string/strsep_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/riscv64/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/spec/bsd_ext.td
    libc/src/string/CMakeLists.txt
    libc/src/string/string_utils.h
    libc/test/src/string/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 92b6f270d8855..7de897fa3cc5c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -65,6 +65,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strnlen
     libc.src.string.strpbrk
     libc.src.string.strrchr
+    libc.src.string.strsep
     libc.src.string.strsignal
     libc.src.string.strspn
     libc.src.string.strstr

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 5e171336485b0..4ae8b0f44a8be 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -52,6 +52,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strnlen
     libc.src.string.strpbrk
     libc.src.string.strrchr
+    libc.src.string.strsep
     libc.src.string.strspn
     libc.src.string.strstr
     libc.src.string.strtok

diff  --git a/libc/config/linux/riscv64/entrypoints.txt b/libc/config/linux/riscv64/entrypoints.txt
index 3f4fbed66202a..9daf4ed8ed2e5 100644
--- a/libc/config/linux/riscv64/entrypoints.txt
+++ b/libc/config/linux/riscv64/entrypoints.txt
@@ -66,6 +66,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strnlen
     libc.src.string.strpbrk
     libc.src.string.strrchr
+    libc.src.string.strsep
     libc.src.string.strsignal
     libc.src.string.strspn
     libc.src.string.strstr

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 238ec847992a9..b74bc665ca1fc 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -66,6 +66,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.string.strnlen
     libc.src.string.strpbrk
     libc.src.string.strrchr
+    libc.src.string.strsep
     libc.src.string.strsignal
     libc.src.string.strspn
     libc.src.string.strstr

diff  --git a/libc/spec/bsd_ext.td b/libc/spec/bsd_ext.td
index 9ee07ce4e182f..3420c2098449f 100644
--- a/libc/spec/bsd_ext.td
+++ b/libc/spec/bsd_ext.td
@@ -15,6 +15,11 @@ def BsdExtensions : StandardSpec<"BSDExtensions"> {
             RetValSpec<SizeTType>,
             [ArgSpec<CharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<SizeTType>]
         >,
+        FunctionSpec<
+            "strsep",
+            RetValSpec<CharPtr>,
+            [ArgSpec<CharRestrictedPtrPtr>, ArgSpec<ConstCharRestrictedPtr>]
+        >,
       ]
   >;
 

diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 034df80c629ae..07c3b802a3c88 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -325,6 +325,16 @@ add_entrypoint_object(
     strrchr.h
 )
 
+add_entrypoint_object(
+  strsep
+  SRCS
+    strsep.cpp
+  HDRS
+    strsep.h
+  DEPENDS
+    .string_utils
+)
+
 add_entrypoint_object(
   strsignal
   SRCS

diff  --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h
index d42f11384b1f2..c778ccf170135 100644
--- a/libc/src/string/string_utils.h
+++ b/libc/src/string/string_utils.h
@@ -182,6 +182,7 @@ LIBC_INLINE size_t complementary_span(const char *src, const char *segment) {
 // is found is then stored within 'context' for subsequent calls. Subsequent
 // calls will use 'context' when a nullptr is passed in for 'src'. Once the null
 // terminating character is reached, returns a nullptr.
+template <bool SkipDelim = true>
 LIBC_INLINE char *string_token(char *__restrict src,
                                const char *__restrict delimiter_string,
                                char **__restrict saveptr) {
@@ -193,8 +194,9 @@ LIBC_INLINE char *string_token(char *__restrict src,
   for (; *delimiter_string != '\0'; ++delimiter_string)
     delimiter_set.set(*delimiter_string);
 
-  for (; *src != '\0' && delimiter_set.test(*src); ++src)
-    ;
+  if constexpr (SkipDelim)
+    for (; *src != '\0' && delimiter_set.test(*src); ++src)
+      ;
   if (*src == '\0') {
     *saveptr = src;
     return nullptr;

diff  --git a/libc/src/string/strsep.cpp b/libc/src/string/strsep.cpp
new file mode 100644
index 0000000000000..5e6aef7ac008e
--- /dev/null
+++ b/libc/src/string/strsep.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of strsep ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strsep.h"
+
+#include "src/string/string_utils.h"
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(char *, strsep, (char **stringp, const char *delim)) {
+  if (!*stringp)
+    return nullptr;
+  return internal::string_token<false>(*stringp, delim, stringp);
+}
+
+} // namespace __llvm_libc

diff  --git a/libc/src/string/strsep.h b/libc/src/string/strsep.h
new file mode 100644
index 0000000000000..1b1f28ff9c87c
--- /dev/null
+++ b/libc/src/string/strsep.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for strsep ------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STRING_STRSEP_H
+#define LLVM_LIBC_SRC_STRING_STRSEP_H
+
+namespace __llvm_libc {
+
+char *strsep(char **stringp, const char *delim);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_STRSEP_H

diff  --git a/libc/test/src/string/CMakeLists.txt b/libc/test/src/string/CMakeLists.txt
index e5c4880c3601d..5d9be5e58f07f 100644
--- a/libc/test/src/string/CMakeLists.txt
+++ b/libc/test/src/string/CMakeLists.txt
@@ -308,6 +308,16 @@ add_libc_unittest(
     libc.src.string.strrchr
 )
 
+add_libc_unittest(
+  strsep_test
+  SUITE
+    libc_string_unittests
+  SRCS
+    strsep_test.cpp
+  DEPENDS
+    libc.src.string.strsep
+)
+
 add_libc_unittest(
   strsignal_test
   SUITE

diff  --git a/libc/test/src/string/strsep_test.cpp b/libc/test/src/string/strsep_test.cpp
new file mode 100644
index 0000000000000..7c0863b3266f5
--- /dev/null
+++ b/libc/test/src/string/strsep_test.cpp
@@ -0,0 +1,53 @@
+//===-- Unittests for strsep ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/string/strsep.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcStrsepTest, NullSrc) {
+  char *string = nullptr;
+  EXPECT_STREQ(__llvm_libc::strsep(&string, ""), nullptr);
+}
+
+TEST(LlvmLibcStrsepTest, NoTokenFound) {
+  {
+    char s[] = "";
+    char *string = s, *orig = s;
+    EXPECT_STREQ(__llvm_libc::strsep(&string, ""), nullptr);
+    EXPECT_EQ(orig, string);
+  }
+  {
+    char s[] = "abcde";
+    char *string = s, *orig = s;
+    EXPECT_STREQ(__llvm_libc::strsep(&string, ""), orig);
+    EXPECT_EQ(string, orig + 5);
+  }
+  {
+    char s[] = "abcde";
+    char *string = s, *orig = s;
+    EXPECT_STREQ(__llvm_libc::strsep(&string, "fghijk"), orig);
+    EXPECT_EQ(string, orig + 5);
+  }
+}
+
+TEST(LlvmLibcStrsepTest, TokenFound) {
+  char s[] = "abacd";
+  char *string = s;
+  EXPECT_STREQ(__llvm_libc::strsep(&string, "c"), "aba");
+  EXPECT_STREQ(string, "d");
+}
+
+TEST(LlvmLibcStrsepTest, DelimitersShouldNotBeIncludedInToken) {
+  char s[] = "__ab__:cd_:_ef_:_";
+  char *string = s;
+  const char *expected[] = {"", "",   "ab", "", "", "cd",   "",
+                            "", "ef", "",   "", "", nullptr};
+  for (int i = 0; expected[i]; i++) {
+    ASSERT_STREQ(__llvm_libc::strsep(&string, "_:"), expected[i]);
+  }
+}


        


More information about the libc-commits mailing list