aboutsummaryrefslogtreecommitdiffstats
path: root/libcharconv_mahjong_tiles.c
blob: 7bc60baccfab69276ab22d6173a7088f4d7839e7 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* See LICENSE file for copyright and license details. */
#include "lib-common.h"


enum libcharconv_result
libcharconv_mahjong_tiles(const char *s, size_t slen, size_t *n, uint_least32_t *cp, size_t *ncp)
{
	uint_least32_t c;
	*n = 0;
	for (; slen--; s++, ++*n) {
		PLAIN_CASE_SELECT("pobc", 0x1F022);
		PLAIN_CASE_SELECT("j#", 0x1F02A);
		if (tolower(s[0]) == 'e') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'w') {
				c = UINT32_C(0x1F000);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 's') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'w') {
				c = UINT32_C(0x1F001);
				goto conv2;
			} else if (tolower(s[1]) == 'p') {
				c = UINT32_C(0x1F026);
				goto conv2;
			} else if (tolower(s[1]) == 'u') {
				c = UINT32_C(0x1F027);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 'w') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'w') {
				c = UINT32_C(0x1F002);
				goto conv2;
			} else if (tolower(s[1]) == 'd') {
				c = UINT32_C(0x1F006);
				goto conv2;
			} else if (tolower(s[1]) == 'i') {
				c = UINT32_C(0x1F029);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 'n') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'w') {
				c = UINT32_C(0x1F003);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 'r') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'd') {
				c = UINT32_C(0x1F004);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 'g') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'd') {
				c = UINT32_C(0x1F005);
				goto conv2;
			}
			goto no_conv;
		}
		if (tolower(s[0]) == 'a') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'u') {
				c = UINT32_C(0x1F028);
				goto conv2;
			}
			goto no_conv;
		}
		if ('1' <= s[0] && s[0] <= '9') {
			if (!slen)
				goto indeterminate;
			if (tolower(s[1]) == 'c')
				c = UINT32_C(0x1F007);
			else if (tolower(s[1]) == 'b')
				c = UINT32_C(0x1F010);
			else if (tolower(s[1]) == 'o')
				c = UINT32_C(0x1F019);
			else
				goto no_conv;
			c += (uint_least32_t)(s[0] - '1');
			goto conv2;
		}
	}
	return LIBCHARCONV_NO_CONVERT;

no_conv:
	*n += 1u;
	return LIBCHARCONV_NO_CONVERT;

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

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

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