aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_braille.c
blob: 3a9807eae5fd7700d6df86a680bb1ab4ee418f61 (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
60
61
62
/* See LICENSE file for copyright and license details. */
#include "lib-common.h"


enum libcharconv_result
libcharconv_braille(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp)
{
	unsigned pattern, dot;
	uint_least32_t c;
	*n = 0;
	for (; slen--; s++) {
		if (s[0] == '0') {
			if (*n)
				goto no_conv;
			c = UINT32_C(0x2800);
			*n = 1u;
			goto conv;
		} else if ('1' <= s[0] && s[0] <= '8') {
			if (*n)
				goto no_conv;
			pattern = 1u << (s[0] - '1');
			*n = 1u;
			for (;;) {
				if (!slen--)
					goto indeterminate;
				if ('1' <= s[*n] && s[*n] <= '8') {
					dot = 1u << (s[*n] - '1');
					if (pattern & dot)
						break;
					pattern |= dot;
					*n += 1u;
				} else if (s[*n] == '0') {
					*n += 1u;
					break;
				} else {
					break;
				}
			}
			c = UINT32_C(0x2800) | pattern;
			goto conv;
		} else {
			*n += 1u;
		}
	}
	return LIBCHARCONV_NO_CONVERT;

no_conv:
	if (!*n)
		*n = 1u;
	return LIBCHARCONV_NO_CONVERT;

indeterminate:
	if (*n)
		goto no_conv;
	return LIBCHARCONV_INDETERMINATE;

conv:
	if (*ncp)
		*cp = c;
	*ncp = 1u;
	return LIBCHARCONV_CONVERTED;
}