aboutsummaryrefslogtreecommitdiff
path: root/lib/mbstring/u8prev.c
blob: 507f7e173b7b3b8a67fd29d8bb6e5e7e5e73990b (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
#include <stddef.h>

#include "mbstring.h"
#include "rune.h"

int
u8prev(rune *ch, const char8_t **p, const char8_t *start)
{
	int off;
	const char8_t *s = *p;
	ptrdiff_t d = s - start;

	if (d <= 0) {
		return 0;
	} else if (U8_BYTE_1(s[-1])) {
		if (ch)
			*ch = s[-1];
		off = 1;
	} else if (d > 1 && U8_BYTE_C(s[-1]) && U8_BYTE_2(s[-2])) {
		if (ch)
			*ch = ((s[-2] & 0x1F) << 6) | (s[-1] & 0x3F);
		off = 2;
	} else if (d > 2 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2])
			&& U8_BYTE_3(s[-3])) {
		if (ch) {
			*ch =
				((s[-3] & 0x0F) << 12) | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F);
		}
		off = 3;
	} else if (d > 3 && U8_BYTE_C(s[-1]) && U8_BYTE_C(s[-2]) && U8_BYTE_C(s[-3])
			&& U8_BYTE_4(s[-4])) {
		if (ch) {
			*ch = ((s[-4] & 0x07) << 18) | ((s[-3] & 0x3F) << 12)
			    | ((s[-2] & 0x3F) << 6) | (s[-1] & 0x3F);
		}
		off = 4;
	} else {
		if (ch)
			*ch = RUNE_ERROR;
		off = 1;
	}

	*p -= off;
	return off;
}