Path: blob/main/system/lib/llvm-libc/src/string/memccpy.cpp
6175 views
//===-- Implementation of memccpy ----------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "src/string/memccpy.h"910#include "src/__support/common.h"11#include "src/__support/macros/config.h"12#include "src/__support/macros/null_check.h"13#include <stddef.h> // For size_t.1415namespace LIBC_NAMESPACE_DECL {1617LLVM_LIBC_FUNCTION(void *, memccpy,18(void *__restrict dest, const void *__restrict src, int c,19size_t count)) {20if (count) {21LIBC_CRASH_ON_NULLPTR(dest);22LIBC_CRASH_ON_NULLPTR(src);23}24unsigned char end = static_cast<unsigned char>(c);25const unsigned char *uc_src = static_cast<const unsigned char *>(src);26unsigned char *uc_dest = static_cast<unsigned char *>(dest);27size_t i = 0;28// Copy up until end is found.29for (; i < count && uc_src[i] != end; ++i)30uc_dest[i] = uc_src[i];31// if i < count, then end must have been found, so copy end into dest and32// return the byte after.33if (i < count) {34uc_dest[i] = uc_src[i];35return uc_dest + i + 1;36}37return nullptr;38}3940} // namespace LIBC_NAMESPACE_DECL414243