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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::functions::classend::classend;
use crate::functions::end_capture::end_capture;
use crate::functions::match_capture::match_capture;
use crate::functions::matchbalance::matchbalance;
use crate::functions::matchbracketclass::matchbracketclass;
use crate::functions::max_expand::max_expand;
use crate::functions::min_expand::min_expand;
use crate::functions::singlematch::singlematch;
use crate::functions::start_capture::start_capture;
use crate::macros::cap_position::CAP_POSITION;
use crate::macros::cap_unfinished::CAP_UNFINISHED;
use crate::macros::l_esc::L_ESC;
use crate::macros::lua_l_error::luaL_error;
use crate::macros::uchar::uchar;
use crate::records::match_state::MatchState;
use core::ffi::{c_char, c_int};
pub(crate) unsafe fn match_item(
ms: *mut MatchState,
mut s: *const c_char,
mut p: *const c_char,
) -> *const c_char {
if (*ms).matchdepth == 0 {
luaL_error!((*ms).L, "pattern too complex");
}
(*ms).matchdepth -= 1;
let L = (*ms).L;
if let Some(interrupt) = (*(*L).global).cb.interrupt {
(*L).nCcalls = (*L).nCcalls.wrapping_add(1);
interrupt(L, -1);
(*L).nCcalls = (*L).nCcalls.wrapping_sub(1);
}
'init: loop {
if p != (*ms).p_end {
match *p as u8 {
b'(' => {
if *p.add(1) == b')' as c_char {
s = start_capture(ms, s, p.add(2), CAP_POSITION);
} else {
s = start_capture(ms, s, p.add(1), CAP_UNFINISHED);
}
break 'init; // C++ outer-switch `break`: do not fall into dflt
}
b')' => {
s = end_capture(ms, s, p.add(1));
break 'init; // C++ outer-switch `break`: do not fall into dflt
}
b'$' => {
if p.add(1) != (*ms).p_end {
// default case below
} else {
s = if s == (*ms).src_end {
s
} else {
core::ptr::null()
};
break 'init;
}
}
x if x == L_ESC as u8 => match *p.add(1) as u8 {
b'b' => {
s = matchbalance(ms, s, p.add(2));
if !s.is_null() {
p = p.add(4);
continue 'init;
}
break 'init;
}
b'f' => {
p = p.add(2);
if *p != b'[' as c_char {
luaL_error!((*ms).L, "missing '[' after '%%f' in pattern");
}
let ep = classend(ms, p);
let previous = if s == (*ms).src_init {
0
} else {
*s.offset(-1)
};
if matchbracketclass(uchar(previous as c_int) as c_int, p, ep.offset(-1))
== 0
&& matchbracketclass(uchar(*s as c_int) as c_int, p, ep.offset(-1)) != 0
{
p = ep;
continue 'init;
}
s = core::ptr::null();
break 'init;
}
b'0'..=b'9' => {
s = match_capture(ms, s, uchar(*p.add(1) as c_int) as c_int);
if !s.is_null() {
p = p.add(2);
continue 'init;
}
break 'init;
}
_ => {}
},
_ => {}
}
if !s.is_null() {
let ep = classend(ms, p);
if singlematch(ms, s, p, ep) == 0 {
if *ep == b'*' as c_char || *ep == b'?' as c_char || *ep == b'-' as c_char {
p = ep.add(1);
continue 'init;
}
s = core::ptr::null();
} else {
match *ep as u8 {
b'?' => {
let res = match_item(ms, s.add(1), ep.add(1));
if !res.is_null() {
s = res;
} else {
p = ep.add(1);
continue 'init;
}
}
b'+' => {
s = s.add(1);
s = max_expand(ms, s, p, ep);
}
b'*' => {
s = max_expand(ms, s, p, ep);
}
b'-' => {
s = min_expand(ms, s, p, ep);
}
_ => {
s = s.add(1);
p = ep;
continue 'init;
}
}
}
}
}
break 'init;
}
(*ms).matchdepth += 1;
s
}