xref: /llvm-project/libc/src/string/memrchr.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
15a9630b7Scgyurgyik //===-- Implementation of memrchr -----------------------------------------===//
25a9630b7Scgyurgyik //
35a9630b7Scgyurgyik // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45a9630b7Scgyurgyik // See https://llvm.org/LICENSE.txt for license information.
55a9630b7Scgyurgyik // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a9630b7Scgyurgyik //
75a9630b7Scgyurgyik //===----------------------------------------------------------------------===//
85a9630b7Scgyurgyik 
95a9630b7Scgyurgyik #include "src/string/memrchr.h"
105a9630b7Scgyurgyik #include "src/__support/common.h"
11*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
125a9630b7Scgyurgyik #include <stddef.h>
135a9630b7Scgyurgyik 
14*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
155a9630b7Scgyurgyik 
16a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
175a9630b7Scgyurgyik   const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
186d1543a1SAlex Brachet   const unsigned char ch = static_cast<unsigned char>(c);
195a9630b7Scgyurgyik   for (; n != 0; --n) {
205a9630b7Scgyurgyik     const unsigned char *s = str + n - 1;
215a9630b7Scgyurgyik     if (*s == ch)
225a9630b7Scgyurgyik       return const_cast<unsigned char *>(s);
235a9630b7Scgyurgyik   }
245a9630b7Scgyurgyik   return nullptr;
255a9630b7Scgyurgyik }
265a9630b7Scgyurgyik 
27*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
28