summaryrefslogtreecommitdiff
path: root/zap/include/zap/mem.hh
blob: b237cbcbe30934c9af9fb052d02dc735435f0bd3 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
	Copyright 2022-2023 Gabriel Jensen.
	This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
	If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
*/

#ifndef zap_priv_cxxhdr_mem
#define zap_priv_cxxhdr_mem

#include <zap/bs.hh>
#include <zap/mem.h>

namespace zap {
	template<typename desttyp,typename srctyp> struct cpret {
		desttyp * dest;
		srctyp *  src;
	};
}

namespace zap {
	template<typename desttyp,typename srctyp> zap_attr_iln inline ::zap::cpret<desttyp,srctyp> bytecp(desttyp * zap_restr dest,srctyp const * zap_restr src,::zap::sz const num) noexcept {
		::zap_cpret const cpret = ::zap_cp(dest,src,num);

		return ::zap::cpret<desttyp,srctyp> {.dest = static_cast<desttyp *>(cpret.dest),.src = static_cast<srctyp *>(cpret.src),};
	}

	template<typename ltyp,typename rtyp> zap_attr_iln inline bool byteeq(ltyp const * lbuf,rtyp const * rbuf,::zap::sz const num) noexcept {
		return ::zap_eq(lbuf,rbuf,num);
	}

	template<typename typ> zap_attr_iln inline void bytefill(typ * dest,unsigned char const val,::zap::sz const num) noexcept {
		::zap_fill(dest,val,num);
	}

	template<typename typ> zap_attr_iln inline typ * bytesrch(typ * buf,unsigned char const val,::zap::sz const num) noexcept {
		return const_cast<typ *>(static_cast<::zap::remqual<typ> *>(::zap_srch(buf,val,num)));
	}
}

namespace zap {
	template<typename typ> constexpr ::zap::cpret<typ,typ> cp(typ * zap_restr dest,typ const * zap_restr src,::zap::sz const num) noexcept {
		zap_priv_ifnconsteval {return ::zap::bytecp(dest,src,num);}

		typ * const zap_restr stop = dest + num;
		while (dest != stop) *dest++ = *src++;

		return ::zap::cpret<typ,typ> {.dest = dest,.src = const_cast<typ *>(src),};
	}

	template<typename typ> constexpr bool eq(typ const * lbuf,typ const * rbuf,::zap::sz const num) noexcept {
		typ const * const stop = lbuf + num;

		while (lbuf != stop) if (*lbuf++ != *rbuf++) return false;

		return true;
	}

	template<typename typ> constexpr void fill(typ * dest,typ const val,::zap::sz const num) noexcept {
		typ * const stop = dest + num;

		while (dest != stop) *dest++ = val;
	}

	template<typename typ> constexpr typ * srch(typ * buf,unsigned char const val,::zap::sz const num) noexcept {
		typ const * const stop = buf + num;
		
		while (buf != stop) {
			typ const * addr = buf++;
			if (*addr == val) return addr;
		}

		return nullptr;
	}
}

#endif