1 //===-- Implementation of strstr ------------------------------------------===//
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 #include "src/string/strstr.h"
10 
11 #include "src/__support/common.h"
12 #include <stddef.h>
13 
14 namespace __llvm_libc {
15 
16 // TODO: This is a simple brute force implementation. This can be
17 // improved upon using well known string matching algorithms.
LLVM_LIBC_ENTRYPOINT(strstr)18 char *LLVM_LIBC_ENTRYPOINT(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;
27 }
28 
29 } // namespace __llvm_libc
30