[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / string / memccpy.cpp
blob989a24d0852c6c8c1ebea5ab14f721d6e238436b
1 //===-- Implementation of memccpy ----------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "src/string/memccpy.h"
11 #include "src/__support/common.h"
12 #include <stddef.h> // For size_t.
14 namespace __llvm_libc {
16 LLVM_LIBC_FUNCTION(void *, memccpy,
17 (void *__restrict dest, const void *__restrict src, int c,
18 size_t count)) {
19 unsigned char end = static_cast<unsigned char>(c);
20 const unsigned char *uc_src = static_cast<const unsigned char *>(src);
21 unsigned char *uc_dest = static_cast<unsigned char *>(dest);
22 size_t i = 0;
23 // Copy up until end is found.
24 for (; i < count && uc_src[i] != end; ++i)
25 uc_dest[i] = uc_src[i];
26 // if i < count, then end must have been found, so copy end into dest and
27 // return the byte after.
28 if (i < count) {
29 uc_dest[i] = uc_src[i];
30 return uc_dest + i + 1;
32 return nullptr;
35 } // namespace __llvm_libc