-
Notifications
You must be signed in to change notification settings - Fork 5
/
parse.c
88 lines (81 loc) · 2.79 KB
/
parse.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
#include <malloc.h>
#include <stddef.h>
#include <string.h>
// functions for parsing
const char *jassonpath_match_string(const char *begin, const char *end) {
if (*begin == '\"')
begin++; // begin should be left quotation mark to match
else
return begin;
for (; begin != end && *begin; begin++) {
if (begin[0] == '\\' && begin[1] != '\0')
begin += 2;
else if (begin[0] == '\"') {
begin++;
break;
}
}
return begin;
}
const char *jassonpath_next_delima(const char *begin, const char *end) {
for (; begin != end && *begin; begin++) {
if (begin[0] == '\"') begin = jassonpath_match_string(begin, end);
if (begin[0] == '[' || begin[0] == ']' || begin[0] == '.') break;
}
return begin;
}
const char *jassonpath_next_matched_bracket(const char *begin, const char *end,
char left, char right) {
if (*begin == left)
begin++; // begin should be left bracket to match
else
return begin;
int count = 1;
for (; begin != end && *begin; begin++) {
if (begin[0] == '\"') begin = jassonpath_match_string(begin, end);
if (begin[0] == left)
count++;
else if (begin[0] == right)
count--;
if (!count) break;
}
return begin;
}
const char *jassonpath_next_seprator(const char *begin, const char *end,
char sep) {
for (; begin != end && *begin; begin++) {
if (begin[0] == '\"') begin = jassonpath_match_string(begin, end);
if (begin[0] == sep) break;
}
return begin;
}
const char *jassonpath_next_punctors_outside_para(const char *begin,
const char *end, char *sep) {
for (; begin != end && *begin; begin++) {
if (begin[0] == '\"') begin = jassonpath_match_string(begin, end);
if (begin[0] == '(')
begin = jassonpath_next_matched_bracket(begin, end, '(', ')');
else if (strchr(sep, begin[0]))
break;
}
return begin;
}
const char *jassonpath_next_punctor_outside_para(const char *begin,
const char *end, char sep) {
for (; begin != end && *begin; begin++) {
if (begin[0] == '\"') begin = jassonpath_match_string(begin, end);
if (begin[0] == '(')
begin = jassonpath_next_matched_bracket(begin, end, '(', ')');
else if (begin[0] == sep)
break;
}
return begin;
}
const char *jassonpath_strdup_no_terminal(const char *begin, const char *end) {
size_t len = end - begin;
if (!end) len = strlen(begin);
char *ret = (char *)malloc(sizeof(char) * (len + 1));
memcpy(ret, begin, len);
ret[len] = '\0';
return ret;
}