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"
115a9630b7Scgyurgyik #include <stddef.h>
125a9630b7Scgyurgyik 
135a9630b7Scgyurgyik namespace __llvm_libc {
145a9630b7Scgyurgyik 
15a0b65a7bSMichael Jones LLVM_LIBC_FUNCTION(void *, memrchr, (const void *src, int c, size_t n)) {
165a9630b7Scgyurgyik   const unsigned char *str = reinterpret_cast<const unsigned char *>(src);
17*6d1543a1SAlex Brachet   const unsigned char ch = static_cast<unsigned char>(c);
185a9630b7Scgyurgyik   for (; n != 0; --n) {
195a9630b7Scgyurgyik     const unsigned char *s = str + n - 1;
205a9630b7Scgyurgyik     if (*s == ch)
215a9630b7Scgyurgyik       return const_cast<unsigned char *>(s);
225a9630b7Scgyurgyik   }
235a9630b7Scgyurgyik   return nullptr;
245a9630b7Scgyurgyik }
255a9630b7Scgyurgyik 
265a9630b7Scgyurgyik } // namespace __llvm_libc
27