fn check_bracket(line: &str, pos: usize) -> Option<(u8, usize)> {
    if line.is_empty() {
        return None;
    }
    let mut pos = pos;
    if pos >= line.len() {
        pos = line.len() - 1; // before cursor
        let b = line.as_bytes()[pos]; // previous byte
        if is_close_bracket(b) {
            Some((b, pos))
        } else {
            None
        }
    } else {
        let mut under_cursor = true;
        loop {
            let b = line.as_bytes()[pos];
            if is_close_bracket(b) {
                return if pos == 0 { None } else { Some((b, pos)) };
            } else if is_open_bracket(b) {
                return if pos + 1 == line.len() {
                    None
                } else {
                    Some((b, pos))
                };
            } else if under_cursor && pos > 0 {
                under_cursor = false;
                pos -= 1; // or before cursor
            } else {
                return None;
            }
        }
    }
}
// check under or before the cursor
check under or before the cursor
check under or before the cursor
fn check_bracket(line: &str, pos: usize) -> Option<(u8, usize)> {
    if line.is_empty() {
        return None;
    }
    let mut pos = pos;
    if pos >= line.len() {
        pos = line.len() - 1; // before cursor
        let b = line.as_bytes()[pos]; // previous byte
        if is_close_bracket(b) {
            Some((b, pos))
        } else {
            None
        }
    } else {
        let mut under_cursor = true;
        loop {
            let b = line.as_bytes()[pos];
            if is_close_bracket(b) {
                return if pos == 0 { None } else { Some((b, pos)) };
            } else if is_open_bracket(b) {
                return if pos + 1 == line.len() {
                    None
                } else {
                    Some((b, pos))
                };
            } else if under_cursor && pos > 0 {
                under_cursor = false;
                pos -= 1; // or before cursor
            } else {
                return None;
            }
        }
    }
}
fn check_bracket ( line : & str , pos : usize ) -> Option < ( u8 , usize ) > { if line . is_empty ( ) { return None ; } let mut pos = pos ; if pos >= line . len ( ) { pos = line . len ( ) - 1 ; // before cursor let b = line . as_bytes ( ) [ pos ] ; // previous byte if is_close_bracket ( b ) { Some ( ( b , pos ) ) } else { None } } else { let mut under_cursor = true ; loop { let b = line . as_bytes ( ) [ pos ] ; if is_close_bracket ( b ) { return if pos == 0 { None } else { Some ( ( b , pos ) ) } ; } else if is_open_bracket ( b ) { return if pos + 1 == line . len ( ) { None } else { Some ( ( b , pos ) ) } ; } else if under_cursor && pos > 0 { under_cursor = false ; pos -= 1 ; // or before cursor } else { return None ; } } } }
check under or before the cursor
check under or before the cursor
line
&str
pos
usize
line
&str
pos
usize