-
Notifications
You must be signed in to change notification settings - Fork 0
/
ujson_utf.c
105 lines (74 loc) · 1.37 KB
/
ujson_utf.c
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright (C) 2022-2024 Cyril Hrubis <metan@ucw.cz>
*/
#include <stddef.h>
#include <ujson_utf.h>
int8_t ujson_utf8_next_chsz(const char *str, size_t off)
{
char ch = str[off];
uint8_t len = 0;
if (!ch)
return 0;
if (UJSON_UTF8_IS_ASCII(ch))
return 1;
if (UJSON_UTF8_IS_2BYTE(ch)) {
len = 2;
goto ret;
}
if (UJSON_UTF8_IS_3BYTE(ch)) {
len = 3;
goto ret;
}
if (UJSON_UTF8_IS_4BYTE(ch)) {
len = 4;
goto ret;
}
return -1;
ret:
if (!UJSON_UTF8_IS_NBYTE(str[off+1]))
return -1;
if (len > 2 && !UJSON_UTF8_IS_NBYTE(str[off+2]))
return -1;
if (len > 3 && !UJSON_UTF8_IS_NBYTE(str[off+3]))
return -1;
return len;
}
int8_t ujson_utf8_prev_chsz(const char *str, size_t off)
{
char ch;
if (!off)
return 0;
ch = str[--off];
if (UJSON_UTF8_IS_ASCII(ch))
return 1;
if (!UJSON_UTF8_IS_NBYTE(ch))
return -1;
if (off < 1)
return -1;
ch = str[--off];
if (UJSON_UTF8_IS_2BYTE(ch))
return 2;
if (!UJSON_UTF8_IS_NBYTE(ch))
return -1;
if (off < 1)
return -1;
ch = str[--off];
if (UJSON_UTF8_IS_3BYTE(ch))
return 3;
if (!UJSON_UTF8_IS_NBYTE(ch))
return -1;
if (off < 1)
return -1;
ch = str[--off];
if (UJSON_UTF8_IS_4BYTE(ch))
return 4;
return -1;
}
size_t ujson_utf8_strlen(const char *str)
{
size_t cnt = 0;
while (ujson_utf8_next(&str))
cnt++;
return cnt;
}