Skip to content

Commit

Permalink
Drop attrs_start_idx and use drain when resolving attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed Nov 21, 2022
1 parent 1dcbdfa commit e5fa00e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -355,7 +355,7 @@ impl NodeId {
/// Returns the `u32` representation of the `NodeId`.
#[inline]
pub fn get(self) -> u32 {
self.0.get() as u32 - 1
self.0.get() - 1
}

/// Returns the `usize` representation of the `NodeId`.
Expand Down Expand Up @@ -877,7 +877,7 @@ impl<'a, 'input: 'a> Node<'a, 'input> {
/// ```
pub fn lookup_prefix(&self, uri: &str) -> Option<&'a str> {
if uri == NS_XML_URI {
return Some("xml");
return Some(NS_XML_PREFIX);
}

self.namespaces()
Expand Down
12 changes: 4 additions & 8 deletions src/parse.rs
Expand Up @@ -346,7 +346,6 @@ struct Entity<'input> {

struct ParserData<'input> {
opt: ParsingOptions,
attrs_start_idx: usize,
ns_start_idx: usize,
tmp_attrs: Vec<TempAttributeData<'input>>,
awaiting_subtree: Vec<NodeId>,
Expand Down Expand Up @@ -458,7 +457,6 @@ impl LoopDetector {
fn parse(text: &str, opt: ParsingOptions) -> Result<Document, Error> {
let mut pd = ParserData {
opt,
attrs_start_idx: 0,
ns_start_idx: 1,
tmp_attrs: Vec::with_capacity(16),
entities: Vec::new(),
Expand Down Expand Up @@ -730,8 +728,6 @@ fn process_element<'input>(
pd.ns_start_idx = doc.namespaces.len();

let attributes = resolve_attributes(pd, namespaces, doc)?;
pd.attrs_start_idx = doc.attrs.len();
pd.tmp_attrs.clear();

match end_token {
xmlparser::ElementEnd::Empty => {
Expand Down Expand Up @@ -826,7 +822,6 @@ fn resolve_attributes<'input>(
namespaces: ShortRange,
doc: &mut Document<'input>,
) -> Result<ShortRange, Error> {
let start_idx = pd.attrs_start_idx;
if pd.tmp_attrs.is_empty() {
return Ok(ShortRange::new(0, 0));
}
Expand All @@ -835,7 +830,9 @@ fn resolve_attributes<'input>(
return Err(Error::NodesLimitReached);
}

for attr in &mut pd.tmp_attrs {
let start_idx = doc.attrs.len();

for attr in pd.tmp_attrs.drain(..) {
let namespace_idx = if attr.prefix.as_str() == NS_XML_PREFIX {
// The prefix 'xml' is by definition bound to the namespace name
// http://www.w3.org/XML/1998/namespace. This namespace is added
Expand Down Expand Up @@ -865,8 +862,7 @@ fn resolve_attributes<'input>(

doc.attrs.push(AttributeData {
name: attr_name,
// Takes a value from a slice without consuming the slice.
value: core::mem::replace(&mut attr.value, Cow::Borrowed("")),
value: attr.value,
#[cfg(feature = "positions")]
pos: attr.pos,
});
Expand Down

0 comments on commit e5fa00e

Please sign in to comment.