[libc-commits] [libc] eebb2e3 - [libc] Templatize strstr

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Wed Jan 25 08:43:06 PST 2023


Author: Alex Brachet
Date: 2023-01-25T16:42:34Z
New Revision: eebb2e31b43811cd4868cc5273cf4a5d5f110ae0

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

LOG: [libc] Templatize strstr

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

Added: 
    libc/src/string/memory_utils/strstr_implementations.h

Modified: 
    libc/src/string/CMakeLists.txt
    libc/src/string/memory_utils/CMakeLists.txt
    libc/src/string/strstr.cpp
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/src/string/CMakeLists.txt b/libc/src/string/CMakeLists.txt
index 749cfab8d01f1..9c580dcaae1aa 100644
--- a/libc/src/string/CMakeLists.txt
+++ b/libc/src/string/CMakeLists.txt
@@ -330,6 +330,8 @@ add_entrypoint_object(
     strstr.cpp
   HDRS
     strstr.h
+  DEPENDS
+    .memory_utils.strstr_implementation
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/string/memory_utils/CMakeLists.txt b/libc/src/string/memory_utils/CMakeLists.txt
index 10e14e1a40fc3..9a4df7c950837 100644
--- a/libc/src/string/memory_utils/CMakeLists.txt
+++ b/libc/src/string/memory_utils/CMakeLists.txt
@@ -66,3 +66,9 @@ add_header_library(
   HDRS
     strcmp_implementations.h
 )
+
+add_header_library(
+  strstr_implementation
+  HDRS
+    strstr_implementations.h
+)

diff  --git a/libc/src/string/memory_utils/strstr_implementations.h b/libc/src/string/memory_utils/strstr_implementations.h
new file mode 100644
index 0000000000000..2c81ca771563e
--- /dev/null
+++ b/libc/src/string/memory_utils/strstr_implementations.h
@@ -0,0 +1,33 @@
+//===-- str{,case}str implementation ----------------------------*- 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_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H
+#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H
+
+#include <stddef.h>
+
+namespace __llvm_libc {
+
+template <typename Comp>
+constexpr static char *strstr_implementation(const char *haystack,
+                                             const char *needle, Comp &&comp) {
+  // TODO: This is a simple brute force implementation. This can be
+  // improved upon using well known string matching algorithms.
+  for (size_t i = 0; comp(haystack[i], 0); ++i) {
+    size_t j = 0;
+    for (; comp(haystack[i + j], 0) && !comp(haystack[i + j], needle[j]); ++j)
+      ;
+    if (!comp(needle[j], 0))
+      return const_cast<char *>(haystack + i);
+  }
+  return nullptr;
+}
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H

diff  --git a/libc/src/string/strstr.cpp b/libc/src/string/strstr.cpp
index 3ec32fddd942f..643461da59935 100644
--- a/libc/src/string/strstr.cpp
+++ b/libc/src/string/strstr.cpp
@@ -9,21 +9,15 @@
 #include "src/string/strstr.h"
 
 #include "src/__support/common.h"
-#include <stddef.h>
+#include "src/string/memory_utils/strstr_implementations.h"
 
 namespace __llvm_libc {
 
 // TODO: This is a simple brute force implementation. This can be
 // improved upon using well known string matching algorithms.
 LLVM_LIBC_FUNCTION(char *, strstr, (const char *haystack, const char *needle)) {
-  for (size_t i = 0; haystack[i]; ++i) {
-    size_t j;
-    for (j = 0; haystack[i + j] && haystack[i + j] == needle[j]; ++j)
-      ;
-    if (!needle[j])
-      return const_cast<char *>(haystack + i);
-  }
-  return nullptr;
+  auto comp = [](char l, char r) -> int { return l - r; };
+  return strstr_implementation(haystack, needle, comp);
 }
 
 } // namespace __llvm_libc

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index ecfdb4f48096a..e8d7f8b608d3a 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1103,6 +1103,7 @@ libc_support_library(
         "src/string/memory_utils/memmove_implementations.h",
         "src/string/memory_utils/memset_implementations.h",
         "src/string/memory_utils/strcmp_implementations.h",
+        "src/string/memory_utils/strstr_implementations.h",
     ],
     deps = [
         ":__support_common",
@@ -1315,6 +1316,7 @@ libc_function(
     hdrs = ["src/string/strstr.h"],
     deps = [
         ":__support_common",
+        ":string_memory_utils",
         ":string_utils",
     ],
 )


        


More information about the libc-commits mailing list