compiler_tools/
util.rs

1/// Simple parse function for a string token with an arbitrary delimeter
2pub fn parse_str<const DELIMITER: char>(input: &str) -> Option<(&str, &str)> {
3    if !input.starts_with(DELIMITER) {
4        return None;
5    }
6    let mut escaped = false;
7    let mut iter = input.char_indices().skip(1);
8    while let Some((i, c)) = iter.next() {
9        if escaped {
10            escaped = false;
11            continue;
12        }
13        if c == '\\' {
14            escaped = true;
15            continue;
16        }
17        if c == DELIMITER {
18            if let Some((_, c)) = iter.next() {
19                if c == DELIMITER {
20                    continue;
21                }
22            }
23            return Some((&input[..i + c.len_utf8()], &input[i + c.len_utf8()..]));
24        }
25    }
26
27    None
28}