perl_parser_core/syntax/
text_line.rs1#![deny(unsafe_code)]
8#![warn(rust_2018_idioms)]
9#![warn(missing_docs)]
10
11#[must_use]
16pub fn line_bounds_at(text: &str, cursor_pos: usize) -> (usize, usize) {
17 let cursor = cursor_pos.min(text.len());
18 let start = text[..cursor].rfind('\n').map_or(0, |idx| idx + 1);
19 let end = text[cursor..].find('\n').map_or(text.len(), |idx| cursor + idx);
20 (start, end)
21}
22
23#[must_use]
25pub fn is_identifier_byte(byte: u8) -> bool {
26 byte.is_ascii_alphanumeric() || byte == b'_'
27}
28
29#[must_use]
32pub fn is_keyword_boundary(bytes: &[u8], start: usize, len: usize) -> bool {
33 if start > bytes.len() {
34 return false;
35 }
36
37 let end = start.saturating_add(len);
38 if end > bytes.len() {
39 return false;
40 }
41
42 if start > 0 && is_identifier_byte(bytes[start - 1]) {
43 return false;
44 }
45
46 if end < bytes.len() && is_identifier_byte(bytes[end]) {
47 return false;
48 }
49
50 true
51}
52
53#[must_use]
55pub fn skip_ascii_whitespace(bytes: &[u8], mut idx: usize) -> usize {
56 while idx < bytes.len() && bytes[idx].is_ascii_whitespace() {
57 idx += 1;
58 }
59 idx
60}