Skip to main content

solar_parse/
natspec.rs

1/// Splits a string slice at the first whitespace character.
2///
3/// Returns the content up to the whitespace and the position of the first following non-blank char.
4#[inline]
5pub fn split_once_ws(content: &str, start: usize, end: usize) -> (&str, usize) {
6    let bytes = content.as_bytes();
7    if let Some(ws_pos) =
8        bytes[start..end].iter().position(|b| b.is_ascii_whitespace()).map(|offset| start + offset)
9    {
10        let rest = &bytes[ws_pos..end];
11        (&content[start..ws_pos], ws_pos + (rest.len() - rest.trim_ascii_start().len()))
12    } else {
13        (&content[start..end], end)
14    }
15}
16
17/// Splits a string slice at the end of its leading identifier.
18///
19/// Returns the identifier prefix and the position of the first following non-blank char; the
20/// prefix is empty when the content does not start with an identifier character. This mirrors
21/// solc, which parses documented parameter names as identifier prefixes, so `@param -` documents
22/// an unnamed parameter.
23#[inline]
24pub fn split_once_ident(content: &str, start: usize, end: usize) -> (&str, usize) {
25    let bytes = content.as_bytes();
26    let ident_len = bytes[start..end]
27        .iter()
28        .position(|&b| !crate::lexer::is_id_continue_byte(b))
29        .unwrap_or(end - start);
30    let ident_end = start + ident_len;
31    let rest = &bytes[ident_end..end];
32    (&content[start..ident_end], ident_end + (rest.len() - rest.trim_ascii_start().len()))
33}
34
35/// Returns the first non-blank word and the position of the first following non-blank char.
36#[inline]
37pub fn first_word(content: &str, start: usize, end: usize) -> Option<(&str, usize)> {
38    let bytes = &content.as_bytes()[start..end];
39    let start = start + (bytes.len() - bytes.trim_ascii_start().len());
40    let (word, content_start) = split_once_ws(content, start, end);
41    if word.is_empty() { None } else { Some((word, content_start)) }
42}