summaryrefslogtreecommitdiff
path: root/zap/source/any/mem/utf8dec.c
blob: f9a0eac5c1dcdf873c50b88d3065c519f4643b6a (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
/*
	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>.
*/

#include <zap/mem.h>

void zap_utf8dec(zap_chr02 * const _dest,zap_chr8 const * const _src) {
	zap_chr02 *      dest;
	zap_chr8 const * src;
	for (dest = _dest,src = _src;;++dest) {
		zap_chr8 const oct = *src;
		if (oct == 0x0u) {
			break;
		}
		if (oct >= 0xF0u) { /* Four octets. */
			zap_chr02 chr =  ((zap_chr02)oct ^ 0xF0u) << 0x12u;
			++src;
			chr           += ((zap_chr02)*src ^ 0x80u) << 0xCu;
			++src;
			chr           += ((zap_chr02)*src ^ 0x80u) << 0x6u;
			++src;
			chr           +=  (zap_chr02)*src ^ 0x80u;
			++src;
			*dest = chr;
			continue;
		}
		if (oct >= 0xE0u) { /* Three octets. */
			zap_chr02 chr =  ((zap_chr02)oct ^ 0xE0u) << 0xCu;
			++src;
			chr           += ((zap_chr02)*src ^ 0x80u) << 0x6u;
			++src;
			chr           +=  (zap_chr02)*src ^ 0x80u;
			++src;
			*dest = chr;
			continue;
		}
		if (oct >= 0xC0u) { /* Two octets. */
			zap_chr02 chr =  ((zap_chr02)oct ^ 0xC0u) << 0x6u;
			++src;
			          chr +=  (zap_chr02)*src ^ 0x80u;
			++src;
			*dest = chr;
			continue;
		}
		/* One octet. */
		*dest = oct;
		++src;
	}
}