Skip to main content

luaur_vm/functions/
classend.rs

1use crate::functions::lua_l_error_l::lua_l_error_l;
2use crate::macros::l_esc::L_ESC;
3use crate::records::match_state::MatchState;
4use crate::type_aliases::match_state::MatchState as MatchStateAlias;
5use core::ffi::c_char;
6
7pub fn classend(ms: *mut MatchState, p: *const c_char) -> *const c_char {
8    unsafe {
9        let p = p.add(1);
10        match *p.offset(-1) as u8 {
11            x if x == L_ESC as u8 => {
12                if p == (*ms).p_end {
13                    lua_l_error_l(
14                        (*ms).L,
15                        c"malformed pattern (ends with '%%')".as_ptr(),
16                        core::format_args!("malformed pattern (ends with '%%')"),
17                    );
18                }
19                p.add(1)
20            }
21            b'[' => {
22                let mut p = p;
23                if *p == b'^' as c_char {
24                    p = p.add(1);
25                }
26                loop {
27                    if p == (*ms).p_end {
28                        lua_l_error_l(
29                            (*ms).L,
30                            c"malformed pattern (missing ']')".as_ptr(),
31                            core::format_args!("malformed pattern (missing ']')"),
32                        );
33                    }
34                    p = p.add(1);
35                    if *p.offset(-1) == L_ESC && p < (*ms).p_end {
36                        p = p.add(1);
37                    }
38                    if *p == b']' as c_char {
39                        break;
40                    }
41                }
42                p.add(1)
43            }
44            _ => p,
45        }
46    }
47}