summaryrefslogtreecommitdiff
path: root/zp/source/any/str/utf16enc.c
blob: 025921443fc4af5f69e3bbde21927f78d34cfc26 (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
/*
	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 <zp/str.h>

void zp_utf16enc(zp_c01 * dst,zp_c02 const * src) {
	for (;;++src) {
		zp_c02 const chr = *src;

		if (chr > 0xFFFFu) { /* Two hextets. */
			zp_c02 const tmp = chr-0x10000u;

			*dst++ = (tmp>>0xAu)       |0xD800u;
			*dst++ = (tmp      &0x3FFu)|0xDC00u;

			continue;
		}

		/* One hextet. */
		*dst++ = chr;
		
		zp_unlik (chr == 0x0u) {break;}
	}
}