Skip to content

Commit eebb2e3

Browse files
committed
[libc] Templatize strstr
Differential Revision: https://reviews.llvm.org/D142517
1 parent b6b3d20 commit eebb2e3

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed

libc/src/string/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ add_entrypoint_object(
330330
strstr.cpp
331331
HDRS
332332
strstr.h
333+
DEPENDS
334+
.memory_utils.strstr_implementation
333335
)
334336

335337
add_entrypoint_object(

libc/src/string/memory_utils/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ add_header_library(
6666
HDRS
6767
strcmp_implementations.h
6868
)
69+
70+
add_header_library(
71+
strstr_implementation
72+
HDRS
73+
strstr_implementations.h
74+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===-- str{,case}str implementation ----------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STRING_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H
10+
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H
11+
12+
#include <stddef.h>
13+
14+
namespace __llvm_libc {
15+
16+
template <typename Comp>
17+
constexpr static char *strstr_implementation(const char *haystack,
18+
const char *needle, Comp &&comp) {
19+
// TODO: This is a simple brute force implementation. This can be
20+
// improved upon using well known string matching algorithms.
21+
for (size_t i = 0; comp(haystack[i], 0); ++i) {
22+
size_t j = 0;
23+
for (; comp(haystack[i + j], 0) && !comp(haystack[i + j], needle[j]); ++j)
24+
;
25+
if (!comp(needle[j], 0))
26+
return const_cast<char *>(haystack + i);
27+
}
28+
return nullptr;
29+
}
30+
31+
} // namespace __llvm_libc
32+
33+
#endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_STRSTR_IMPLEMENTATIONS_H

libc/src/string/strstr.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,15 @@
99
#include "src/string/strstr.h"
1010

1111
#include "src/__support/common.h"
12-
#include <stddef.h>
12+
#include "src/string/memory_utils/strstr_implementations.h"
1313

1414
namespace __llvm_libc {
1515

1616
// TODO: This is a simple brute force implementation. This can be
1717
// improved upon using well known string matching algorithms.
1818
LLVM_LIBC_FUNCTION(char *, strstr, (const char *haystack, const char *needle)) {
19-
for (size_t i = 0; haystack[i]; ++i) {
20-
size_t j;
21-
for (j = 0; haystack[i + j] && haystack[i + j] == needle[j]; ++j)
22-
;
23-
if (!needle[j])
24-
return const_cast<char *>(haystack + i);
25-
}
26-
return nullptr;
19+
auto comp = [](char l, char r) -> int { return l - r; };
20+
return strstr_implementation(haystack, needle, comp);
2721
}
2822

2923
} // namespace __llvm_libc

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ libc_support_library(
11031103
"src/string/memory_utils/memmove_implementations.h",
11041104
"src/string/memory_utils/memset_implementations.h",
11051105
"src/string/memory_utils/strcmp_implementations.h",
1106+
"src/string/memory_utils/strstr_implementations.h",
11061107
],
11071108
deps = [
11081109
":__support_common",
@@ -1315,6 +1316,7 @@ libc_function(
13151316
hdrs = ["src/string/strstr.h"],
13161317
deps = [
13171318
":__support_common",
1319+
":string_memory_utils",
13181320
":string_utils",
13191321
],
13201322
)

0 commit comments

Comments
 (0)