1 //===-- Implementation of memccpy ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/string/memccpy.h"
11 #include "src/__support/common.h"
12 #include "src/__support/macros/config.h"
13 #include <stddef.h> // For size_t.
15 namespace LIBC_NAMESPACE_DECL
{
17 LLVM_LIBC_FUNCTION(void *, memccpy
,
18 (void *__restrict dest
, const void *__restrict src
, int c
,
20 unsigned char end
= static_cast<unsigned char>(c
);
21 const unsigned char *uc_src
= static_cast<const unsigned char *>(src
);
22 unsigned char *uc_dest
= static_cast<unsigned char *>(dest
);
24 // Copy up until end is found.
25 for (; i
< count
&& uc_src
[i
] != end
; ++i
)
26 uc_dest
[i
] = uc_src
[i
];
27 // if i < count, then end must have been found, so copy end into dest and
28 // return the byte after.
30 uc_dest
[i
] = uc_src
[i
];
31 return uc_dest
+ i
+ 1;
36 } // namespace LIBC_NAMESPACE_DECL