Skip to main content

luaur_analysis/methods/
lint_format_string_check_string_replace.rs

1use core::ffi::c_char;
2
3impl crate::records::lint_format_string::LintFormatString {
4    #[inline]
5    pub fn check_string_replace(
6        &self,
7        data: *const c_char,
8        size: usize,
9        captures: i32,
10    ) -> *const c_char {
11        let mut i = 0;
12        while i < size {
13            if unsafe { *data.add(i) } == b'%' as i8 {
14                i += 1;
15
16                if i == size {
17                    return c"unfinished replacement".as_ptr();
18                }
19
20                let next_ch = unsafe { *data.add(i) };
21                if next_ch != b'%' as i8 && !self.is_digit(next_ch) {
22                    return c"unexpected replacement character; must be a digit or %".as_ptr();
23                }
24
25                if self.is_digit(next_ch)
26                    && captures >= 0
27                    && (next_ch as u8 - b'0') as i32 > captures
28                {
29                    return c"invalid capture index, must refer to pattern capture".as_ptr();
30                }
31            }
32
33            i += 1;
34        }
35
36        core::ptr::null()
37    }
38}