iri_string/parser/trusted/
authority.rs

1//! Parsers for trusted `authority` string.
2
3use crate::components::AuthorityComponents;
4use crate::parser::str::{find_split_hole, rfind_split2};
5
6/// Decomposes the authority into `(userinfo, host, port)`.
7///
8/// The leading `:` is truncated.
9///
10/// # Precondition
11///
12/// The given string must be a valid IRI reference.
13#[inline]
14#[must_use]
15pub(crate) fn decompose_authority(authority: &str) -> AuthorityComponents<'_> {
16    let i = authority;
17    let (i, host_start) = match find_split_hole(i, b'@') {
18        Some((userinfo, rest)) => (rest, userinfo.len() + 1),
19        None => (authority, 0),
20    };
21    let colon_port_len = match rfind_split2(i, b':', b']') {
22        Some((_, suffix)) if suffix.starts_with(':') => suffix.len(),
23        _ => 0,
24    };
25    let host_end = authority.len() - colon_port_len;
26
27    AuthorityComponents {
28        authority,
29        host_start,
30        host_end,
31    }
32}