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

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

char8_t *
u8upper(size_t *dstn, struct u8view sv, enum caseflags flags, alloc_fn alloc,
        void *alloc_ctx)
{
	ASSUME(dstn != nullptr);
	ASSUME(alloc != nullptr);

	struct ucctx ctx = {
		.az_or_tr = flags & CF_LANG_AZ,
		.lt = flags & CF_LANG_LT,
		. = flags & CF_ẞ,
	};

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

	char8_t *dst = alloc(alloc_ctx, nullptr, 0, bufsz, alignof(char8_t));

	rune ch;
	size_t n = 0;
	while (u8next(&ch, &sv)) {
		struct rview rv = uprop_get_uc(ch, ctx);
		for (size_t i = 0; i < rv.len; i++)
			n += rtou8(dst + n, bufsz - n, rv.p[i]);
		if (ctx.lt) {
			enum uprop_ccc ccc;
			if (uprop_is_sd(ch))
				ctx.after_soft_dotted = true;
			else if ((ccc = uprop_get_ccc(ch)) == CCC_NR || ccc == CCC_L)
				ctx.after_soft_dotted = false;
		}
	}

	*dstn = n;
	return alloc(alloc_ctx, dst, bufsz, n, alignof(char8_t));
}