Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::str::FromStr;
use std::{fmt, fs};

use indexmap::IndexMap;
use regex::Regex;
use rustc_ast::join_path_syms;
use rustc_data_structures::flock;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
Expand Down Expand Up @@ -376,12 +375,15 @@ fn hack_get_external_crate_names(
};
// this is only run once so it's fine not to cache it
// !dot_matches_new_line: all crates on same line. greedy: match last bracket
let regex = Regex::new(r"\[.*\]").unwrap();
let Some(content) = regex.find(&content) else {
return Err(Error::new("could not find crates list in crates.js", path));
};
let content: Vec<String> = try_err!(serde_json::from_str(content.as_str()), &path);
Ok(content)
if let Some(start) = content.find('[')
&& let Some(end) = content[start..].find(']')
{
let content: Vec<String> =
try_err!(serde_json::from_str(&content[start..=start + end]), &path);
Ok(content)
} else {
Err(Error::new("could not find crates list in crates.js", path))
}
}

#[derive(Serialize, Deserialize, Clone, Default, Debug)]
Expand Down Expand Up @@ -504,33 +506,35 @@ impl Hierarchy {

fn add_path(self: &Rc<Self>, path: &Path) {
let mut h = Rc::clone(self);
let mut elems = path
let mut components = path
.components()
.filter_map(|s| match s {
Component::Normal(s) => Some(s.to_owned()),
Component::ParentDir => Some(OsString::from("..")),
_ => None,
})
.filter(|component| matches!(component, Component::Normal(_) | Component::ParentDir))
.peekable();
loop {
let cur_elem = elems.next().expect("empty file path");
if cur_elem == ".." {
if let Some(parent) = h.parent.upgrade() {

assert!(components.peek().is_some(), "empty file path");
while let Some(component) = components.next() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed there's a very small change in behavior here.
previous version would panic if path.components() doesn't yield any items.
not sure if it's worth preserving? (you could add a assert!(components.peek().is_some()) before the loop)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Yeah let's go with an assert.

match component {
Component::Normal(s) => {
if components.peek().is_none() {
h.elems.borrow_mut().insert(s.to_owned());
break;
}
h = {
let mut children = h.children.borrow_mut();

if let Some(existing) = children.get(s) {
Rc::clone(existing)
} else {
let new_node = Rc::new(Self::with_parent(s.to_owned(), &h));
children.insert(s.to_owned(), Rc::clone(&new_node));
new_node
}
};
}
Component::ParentDir if let Some(parent) = h.parent.upgrade() => {
h = parent;
}
continue;
}
if elems.peek().is_none() {
h.elems.borrow_mut().insert(cur_elem);
break;
} else {
let entry = Rc::clone(
h.children
.borrow_mut()
.entry(cur_elem.clone())
.or_insert_with(|| Rc::new(Self::with_parent(cur_elem, &h))),
);
h = entry;
_ => {}
}
}
}
Expand Down
Loading