Skip to content

Commit

Permalink
Cleanup first_element_by_path
Browse files Browse the repository at this point in the history
Instead of performing a late null check that is redundant and only
needed to silence clang static analysis warning, we pick the context as
a root / self node. This way the code is a bit less redundant and the
static analyzer is happy.
  • Loading branch information
zeux committed Dec 4, 2019
1 parent 2e8631d commit 53a30c6
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/pugixml.cpp
Expand Up @@ -6233,16 +6233,9 @@ namespace pugi

PUGI__FN xml_node xml_node::first_element_by_path(const char_t* path_, char_t delimiter) const
{
xml_node found = *this; // Current search context.
xml_node context = path_[0] == delimiter ? root() : *this;

if (!_root || !path_[0]) return found;

if (path_[0] == delimiter)
{
// Absolute path; e.g. '/foo/bar'
found = found.root();
++path_;
}
if (!context._root) return xml_node();

const char_t* path_segment = path_;

Expand All @@ -6252,29 +6245,27 @@ namespace pugi

while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end;

if (path_segment == path_segment_end) return found;
if (path_segment == path_segment_end) return context;

const char_t* next_segment = path_segment_end;

while (*next_segment == delimiter) ++next_segment;

if (*path_segment == '.' && path_segment + 1 == path_segment_end)
return found.first_element_by_path(next_segment, delimiter);
return context.first_element_by_path(next_segment, delimiter);
else if (*path_segment == '.' && *(path_segment+1) == '.' && path_segment + 2 == path_segment_end)
return found.parent().first_element_by_path(next_segment, delimiter);
return context.parent().first_element_by_path(next_segment, delimiter);
else
{
if (found._root) {
for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling)
{
if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
{
xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);

if (subsearch) return subsearch;
}
}
}
for (xml_node_struct* j = context._root->first_child; j; j = j->next_sibling)
{
if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
{
xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);

if (subsearch) return subsearch;
}
}

return xml_node();
}
Expand Down

0 comments on commit 53a30c6

Please sign in to comment.