| 1 | //===-- Implementation of wcspbrk -----------------------------------------===// |
| 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/wcspbrk.h" |
| 10 | |
| 11 | #include "hdr/types/wchar_t.h" |
| 12 | #include "src/__support/common.h" |
| 13 | #include "src/__support/macros/null_check.h" |
| 14 | |
| 15 | namespace LIBC_NAMESPACE_DECL { |
| 16 | |
| 17 | bool contains_char(const wchar_t *str, wchar_t target) { |
| 18 | for (; *str != L'\0'; str++) |
| 19 | if (*str == target) |
| 20 | return true; |
| 21 | |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk, |
| 26 | (const wchar_t *src, const wchar_t *breakset)) { |
| 27 | LIBC_CRASH_ON_NULLPTR(src); |
| 28 | LIBC_CRASH_ON_NULLPTR(breakset); |
| 29 | |
| 30 | // currently O(n * m), can be further optimized to O(n + m) with a hash set |
| 31 | for (int src_idx = 0; src[src_idx] != 0; src_idx++) |
| 32 | if (contains_char(breakset, src[src_idx])) |
| 33 | return src + src_idx; |
| 34 | |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | } // namespace LIBC_NAMESPACE_DECL |
| 39 | |