| 1 | //===-- Implementation of wcsstr ------------------------------------------===// |
| 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/wchar/wcsstr.h" |
| 10 | |
| 11 | #include "hdr/types/size_t.h" |
| 12 | #include "hdr/types/wchar_t.h" |
| 13 | #include "src/__support/common.h" |
| 14 | #include "src/__support/macros/config.h" |
| 15 | #include "src/string/string_utils.h" |
| 16 | |
| 17 | namespace LIBC_NAMESPACE_DECL { |
| 18 | |
| 19 | LLVM_LIBC_FUNCTION(const wchar_t *, wcsstr, |
| 20 | (const wchar_t *s1, const wchar_t *s2)) { |
| 21 | size_t s1_len = internal::string_length(s1); |
| 22 | size_t s2_len = internal::string_length(s2); |
| 23 | if (s2_len == 0) |
| 24 | return s1; |
| 25 | if (s2_len > s1_len) |
| 26 | return nullptr; |
| 27 | for (size_t i = 0; i <= (s1_len - s2_len); ++i) { |
| 28 | size_t j = 0; |
| 29 | // j will increment until the characters don't match or end of string. |
| 30 | for (; j < s2_len && s1[i + j] == s2[j]; ++j) |
| 31 | ; |
| 32 | if (j == s2_len) |
| 33 | return (s1 + i); |
| 34 | } |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | } // namespace LIBC_NAMESPACE_DECL |
| 39 | |