summaryrefslogtreecommitdiff
path: root/zp/include/zp/str.d/utf8enclen.ii
blob: 9f93912acf3473242d6bcb89bc1ac38749008a2e (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
/*
	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>.
*/

constexpr auto ::zp::utf8enclen(::zp::c02 const * str) noexcept -> ::zp::sz {
	if (!::zp::isconsteval()) {
		return ::zp_utf8enclen(str);
	}

	::zp::sz len = 0x0u;

	for (;;++str) {
		auto const chr = *str;

		if (chr >= 0x10000u) {
			len += 0x4u;
			continue;
		}
		
		if (chr >= 0x800u) {
			len += 0x3u;
			continue;
		}
		
		if (chr >= 0x80u) {
			len += 0x2u;
			continue;
		}

		zp_ulikly (chr == 0x0u) {break;}

		++len;
	}

	return len;
}