aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_flipped.c
blob: 43893fc2a1e09e67bcafac90d66e94ebd4a911b2 (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
52
53
54
55
56
57
58
59
/* See LICENSE file for copyright and license details. */
#include "lib-common.h"


static struct {
	uint_least32_t a;
	uint_least32_t b;
} pairs[] = {
	{0x00A1, 0x0021}
};


enum libcharconv_result
libcharconv_flipped(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp)
{
	uint_least32_t c;
	size_t i, clen;
	*n = 0;
	while (slen) {
		clen = libcharconv_decode_utf8_(s, slen, &c);
		if (clen > slen) {
			if (*n)
				goto no_conv;
			return LIBCHARCONV_INDETERMINATE;
		}
		if (!clen) {
			*n += 1u;
			slen -= 1u;
			s = &s[1];
			continue;
		}

		for (i = 0u; i < sizeof(pairs) / sizeof(*pairs); i++) {
			if (c == pairs[i].a) {
				c = pairs[i].b;
				goto conv;
			}
			if (c == pairs[i].b) {
				c = pairs[i].a;
				goto conv;
			}
		}

		*n += clen;
		s = &s[clen];
		slen -= clen;
	}
no_conv:
	return LIBCHARCONV_NO_CONVERT;

conv:
	if (*n)
		goto no_conv;
	if (*ncp)
		*cp = c;
	*n += clen;
	*ncp = 1u;
	return LIBCHARCONV_CONVERTED;
}