Merge pull request #1910 from storytold/refactor/timeline-depth-preflight

timeline: share allocation-free XML and JSON nesting preflight
This commit is contained in:
Dylan M. Taylor
2026-11-16 13:32:41 -05:00
committed by GitHub
3 changed files with 114 additions and 0 deletions
+1
View File
@@ -34,6 +34,7 @@ pub mod model;
#[cfg(feature = "otio")]
pub mod otio;
pub mod path;
pub mod preflight;
pub mod rate;
pub mod report;
pub mod time;
+82
View File
@@ -0,0 +1,82 @@
//! Allocation-free lexical nesting preflight for timeline XML and JSON.
//!
//! Extracted from EffectCraft's interchange reader; original attribution is retained in Git history.
//! These functions preserve its lightweight byte-scanner behavior. They do not validate
//! syntax, expand entities, decode encodings, or replace a bounded format parser. In
//! particular XML self-closing elements do not increase the count, and declarations are
//! skipped lexically rather than parsed as DTDs. Apply document/encoding/entity policy
//! separately; callers choose the maximum acceptable nesting for their parser.
/// The deepest element nesting of an XML document (a quick scan that skips comments, CDATA,
/// processing instructions, declarations and quoted attribute values).
pub fn xml_depth(b: &[u8]) -> usize {
let (mut i, mut depth, mut max) = (0usize, 0usize, 0usize);
let skip_to =
|from: usize, end: &[u8]| b.get(from..).and_then(|r| r.windows(end.len()).position(|w| w == end)).map_or(b.len(), |p| from + p + end.len());
while let Some(&c) = b.get(i) {
if c != b'<' {
i += 1;
continue;
}
let rest = b.get(i..).unwrap_or_default();
if rest.starts_with(b"<!--") {
i = skip_to(i + 4, b"-->");
} else if rest.starts_with(b"<![CDATA[") {
i = skip_to(i + 9, b"]]>");
} else if rest.starts_with(b"<?") {
i = skip_to(i + 2, b"?>");
} else if rest.starts_with(b"<!") {
i = skip_to(i + 2, b">");
} else if rest.starts_with(b"</") {
depth = depth.saturating_sub(1);
i = skip_to(i + 2, b">");
} else {
// A start tag: find its end outside quotes; `/>` closes it at once.
let mut j = i + 1;
let mut quote = None;
while let Some(&c) = b.get(j) {
match quote {
Some(q) if c == q => quote = None,
Some(_) => {}
None if c == b'"' || c == b'\'' => quote = Some(c),
None if c == b'>' => break,
None => {}
}
j += 1;
}
if b.get(j.wrapping_sub(1)) != Some(&b'/') {
depth += 1;
max = max.max(depth);
}
i = j + 1;
}
}
max
}
/// The deepest array / object nesting of a JSON document (strings skipped).
pub fn json_depth(b: &[u8]) -> usize {
let (mut depth, mut max, mut in_str, mut esc) = (0usize, 0usize, false, false);
for &c in b {
if in_str {
if esc {
esc = false;
} else if c == b'\\' {
esc = true;
} else if c == b'"' {
in_str = false;
}
continue;
}
match c {
b'"' => in_str = true,
b'[' | b'{' => {
depth += 1;
max = max.max(depth);
}
b']' | b'}' => depth = depth.saturating_sub(1),
_ => {}
}
}
max
}
+31
View File
@@ -0,0 +1,31 @@
use craft_timeline::preflight::{json_depth, xml_depth};
#[test]
fn xml_skips_markup_inside_quotes_comments_and_cdata() {
let input = br#"<?xml version="1.0"?><a x="<fake><fake>" y='>'><!-- <fake> --><![CDATA[<fake>]]><b><leaf/></b></a>"#;
assert_eq!(xml_depth(input), 2);
assert_eq!(xml_depth(b"<self-closing/>"), 0);
}
#[test]
fn lexical_scans_do_not_claim_to_validate_malformed_documents() {
assert_eq!(xml_depth(b"</a></b><a>"), 1);
assert_eq!(xml_depth(b"<a><!-- unterminated <b>"), 1);
assert_eq!(json_depth(b"]]}{"), 1);
assert_eq!(json_depth(br#"{"unterminated[[["#), 1);
}
#[test]
fn json_ignores_brackets_and_escaped_quotes_in_strings() {
assert_eq!(json_depth(br#"{"x":[{"s":"[\"{]}"}]}"#), 3);
assert_eq!(json_depth(br#""[[[""#), 0);
}
#[test]
fn deep_inputs_are_scanned_iteratively_without_a_tree() {
let depth = 10_000;
let xml = "<x>".repeat(depth) + &"</x>".repeat(depth);
assert_eq!(xml_depth(xml.as_bytes()), depth);
let json = "[".repeat(depth) + &"]".repeat(depth);
assert_eq!(json_depth(json.as_bytes()), depth);
}