1 | //===-- str{,case}cmp 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_INLINE_STRCMP_H |
10 | #define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H |
11 | |
12 | #include <stddef.h> |
13 | |
14 | namespace LIBC_NAMESPACE { |
15 | |
16 | template <typename Comp> |
17 | LIBC_INLINE constexpr int inline_strcmp(const char *left, const char *right, |
18 | Comp &&comp) { |
19 | // TODO: Look at benefits for comparing words at a time. |
20 | for (; *left && !comp(*left, *right); ++left, ++right) |
21 | ; |
22 | return comp(*reinterpret_cast<const unsigned char *>(left), |
23 | *reinterpret_cast<const unsigned char *>(right)); |
24 | } |
25 | |
26 | template <typename Comp> |
27 | LIBC_INLINE constexpr int inline_strncmp(const char *left, const char *right, |
28 | size_t n, Comp &&comp) { |
29 | if (n == 0) |
30 | return 0; |
31 | |
32 | // TODO: Look at benefits for comparing words at a time. |
33 | for (; n > 1; --n, ++left, ++right) { |
34 | char lc = *left; |
35 | if (!comp(lc, '\0') || comp(lc, *right)) |
36 | break; |
37 | } |
38 | return comp(*reinterpret_cast<const unsigned char *>(left), |
39 | *reinterpret_cast<const unsigned char *>(right)); |
40 | } |
41 | |
42 | } // namespace LIBC_NAMESPACE |
43 | |
44 | #endif // LLVM_LIBC_SRC_STRING_MEMORY_UTILS_INLINE_STRCMP_H |
45 | |