aboutsummaryrefslogtreecommitdiff
path: root/lib/unicode/string/u8casefold.c
blob: fc9ed545c3c0948263508313b91b1d7f3f9435e4 (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
#include <alloc.h>
#include <errno.h>
#include <stdckdint.h>

#include "macros.h"
#include "mbstring.h"
#include "unicode/prop.h"
#include "unicode/string.h"

char8_t *
u8casefold(size_t *dstn, u8view_t sv, caseflags_t flags, allocator_t mem)
{
	ASSUME(dstn != nullptr);

	size_t bufsz;
	if (ckd_mul(&bufsz, sv.len, (size_t)U8CASEFOLD_SCALE)) {
		errno = EOVERFLOW;
		return nullptr;
	}

	char8_t *dst = new(mem, typeof(*dst), bufsz);

	rune ch;
	size_t n = 0;
	while (u8next(&ch, &sv)) {
		struct rview rv = uprop_get_cf(ch, flags & CF_LANG_AZ);
		for (size_t i = 0; i < rv.len; i++)
			n += rtou8(dst + n, bufsz - n, rv.p[i]);
	}

	*dstn = n;
	return resz(mem, dst, bufsz, n);
}