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