mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-18 18:15:34 +00:00
Merge main into onee/fix-annot-tests
This commit is contained in:
commit
64c10d450c
2 files changed
+19
-3
No files matched your search
@@ -517,6 +517,12 @@ const MAX_URL_CHARS: usize = 2048;
|
||||
/// it would open with a scheme the text doesn't say. Brackets inside a web address belong to it
|
||||
/// when they pair up (`…/wiki/Mercury_(planet)`); an unpaired one ends it.
|
||||
pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range<usize>, String)> {
|
||||
find_urls_with_scan_observer(chars, |_| {})
|
||||
}
|
||||
|
||||
/// Observe each candidate scan offset before reading it. The production caller is a no-op;
|
||||
/// regression tests assert the work bound directly, independent of machine speed.
|
||||
pub(crate) fn find_urls_with_scan_observer(chars: &[char], mut observe: impl FnMut(usize)) -> Vec<(std::ops::Range<usize>, String)> {
|
||||
let url_char = |c: char| !c.is_whitespace() && !"<>\"'()[]{}".contains(c);
|
||||
// Characters found inside an address but not between words: a `www.` after one is part of
|
||||
// a longer address, and so is an e-mail address after a slash.
|
||||
@@ -542,6 +548,7 @@ pub fn find_urls(chars: &[char]) -> Vec<(std::ops::Range<usize>, String)> {
|
||||
too_long = true;
|
||||
break;
|
||||
}
|
||||
observe(j.saturating_sub(i));
|
||||
match chars.get(j) {
|
||||
Some('(') => {
|
||||
if depth == 0 {
|
||||
|
||||
@@ -1459,9 +1459,18 @@ fn urls_keep_their_written_scheme_and_paired_brackets() {
|
||||
fn unclosed_brackets_in_text_without_spaces_are_read_once_per_address() {
|
||||
let found = |s: &str| -> Vec<String> { crate::links::find_urls(&s.chars().collect::<Vec<_>>()).into_iter().map(|(_, u)| u).collect() };
|
||||
let text = format!("www.before.org {} www.after.org", "https://a/(".repeat(60_000));
|
||||
let started = std::time::Instant::now();
|
||||
assert_eq!(found(&text), ["http://www.before.org", "http://www.after.org"]);
|
||||
assert!(started.elapsed().as_secs() < 5, "{:?}", started.elapsed());
|
||||
let chars: Vec<char> = text.chars().collect();
|
||||
let mut reads = 0usize;
|
||||
let mut furthest = 0usize;
|
||||
let urls = crate::links::find_urls_with_scan_observer(&chars, |offset| {
|
||||
// Fail on the first over-budget read, rather than waiting for a quadratic scan.
|
||||
assert!(offset < 2048, "candidate read beyond its 2048-character bound: {offset}");
|
||||
reads += 1;
|
||||
furthest = furthest.max(offset);
|
||||
});
|
||||
assert_eq!(urls.into_iter().map(|(_, u)| u).collect::<Vec<_>>(), ["http://www.before.org", "http://www.after.org"]);
|
||||
assert_eq!(furthest, 2047, "the hostile fixture must exercise the scan boundary");
|
||||
assert!(reads <= (60_000 + 2) * 2048, "aggregate candidate work: {reads}");
|
||||
let long = format!("https://example.com/{} www.after.org", "a".repeat(3000));
|
||||
assert_eq!(found(&long), ["http://www.after.org"]);
|
||||
let fits = format!("https://example.com/{}", "a".repeat(2000));
|
||||
|
||||
Reference in new issue
Block a user