use crate::event::{TagForm, TriviaKind};
pub(super) fn byte(src: &str, i: usize) -> Option<u8> {
src.as_bytes().get(i).copied()
}
pub(super) fn char_at(src: &str, i: usize) -> Option<char> {
src.get(i..).and_then(|s| s.chars().next())
}
pub(super) const fn is_white(b: u8) -> bool {
b == b' ' || b == b'\t'
}
pub(super) const fn is_break(b: u8) -> bool {
b == b'\n' || b == b'\r'
}
pub(super) fn blank_or_end(src: &str, i: usize) -> bool {
byte(src, i).is_none_or(|b| is_white(b) || is_break(b))
}
pub(super) fn break_or_end(src: &str, i: usize) -> bool {
byte(src, i).is_none_or(is_break)
}
pub(super) fn break_len(src: &str, i: usize) -> usize {
match byte(src, i) {
Some(b'\r') if byte(src, i.saturating_add(1)) == Some(b'\n') => 2,
Some(b'\n' | b'\r') => 1,
_ => 0,
}
}
pub(super) fn line_end(src: &str, i: usize) -> usize {
src.get(i..)
.and_then(|rest| rest.find(['\n', '\r']))
.map_or(src.len(), |k| i.saturating_add(k))
}
pub(super) fn skip_white(src: &str, i: usize) -> usize {
let run = src
.as_bytes()
.get(i..)
.map_or(0, |rest| rest.iter().take_while(|b| is_white(**b)).count());
i.saturating_add(run)
}
pub(super) fn first_mark(src: &str, from: usize, to: usize) -> Option<usize> {
let text = src.get(from..to)?;
text.find('\u{feff}').map(|k| from.saturating_add(k))
}
pub(super) fn mark_at(src: &str, i: usize) -> bool {
src.get(i..).is_some_and(|r| r.starts_with('\u{feff}'))
}
pub(super) const fn is_printable(c: char) -> bool {
matches!(c,
'\t' | '\n' | '\r' | '\x20'..='\x7e' | '\u{85}' | '\u{a0}'..='\u{d7ff}'
| '\u{e000}'..='\u{fffd}' | '\u{10000}'..)
}
pub(super) const fn is_nb_json(c: char) -> bool {
c == '\t' || c >= '\x20'
}
pub(super) const fn is_ns_char(c: char) -> bool {
is_printable(c) && !matches!(c, ' ' | '\t' | '\n' | '\r' | '\u{feff}')
}
pub(super) const fn is_flow_indicator(c: char) -> bool {
matches!(c, ',' | '[' | ']' | '{' | '}')
}
const fn is_indicator(c: char) -> bool {
matches!(
c,
'-' | '?'
| ':'
| ','
| '['
| ']'
| '{'
| '}'
| '#'
| '&'
| '*'
| '!'
| '|'
| '>'
| '\''
| '"'
| '%'
| '@'
| '`'
)
}
fn plain_safe_at(src: &str, i: usize, flow: bool) -> bool {
char_at(src, i).is_some_and(|c| is_ns_char(c) && !(flow && is_flow_indicator(c)))
}
pub(super) fn is_plain_first(src: &str, i: usize, flow: bool) -> bool {
match char_at(src, i) {
Some('-' | '?' | ':') => plain_safe_at(src, i.saturating_add(1), flow),
Some(c) => is_ns_char(c) && !is_indicator(c),
None => false,
}
}
pub(super) fn indicator_at(src: &str, i: usize, c: u8) -> bool {
byte(src, i) == Some(c) && blank_or_end(src, i.saturating_add(1))
}
pub(super) fn marker_at(src: &str, i: usize, marker: &str) -> bool {
src.get(i..)
.is_some_and(|rest| rest.starts_with(marker) && blank_or_end(src, i.saturating_add(3)))
}
pub(super) fn any_marker_at(src: &str, i: usize) -> bool {
marker_at(src, i, "---") || marker_at(src, i, "...")
}
pub(super) fn plain_line_end(src: &str, start: usize, flow: bool) -> usize {
line_text_end(src, start, flow, false)
}
pub(super) fn plain_line_end_lenient(src: &str, start: usize) -> usize {
line_text_end(src, start, false, true)
}
fn line_text_end(src: &str, start: usize, flow: bool, colons: bool) -> usize {
let mut i = start;
let mut end = start;
while let Some(c) = char_at(src, i) {
match c {
' ' | '\t' => {
i = i.saturating_add(1);
continue;
}
':' if !colons && !plain_safe_at(src, i.saturating_add(1), flow) => break,
'#' if i > start
&& src
.as_bytes()
.get(i.saturating_sub(1))
.is_some_and(|b| is_white(*b)) =>
{
break;
}
c if !is_ns_char(c) || (flow && is_flow_indicator(c)) => break,
_ => {}
}
i = i.saturating_add(c.len_utf8());
end = i;
}
end
}
pub(super) fn anchor_end(src: &str, i: usize) -> usize {
let mut j = i;
while let Some(c) = char_at(src, j) {
if !is_ns_char(c) || is_flow_indicator(c) {
break;
}
j = j.saturating_add(c.len_utf8());
}
j
}
const fn is_word_char(b: u8) -> bool {
b.is_ascii_alphanumeric() || b == b'-'
}
fn uri_char_len(src: &str, i: usize, tag_char: bool) -> usize {
let Some(b) = byte(src, i) else { return 0 };
let hex = |k: usize| byte(src, i.saturating_add(k)).is_some_and(|h| h.is_ascii_hexdigit());
match b {
b'%' if hex(1) && hex(2) => 3,
b'!' | b',' | b'[' | b']' if tag_char => 0,
_ if is_word_char(b) => 1,
b'#' | b';' | b'/' | b'?' | b':' | b'@' | b'&' | b'=' | b'+' | b'$' | b'_' | b'.'
| b'~' | b'*' | b'\'' | b'(' | b')' | b'!' | b',' | b'[' | b']' => 1,
_ => 0,
}
}
pub(super) fn uri_end(src: &str, i: usize, tag_char: bool) -> usize {
let mut j = i;
loop {
let n = uri_char_len(src, j, tag_char);
if n == 0 {
return j;
}
j = j.saturating_add(n);
}
}
#[derive(Clone, Copy, Debug)]
pub(super) struct TagScan {
pub(super) form: TagForm,
pub(super) handle: (usize, usize),
pub(super) suffix: (usize, usize),
pub(super) end: usize,
}
pub(super) fn scan_tag(src: &str, start: usize) -> Option<TagScan> {
let after = start.saturating_add(1);
match byte(src, after) {
Some(b'<') => {
let from = after.saturating_add(1);
let to = uri_end(src, from, false);
(to > from && byte(src, to) == Some(b'>')).then_some(TagScan {
form: TagForm::Verbatim,
handle: (start, start),
suffix: (from, to),
end: to.saturating_add(1),
})
}
Some(b'!') => {
let from = after.saturating_add(1);
let to = uri_end(src, from, true);
(to > from).then_some(TagScan {
form: TagForm::Shorthand,
handle: (start, from),
suffix: (from, to),
end: to,
})
}
_ => {
let words = src
.as_bytes()
.get(after..)
.map_or(0, |r| r.iter().take_while(|b| is_word_char(**b)).count());
let word_end = after.saturating_add(words);
let (handle_end, from) = if words > 0 && byte(src, word_end) == Some(b'!') {
let h = word_end.saturating_add(1);
(h, h)
} else {
(after, after)
};
let to = uri_end(src, from, true);
if to == from {
return (handle_end == after).then_some(TagScan {
form: TagForm::NonSpecific,
handle: (start, after),
suffix: (after, after),
end: after,
});
}
Some(TagScan {
form: TagForm::Shorthand,
handle: (start, handle_end),
suffix: (from, to),
end: to,
})
}
}
}
pub(super) fn percent_decode_concat(prefix: &str, suffix: &str) -> Option<String> {
let mut out = Vec::with_capacity(prefix.len().saturating_add(suffix.len()));
out.extend(decoded(prefix));
out.extend(decoded(suffix));
String::from_utf8(out).ok()
}
pub(super) fn decoded(s: &str) -> impl Iterator<Item = u8> + '_ {
let bytes = s.as_bytes();
let mut i = 0usize;
core::iter::from_fn(move || {
let b = *bytes.get(i)?;
let hex = bytes
.get(i.saturating_add(1)..i.saturating_add(3))
.and_then(|h| core::str::from_utf8(h).ok())
.and_then(|h| u8::from_str_radix(h, 16).ok());
match (b, hex) {
(b'%', Some(v)) => {
i = i.saturating_add(3);
Some(v)
}
_ => {
i = i.saturating_add(1);
Some(b)
}
}
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Utf8 {
need: u8,
lo: u8,
hi: u8,
}
impl Utf8 {
pub(super) const START: Self = Self {
need: 0,
lo: 0x80,
hi: 0xBF,
};
const fn owing(need: u8, lo: u8, hi: u8) -> Self {
Self { need, lo, hi }
}
pub(super) fn push(self, b: u8) -> Option<Self> {
if self.need > 0 {
return (self.lo..=self.hi)
.contains(&b)
.then(|| Self::owing(self.need.saturating_sub(1), 0x80, 0xBF));
}
Some(match b {
0x00..=0x7F => Self::START,
0xC2..=0xDF => Self::owing(1, 0x80, 0xBF),
0xE0 => Self::owing(2, 0xA0, 0xBF),
0xED => Self::owing(2, 0x80, 0x9F),
0xE1..=0xEF => Self::owing(2, 0x80, 0xBF),
0xF0 => Self::owing(3, 0x90, 0xBF),
0xF1..=0xF3 => Self::owing(3, 0x80, 0xBF),
0xF4 => Self::owing(3, 0x80, 0x8F),
_ => return None,
})
}
pub(super) const fn complete(self) -> bool {
self.need == 0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct UriRead {
pub(super) len: usize,
pub(super) utf8: Option<Utf8>,
}
impl UriRead {
pub(super) const EMPTY: Self = Self {
len: 0,
utf8: Some(Utf8::START),
};
pub(super) fn then(self, s: &str) -> Self {
let mut out = self;
for b in decoded(s) {
out.len = out.len.saturating_add(1);
out.utf8 = out.utf8.and_then(|u| u.push(b));
}
out
}
pub(super) fn is_utf8(self) -> bool {
self.utf8.is_some_and(Utf8::complete)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct TagPrefix {
pub(super) start: usize,
pub(super) read: UriRead,
}
impl TagPrefix {
pub(super) fn read(src: &str, start: usize) -> Option<Self> {
let text = tag_prefix_at(src, start)?;
Some(Self {
start,
read: UriRead::EMPTY.then(text),
})
}
}
pub(super) fn tag_prefix_at(src: &str, p: usize) -> Option<&str> {
src.get(p..uri_end(src, p, false)).filter(|t| !t.is_empty())
}
pub(super) fn comment_at(src: &str, i: usize) -> bool {
byte(src, i) == Some(b'#')
&& src.get(..i).is_some_and(|before| {
matches!(
before.chars().next_back(),
None | Some(' ' | '\t' | '\n' | '\r' | '\u{feff}')
)
})
}
pub(super) fn lex_trivia(src: &str, i: usize, end: usize) -> Option<(TriviaKind, usize)> {
if i >= end {
return None;
}
let (kind, stop) = match byte(src, i) {
Some(b' ' | b'\t') => (TriviaKind::Whitespace, skip_white(src, i)),
Some(b'\n' | b'\r') => (TriviaKind::LineBreak, i.saturating_add(break_len(src, i))),
Some(b'#') => (TriviaKind::Comment, line_end(src, i)),
_ if src.get(i..).is_some_and(|r| r.starts_with('\u{feff}')) => (
TriviaKind::ByteOrderMark,
i.saturating_add('\u{feff}'.len_utf8()),
),
_ => return None,
};
Some((kind, stop.min(end).max(i.saturating_add(1)).min(end)))
}
pub(super) const IMPLICIT_KEY_MAX_CHARS: usize = 1024;
pub(super) fn key_too_long(src: &str, start: usize, end: usize) -> bool {
src.get(start..end).is_some_and(|key| {
key.len() > IMPLICIT_KEY_MAX_CHARS && key.chars().nth(IMPLICIT_KEY_MAX_CHARS).is_some()
})
}
pub(super) fn valid_handle(h: &str) -> bool {
if h == "!" || h == "!!" {
return true;
}
h.strip_prefix('!')
.and_then(|r| r.strip_suffix('!'))
.is_some_and(|w| !w.is_empty() && w.bytes().all(is_word_char))
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::indexing_slicing)]
use super::*;
#[test]
fn breaks_and_lines() {
assert_eq!(break_len("a\r\nb", 1), 2);
assert_eq!(break_len("a\rb", 1), 1);
assert_eq!(break_len("a\nb", 0), 0);
assert_eq!(line_end("ab\ncd", 0), 2);
assert_eq!(line_end("ab", 1), 2);
assert_eq!(skip_white("a \t b", 1), 4);
}
#[test]
fn plain_lines() {
let s = "a b: c";
assert_eq!(plain_line_end(s, 0, false), 3);
assert_eq!(plain_line_end("a:b #c", 0, false), 3);
assert_eq!(plain_line_end("a#b ", 0, false), 3);
assert_eq!(plain_line_end("a,b]", 0, false), 4);
assert_eq!(plain_line_end("a,b]", 0, true), 1);
assert!(is_plain_first("-a", 0, false));
assert!(!is_plain_first("- a", 0, false));
assert!(!is_plain_first("[a", 0, false));
assert!(!is_plain_first("@a", 0, false));
assert_eq!(plain_line_end_lenient("a: b: c # d", 0), 7);
assert_eq!(plain_line_end_lenient("a:\tb:", 0), 5);
assert_eq!(plain_line_end_lenient("a: [b, c] :\nx", 0), 11);
assert_eq!(plain_line_end_lenient("a: b\u{1}c", 0), 4);
}
#[test]
fn markers_and_indicators() {
assert!(marker_at("--- a", 0, "---"));
assert!(marker_at("...", 0, "..."));
assert!(!marker_at("---a", 0, "---"));
assert!(indicator_at("- a", 0, b'-'));
assert!(indicator_at("-", 0, b'-'));
assert!(!indicator_at("-a", 0, b'-'));
}
#[test]
fn tags() {
let t = scan_tag("!!str x", 0).unwrap();
assert_eq!(
(t.form, t.handle, t.suffix, t.end),
(TagForm::Shorthand, (0, 2), (2, 5), 5)
);
let t = scan_tag("!e!foo", 0).unwrap();
assert_eq!((t.handle, t.suffix), ((0, 3), (3, 6)));
let t = scan_tag("!foo", 0).unwrap();
assert_eq!((t.handle, t.suffix), ((0, 1), (1, 4)));
let t = scan_tag("! a", 0).unwrap();
assert_eq!((t.form, t.end), (TagForm::NonSpecific, 1));
let t = scan_tag("!<tag:a,b> x", 0).unwrap();
assert_eq!((t.form, t.suffix, t.end), (TagForm::Verbatim, (2, 9), 10));
assert!(scan_tag("!<> x", 0).is_none());
assert!(scan_tag("!! x", 0).is_none());
assert!(scan_tag("!e! x", 0).is_none());
assert_eq!(percent_decode_concat("p:", "a%21b").unwrap(), "p:a!b");
assert_eq!(percent_decode_concat("", "%4").unwrap(), "%4");
assert!(percent_decode_concat("", "%ff").is_none());
assert_eq!(percent_decode_concat("p:%21", "a").unwrap(), "p:!a");
assert_eq!(percent_decode_concat("p:%C3", "%A9").unwrap(), "p:é");
assert!(percent_decode_concat("%FF", "x").is_none());
assert!(percent_decode_concat("p:%C3", "x").is_none());
assert_eq!(tag_prefix_at("%TAG !e! a:%21 ", 9), Some("a:%21"));
assert_eq!(tag_prefix_at("x ", 1), None);
let p = TagPrefix::read("%TAG !e! a:%C3 ", 9).unwrap();
assert_eq!(p.read.len, 3);
assert!(!p.read.is_utf8() && p.read.utf8.is_some());
assert!(p.read.then("%A9x").is_utf8());
assert_eq!(p.read.then("%A9x").len, 5);
assert!(!p.read.then("x").is_utf8());
assert!(TagPrefix::read("x", 1).is_none());
assert!(valid_handle("!e!") && !valid_handle("!e") && !valid_handle("!!!"));
}
#[test]
fn utf8_validation_matches_std() {
let valid = |bytes: &[u8]| {
let mut u = Some(Utf8::START);
for b in bytes {
u = u.and_then(|u| u.push(*b));
}
u.is_some_and(Utf8::complete)
};
let edges = [0x00, 0x7F, 0x80, 0x8F, 0x90, 0x9F, 0xA0, 0xBF, 0xC0, 0xFF];
for a in 0..=255u8 {
assert_eq!(valid(&[a]), core::str::from_utf8(&[a]).is_ok(), "{a:x}");
for b in 0..=255u8 {
let two = [a, b];
assert_eq!(valid(&two), core::str::from_utf8(&two).is_ok());
for c in edges {
let three = [a, b, c];
assert_eq!(valid(&three), core::str::from_utf8(&three).is_ok());
for d in edges {
let four = [a, b, c, d];
assert_eq!(valid(&four), core::str::from_utf8(&four).is_ok());
}
}
}
}
}
#[test]
fn anchors() {
assert_eq!(anchor_end("&a: b", 1), 3);
assert_eq!(anchor_end("&a,b", 1), 2);
assert_eq!(anchor_end("&a\u{a0}b c", 1), 5);
assert_eq!(anchor_end("&a\u{feff}", 1), 2);
}
}