grippo 0.3.1

Grok, regex, CEL and scrubbing on typed primitives. Up to 9 times faster than the stock Rust regex.
Documentation
// Project:   grippo
// File:      src/uri/mod.rs
// Purpose:   URI references split into borrowed parts
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

//! URI references split into borrowed parts.
//!
//! [`split`] is RFC 3986's generic split, relative references kept and
//! nothing checked or normalised. [`java`] reads a reference the way
//! `java.net.URI` does, with `java.net.URL` as the fallback, and
//! [`percent`] decodes escapes in the form, URI and lenient readings.

pub mod java;
pub mod percent;

/// A URI reference split into its parts, each a span of the text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct UriRef<'a> {
    /// The text before the first `:`, where that text is a scheme name.
    pub scheme: Option<&'a str>,
    /// The text before the last `@` of the authority.
    pub user_info: Option<&'a str>,
    /// The authority's host. `Some` whenever there's an authority, even an empty one.
    pub host: Option<&'a str>,
    /// The text after the port's `:`, as written.
    pub port: Option<&'a str>,
    /// The path, possibly empty.
    pub path: &'a str,
    /// The text after the first `?`.
    pub query: Option<&'a str>,
    /// The text after the first `#`.
    pub fragment: Option<&'a str>,
}

/// `uri` split into its parts per RFC 3986's generic syntax.
///
/// Any text splits: a bare path, a host with no scheme or a relative
/// reference comes back as a path. The port starts at the authority's last
/// colon, or for a bracketed host the first colon after its closing bracket.
#[must_use]
pub fn split(uri: &str) -> UriRef<'_> {
    let (rest, fragment) = match uri.split_once('#') {
        Some((before, after)) => (before, Some(after)),
        None => (uri, None),
    };
    let (rest, query) = match rest.split_once('?') {
        Some((before, after)) => (before, Some(after)),
        None => (rest, None),
    };
    let (rest, scheme) = match rest.find(':') {
        Some(colon) if is_scheme(&rest[..colon]) => (&rest[colon + 1..], Some(&rest[..colon])),
        _ => (rest, None),
    };

    let Some(after_slashes) = rest.strip_prefix("//") else {
        return UriRef {
            scheme,
            user_info: None,
            host: None,
            port: None,
            path: rest,
            query,
            fragment,
        };
    };

    let end = after_slashes.find('/').unwrap_or(after_slashes.len());
    let (authority, path) = after_slashes.split_at(end);
    let (user_info, host_port) = match authority.rsplit_once('@') {
        Some((user, host)) => (Some(user), host),
        None => (None, authority),
    };
    let colon = host_port.rfind(']').map_or_else(
        || host_port.rfind(':'),
        |bracket| host_port[bracket..].find(':').map(|at| bracket + at),
    );
    let (host, port) = colon.map_or((host_port, None), |at| {
        (&host_port[..at], Some(&host_port[at + 1..]))
    });

    UriRef {
        scheme,
        user_info,
        host: Some(host),
        port,
        path,
        query,
        fragment,
    }
}

/// The extension of a path: the text after the last `.` of its last
/// segment, or `None` where that segment has no dot, the dot ends it, or the
/// path has no `/` at all.
#[must_use]
pub fn extension(path: &str) -> Option<&str> {
    let segment = &path[path.rfind('/')?..];
    let dot = segment.rfind('.')?;
    let extension = &segment[dot + 1..];
    (!extension.is_empty()).then_some(extension)
}

/// Whether `candidate` is `ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )`.
fn is_scheme(candidate: &str) -> bool {
    let mut bytes = candidate.bytes();
    bytes.next().is_some_and(|b| b.is_ascii_alphabetic())
        && bytes.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
}

#[cfg(test)]
mod tests;