1 | //===-- Implementation of memrchr -----------------------------------------===// |
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/memrchr.h" |
10 | #include "src/__support/common.h" |
11 | #include "src/__support/macros/config.h" |
12 | #include "src/__support/macros/null_check.h" |
13 | #include <stddef.h> |
14 | |
15 | namespace LIBC_NAMESPACE_DECL { |
16 | |
17 | LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) { |
18 | |
19 | if (n) |
20 | LIBC_CRASH_ON_NULLPTR(src); |
21 | |
22 | const unsigned char *str = reinterpret_cast<const unsigned char *>(src); |
23 | const unsigned char ch = static_cast<unsigned char>(c); |
24 | for (; n != 0; --n) { |
25 | const unsigned char *s = str + n - 1; |
26 | if (*s == ch) |
27 | return const_cast<unsigned char *>(s); |
28 | } |
29 | return nullptr; |
30 | } |
31 | |
32 | } // namespace LIBC_NAMESPACE_DECL |
33 | |