use super::run;
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> {
match src.as_bytes().get(i) {
Some(b) if b.is_ascii() => Some(char::from(*b)),
_ => 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 {
run::find_break(src.as_bytes(), i)
}
pub(super) fn comment_extent(src: &str, i: usize) -> (usize, bool) {
run::line_end_checked(src.as_bytes(), i)
}
pub(super) fn comment_end(src: &str, i: usize) -> usize {
run::find_break(src.as_bytes(), i)
}
pub(super) fn skip_white(src: &str, i: usize) -> usize {
let bytes = src.as_bytes();
let mut j = i;
while bytes.get(j).is_some_and(|b| is_white(*b)) {
j = j.saturating_add(1);
}
j
}
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 {
byte(src, i) == Some(0xEF) && 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 {
if c.is_ascii() {
return matches!(c, '\x21'..='\x7e');
}
is_printable(c) && c != '\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 {
matches!(byte(src, i), Some(b'-' | b'.'))
&& (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 bytes = src.as_bytes();
let mut i = start;
while i < src.len() {
i = run::plain_stop(bytes, i, flow);
let Some(c) = char_at(src, i) else { break };
match c {
' ' | '\t' => {
i = i.saturating_add(1);
continue;
}
':' if !colons && !plain_safe_at(src, i.saturating_add(1), flow) => break,
'#' if i > start && 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());
}
let mut end = i;
while end > start
&& bytes
.get(end.saturating_sub(1))
.is_some_and(|b| is_white(*b))
{
end = end.saturating_sub(1);
}
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,
}
fn verbatim_fault(src: &str, from: usize, to: usize) -> Option<usize> {
if byte(src, from) == Some(b'!') {
return (to == from.saturating_add(1)).then_some(to);
}
if !byte(src, from).is_some_and(|b| b.is_ascii_alphabetic()) {
return Some(from);
}
let mut j = from.saturating_add(1);
while j < to
&& byte(src, j)
.is_some_and(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'-' | b'.'))
{
j = j.saturating_add(1);
}
(byte(src, j) != Some(b':') || j >= to).then_some(j)
}
fn after_partial_escape(src: &str, at: usize) -> usize {
if byte(src, at) != Some(b'%') {
return at;
}
let hex = |k: usize| byte(src, at.saturating_add(k)).is_some_and(|h| h.is_ascii_hexdigit());
at.saturating_add(if hex(1) { 2 } else { 1 })
}
pub(super) fn tag_blame(src: &str, start: usize, scanned: Option<TagScan>) -> usize {
if let Some(t) = scanned {
return after_partial_escape(src, t.end);
}
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);
if to > from && byte(src, to) == Some(b'>') {
verbatim_fault(src, from, to).unwrap_or(to)
} else {
after_partial_escape(src, to)
}
}
Some(b'!') => {
let from = after.saturating_add(1);
after_partial_escape(src, uri_end(src, from, true))
}
_ => {
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 from = if words > 0 && byte(src, word_end) == Some(b'!') {
word_end.saturating_add(1)
} else {
after
};
after_partial_escape(src, uri_end(src, from, true))
}
}
}
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'>') && verbatim_fault(src, from, to).is_none())
.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) -> String {
let mut out = Vec::with_capacity(prefix.len().saturating_add(suffix.len()));
out.extend(decoded(prefix));
out.extend(decoded(suffix));
String::from_utf8_lossy(&out).into_owned()
}
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)) if v.is_ascii() && v != b'%' => {
i = i.saturating_add(3);
Some(v)
}
_ => {
i = i.saturating_add(1);
Some(b)
}
}
})
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct UriRead {
pub(super) len: usize,
}
impl UriRead {
pub(super) const EMPTY: Self = Self { len: 0 };
pub(super) fn then(self, s: &str) -> Self {
Self {
len: self.len.saturating_add(decoded(s).count()),
}
}
}
#[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 {
if byte(src, i) != Some(b'#') {
return false;
}
match i.checked_sub(1).and_then(|p| byte(src, p)) {
None | Some(b' ' | b'\t' | b'\n' | b'\r') => true,
Some(b) if b.is_ascii() => false,
Some(_) => src
.get(..i)
.is_some_and(|before| before.ends_with('\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 mark_at(src, i) => (
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 {
end.saturating_sub(start) > IMPLICIT_KEY_MAX_CHARS
&& src
.get(start..end)
.is_some_and(|key| 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"), "p:a!b");
assert_eq!(percent_decode_concat("", "%4"), "%4");
assert_eq!(percent_decode_concat("", "%ff"), "%ff");
assert_eq!(percent_decode_concat("p:%21", "a"), "p:!a");
assert_eq!(percent_decode_concat("p:%C3", "%A9"), "p:%C3%A9");
assert_eq!(percent_decode_concat("", "a%25C3%25A9"), "a%25C3%25A9");
assert_ne!(
percent_decode_concat("", "a%25C3%25A9"),
percent_decode_concat("", "a%C3%A9")
);
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, 5);
assert_eq!(p.read.then("%A9x").len, 9);
assert!(TagPrefix::read("x", 1).is_none());
assert!(valid_handle("!e!") && !valid_handle("!e") && !valid_handle("!!!"));
}
#[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);
}
#[test]
fn word_scans_agree_with_the_plain_ones() {
for len in 0..40 {
for brk in 0..=len {
for sep in ["\n", "\r", "\r\n"] {
let mut s = "x".repeat(brk);
s.push_str(sep);
s.push_str(&"y".repeat(len - brk));
for i in 0..=s.len() {
assert_eq!(comment_end(&s, i), line_end(&s, i), "{s:?} {i}");
}
}
}
let s = format!("{}\u{e9}\u{1F600}", "z".repeat(len));
for i in (0..=s.len()).filter(|i| s.is_char_boundary(*i)) {
assert_eq!(comment_end(&s, i), line_end(&s, i), "{s:?} {i}");
}
assert_eq!(comment_end(&s, s.len() + 1), line_end(&s, s.len() + 1));
}
}
}