summaryrefslogtreecommitdiffstats
path: root/include/c/str/cpy/stp/stpe/stpecpy.h
blob: d75b2b0891bba9191ff0ca3c868a585f3640f35d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2022 Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier:  LGPL-3.0-or-later WITH LGPL-3.0-linking-exception


#ifndef INCLUDE_C_STR_CPY_STP_STPE_STPECPY_H_
#define INCLUDE_C_STR_CPY_STP_STPE_STPECPY_H_


#include <stdbool.h>
#include <stddef.h>

#include <c/branch/likely.h>
#include <c/branch/unreachable.h>
#include <c/qual/nullable/nullable.h>
#include <c/str/cpy/stp/stp/ustr2stp.h>
#include <c/str/len/strlen.h>


#pragma clang assume_nonnull begin
inline char *c_nullable c_stpecpy(char *c_nullable dst, char *end,
    const char *restrict src);


inline char *c_nullable
c_stpecpy(char *c_nullable dst, char *end, const char *restrict src)
{
	bool    trunc;
	size_t  dsize, dlen, slen;

	if (dst == end)
		return end;
	if (c_unlikely(dst == NULL))  // Allow chaining with stpeprintf().
		return NULL;
	c_impossible(dst > end);

	dsize = end - dst;
	slen = c_strnlen(src, dsize);
	trunc = (slen == dsize);
	dlen = slen - trunc;
	return c_ustr2stp(dst, src, dlen) + trunc;
}
#pragma clang assume_nonnull end


#endif  // Header guard