aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_clock_faces.c
blob: cd49b53cbdb806fa0426861dc44428fd72c71768 (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
63
64
65
/* See LICENSE file for copyright and license details. */
#include "lib-common.h"


enum libcharconv_result
libcharconv_clock_faces(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp)
{
	int num, thirty;
	*n = 0;
	for (; slen--; s++) {
		if ('0' <= s[0] && s[0] <= '9') {
			if (*n)
				goto no_conv;
			num = (int)(s[0] - '0');
			if (!slen--)
				goto indeterminate;
			*n = 2u;
			if (s[1] == ':')
				goto conv;
			if ('0' > s[1] || s[1] > '9')
				goto no_conv;
			num *= 10;
			num += (int)(s[1] - '0');
			if (!slen--)
				goto indeterminate;
			if (num > 24)
				goto no_conv;
			*n = 3u;
			if (s[2] == ':')
				goto conv;
			goto no_conv;
		} else {
			*n += 1u;
		}
	}
no_conv:
	return LIBCHARCONV_NO_CONVERT;

indeterminate:
	return LIBCHARCONV_INDETERMINATE;

conv:
	if (!slen--)
		goto indeterminate;
	if (s[*n] == '3')
		thirty = 1;
	else if (s[*n] == '0')
		thirty = 0;
	else
		goto no_conv;
	if (!slen--)
		goto indeterminate;
	if (s[*n + 1u] != '0')
		goto no_conv;
	*n += 2u;
	if (num > (thirty ? 23 : 34))
		goto no_conv;
	num %= 12;
	num = (!num ? 12 : num) - 1;
	num += 12 * thirty;
	if (*ncp)
		*cp = UINT32_C(0x1F550) + (uint_least32_t)num;
	*ncp = 1u;
	return LIBCHARCONV_CONVERTED;
}