aboutsummaryrefslogtreecommitdiff
path: root/lib/unicode/string/u8upper.c
blob: 6b041f66f8ef6fa164c6ce3f0c44881d6e565384 (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
#include "mbstring.h"
#include "unicode/prop.h"
#include "unicode/string.h"

size_t
u8upper(char8_t *restrict dst, size_t dstn, const char8_t *src, size_t srcn,
        enum caseflags flags)
{
	struct ucctx ctx = {
		.az_or_tr = flags & CF_LANG_AZ,
		.lt = flags & CF_LANG_LT,
		. = flags & CF_ẞ,
	};

	rune ch;
	size_t n = 0;

	while (u8next(&ch, &src, &srcn)) {
		struct rview rv = uprop_get_uc(ch, ctx);
		for (size_t i = 0; i < rv.len; i++) {
			if (n >= dstn) {
				char8_t buf[U8_LEN_MAX];
				n += rtou8(buf, sizeof(buf), rv.p[i]);
			} else
				n += rtou8(dst + n, dstn - n, rv.p[i]);
		}
		ctx.after_i = ch == 'i';
	}

	return n;
}