Skip to content

Commit

Permalink
Sonar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwd committed Sep 5, 2024
1 parent fc54834 commit ec5e944
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 74 deletions.
102 changes: 48 additions & 54 deletions rapidxml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace rapidxml

class eof_error : public parse_error {
public:
eof_error(const char * what, void * where) : parse_error(what, where) {}
using parse_error::parse_error;
};

class validation_error : public std::runtime_error
Expand All @@ -114,26 +114,22 @@ namespace rapidxml

class xmlns_unbound : public validation_error {
public:
explicit xmlns_unbound(const char * what)
: validation_error(what) {}
using validation_error::validation_error;
};

class duplicate_attribute : public validation_error {
public:
explicit duplicate_attribute(const char * what)
: validation_error(what) {}
using validation_error::validation_error;
};

class attr_xmlns_unbound : public xmlns_unbound {
public:
explicit attr_xmlns_unbound(const char * what)
: xmlns_unbound(what) {}
using xmlns_unbound::xmlns_unbound;
};

class element_xmlns_unbound : public xmlns_unbound {
public:
explicit element_xmlns_unbound(const char * what)
: xmlns_unbound(what) {}
using xmlns_unbound::xmlns_unbound;
};
}

Expand Down Expand Up @@ -168,7 +164,7 @@ namespace rapidxml

//! Enumeration listing all node types produced by the parser.
//! Use xml_node::type() function to query node type.
enum node_type
enum class node_type
{
node_document, //!< A document node. Name and value are empty.
node_element, //!< An element node. Name contains element name. Value contains text of first data node.
Expand Down Expand Up @@ -200,13 +196,6 @@ namespace rapidxml
//! See xml_document::parse() function.
const int parse_no_element_values = 0x2;

//! Parse flag instructing the parser to not place zero terminators after strings in the source text.
//! By default zero terminators are placed, modifying source text.
//! Can be combined with other flags by use of | operator.
//! <br><br>
//! See xml_document::parse() function.
// const int parse_no_string_terminators = 0x4;

//! Parse flag instructing the parser to not translate entities in the source text.
//! By default entities are translated, modifying source text.
//! Can be combined with other flags by use of | operator.
Expand Down Expand Up @@ -383,14 +372,16 @@ namespace rapidxml
public:

//! \cond internal
typedef void *(alloc_func)(std::size_t); // Type of user-defined function used to allocate memory
typedef void (free_func)(void *); // Type of user-defined function used to free memory
using alloc_func = void * (*)(std::size_t); // Type of user-defined function used to allocate memory
using free_func = void (*)(void *); // Type of user-defined function used to free memory
//! \endcond

//! Constructs empty pool with default allocator functions.
memory_pool() : m_static_memory(0) {
memory_pool() {
init();
}
memory_pool(memory_pool const &) = delete;
memory_pool(memory_pool &&) = delete;

//! Destroys pool and frees all the memory.
//! This causes memory occupied by nodes allocated by the pool to be freed.
Expand Down Expand Up @@ -651,9 +642,9 @@ namespace rapidxml
void *m_begin = nullptr; // Start of raw memory making up current pool
void *m_ptr = nullptr; // First free byte in current pool
std::size_t m_space = RAPIDXML_STATIC_POOL_SIZE; // Available space remaining
char m_static_memory[RAPIDXML_STATIC_POOL_SIZE]; // Static raw memory
alloc_func *m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used
free_func *m_free_func = nullptr; // Free function, or 0 if default is to be used
char m_static_memory[RAPIDXML_STATIC_POOL_SIZE] = {}; // Static raw memory
alloc_func m_alloc_func = nullptr; // Allocator function, or 0 if default is to be used
free_func m_free_func = nullptr; // Free function, or 0 if default is to be used
view_type m_nullstr;
view_type m_xmlns_xml;
view_type m_xmlns_xmlns;
Expand Down Expand Up @@ -906,6 +897,7 @@ namespace rapidxml
template<typename Ch = char>
class xml_node: public xml_base<Ch>
{
using enum node_type;

public:
using view_type = std::basic_string_view<Ch>;
Expand Down Expand Up @@ -1297,12 +1289,12 @@ namespace rapidxml
}
else
{
child->m_prev_sibling = 0;
child->m_prev_sibling = nullptr;
m_first_node = child;
}
m_last_node = child;
child->m_parent = this;
child->m_next_sibling = 0;
child->m_next_sibling = nullptr;
return child;
}
optional_ptr<xml_node<Ch>> append_node(optional_ptr<xml_node<Ch>> ptr) {
Expand Down Expand Up @@ -1332,7 +1324,7 @@ namespace rapidxml
dirty();
if (where == m_first_node)
prepend_node(child);
else if (where == 0)
else if (!where)
append_node(child);
else
{
Expand Down Expand Up @@ -1370,10 +1362,10 @@ namespace rapidxml
xml_node<Ch> *child = m_first_node;
m_first_node = child->m_next_sibling;
if (child->m_next_sibling)
child->m_next_sibling->m_prev_sibling = 0;
child->m_next_sibling->m_prev_sibling = nullptr;
else
m_last_node = 0;
child->m_parent = 0;
m_last_node = nullptr;
child->m_parent = nullptr;
}

//! Removes last child of the node.
Expand All @@ -1387,11 +1379,11 @@ namespace rapidxml
if (child->m_prev_sibling)
{
m_last_node = child->m_prev_sibling;
child->m_prev_sibling->m_next_sibling = 0;
child->m_prev_sibling->m_next_sibling = nullptr;
}
else
m_first_node = 0;
child->m_parent = 0;
m_first_node = nullptr;
child->m_parent = nullptr;
}

//! Removes specified child from the node
Expand All @@ -1409,7 +1401,7 @@ namespace rapidxml
{
where->m_prev_sibling->m_next_sibling = where->m_next_sibling;
where->m_next_sibling->m_prev_sibling = where->m_prev_sibling;
where->m_parent = 0;
where->m_parent = nullptr;
}
}

Expand All @@ -1418,8 +1410,9 @@ namespace rapidxml
{
if (!m_first_node) return;
dirty();
for (xml_node<Ch> *node = m_first_node; node; node = node->m_next_sibling)
for (xml_node<Ch> *node = m_first_node; node; node = node->m_next_sibling) {
node->m_parent = nullptr;
}
m_first_node = nullptr;
m_last_node = nullptr;
}
Expand All @@ -1437,12 +1430,12 @@ namespace rapidxml
}
else
{
attribute->m_next_attribute = 0;
attribute->m_next_attribute = nullptr;
m_last_attribute = attribute;
}
m_first_attribute = attribute;
attribute->m_parent = this;
attribute->m_prev_attribute = 0;
attribute->m_prev_attribute = nullptr;
}

//! Appends a new attribute to the node.
Expand All @@ -1458,12 +1451,12 @@ namespace rapidxml
}
else
{
attribute->m_prev_attribute = 0;
attribute->m_prev_attribute = nullptr;
m_first_attribute = attribute;
}
m_last_attribute = attribute;
attribute->m_parent = this;
attribute->m_next_attribute = 0;
attribute->m_next_attribute = nullptr;
}

//! Inserts a new attribute at specified place inside the node.
Expand All @@ -1477,7 +1470,7 @@ namespace rapidxml
dirty_parent();
if (where == m_first_attribute)
prepend_attribute(attribute);
else if (where == 0)
else if (!where)
append_attribute(attribute);
else
{
Expand All @@ -1502,8 +1495,8 @@ namespace rapidxml
attribute->m_next_attribute->m_prev_attribute = 0;
}
else
m_last_attribute = 0;
attribute->m_parent = 0;
m_last_attribute = nullptr;
attribute->m_parent = nullptr;
m_first_attribute = attribute->m_next_attribute;
}

Expand All @@ -1521,8 +1514,8 @@ namespace rapidxml
m_last_attribute = attribute->m_prev_attribute;
}
else
m_first_attribute = 0;
attribute->m_parent = 0;
m_first_attribute = nullptr;
attribute->m_parent = nullptr;
}

//! Removes specified attribute from node.
Expand All @@ -1539,7 +1532,7 @@ namespace rapidxml
{
where->m_prev_attribute->m_next_attribute = where->m_next_attribute;
where->m_next_attribute->m_prev_attribute = where->m_prev_attribute;
where->m_parent = 0;
where->m_parent = nullptr;
}
}

Expand All @@ -1548,8 +1541,9 @@ namespace rapidxml
{
if (!m_first_attribute) return;
dirty_parent();
for (xml_attribute<Ch> *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute)
for (xml_attribute<Ch> *attribute = m_first_attribute; attribute; attribute = attribute->m_next_attribute) {
attribute->m_parent = nullptr;
}
m_first_attribute = nullptr;
}

Expand Down Expand Up @@ -1626,6 +1620,7 @@ namespace rapidxml
template<class Ch = char>
class xml_document: public xml_node<Ch>, public memory_pool<Ch>
{
using enum node_type;

public:
using view_type = std::basic_string_view<Ch>;
Expand Down Expand Up @@ -1677,7 +1672,7 @@ namespace rapidxml
parse_bom<Flags>(text);

// Parse children
while (1)
while (true)
{
// Skip whitespace before node
skip<whitespace_pred, Flags>(text);
Expand All @@ -1690,9 +1685,8 @@ namespace rapidxml
++text; // Skip '<'
if (xml_node<Ch> *node = parse_node<Flags>(text)) {
this->append_node(node);
if (Flags & (parse_open_only|parse_parse_one)) {
if (node->type() == node_element)
break;
if (Flags & (parse_open_only|parse_parse_one) && node->type() == node_element) {
break;
}
}
}
Expand All @@ -1714,18 +1708,18 @@ namespace rapidxml

template<int Flags>
view_type decode_data_value_low(view_type const & v) {
Ch * init = const_cast<Ch *>(v.data());
Ch * first = init;
auto * init = const_cast<Ch *>(v.data());
auto * first = init;
if (Flags & parse_normalize_whitespace) {
skip<text_pure_with_ws_pred,0>(first);
} else {
skip<text_pure_no_ws_pred,0>(first);
}
if (*first == '<') return v;
auto buf = this->allocate_string(v);
Ch * start = const_cast<Ch *>(buf.data());
Ch * tmp = start;
Ch * end = (Flags & parse_normalize_whitespace) ?
auto * start = const_cast<Ch *>(buf.data());
auto * tmp = start;
auto * end = (Flags & parse_normalize_whitespace) ?
skip_and_expand_character_refs<text_pred,text_pure_with_ws_pred,Flags>(tmp) :
skip_and_expand_character_refs<text_pred,text_pure_no_ws_pred,Flags>(tmp);
// Trim trailing whitespace if flag is set; leading was already trimmed by whitespace skip after >
Expand Down
12 changes: 6 additions & 6 deletions rapidxml_predicates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace rapidxml {

bool do_match(const xml_node<Ch> & t) override {
if (m_xmlns.has_value() && t.xmlns() != m_xmlns.value()) return false;
return (t.type() == node_element) && (t.name() == m_name || m_name == "*");
return (t.type() == node_type::node_element) && (t.name() == m_name || m_name == "*");
}
};

Expand All @@ -43,7 +43,7 @@ namespace rapidxml {
: xpath_base<Ch>(), m_value(v) {}

bool do_match(const xml_node<Ch> & t) override {
return (t.type() == node_element) && (t.value() == m_value);
return (t.type() == node_type::node_element) && (t.value() == m_value);
}
};

Expand All @@ -56,7 +56,7 @@ namespace rapidxml {
: xpath_base<Ch>(), m_xmlns(v) {}

bool do_match(const xml_node<Ch> & t) override {
return (t.type() == node_element) && (t.xmlns() == m_xmlns);
return (t.type() == node_type::node_element) && (t.xmlns() == m_xmlns);
}
};

Expand All @@ -74,7 +74,7 @@ namespace rapidxml {
: xpath_base<Ch>(), m_name(n), m_value(v), m_xmlns(x) {}

bool do_match(const xml_node<Ch> & t) override {
if (t.type() != node_element) return false;
if (t.type() != node_type::node_element) return false;
for (auto const & attr : t.attributes()) {
if (m_xmlns.has_value()) {
if (m_name == "*" || attr.local_name() != m_name) continue;
Expand All @@ -100,7 +100,7 @@ namespace rapidxml {
}

bool do_match(const xml_node<Ch> & t) override {
return t.type() == node_document || t.type() == node_element;
return t.type() == node_type::node_document || t.type() == node_type::node_element;
}
};

Expand All @@ -117,7 +117,7 @@ namespace rapidxml {
}

bool do_match(const xml_node<Ch> & t) override {
return t.type() == node_document || t.type() == node_element;
return t.type() == node_type::node_document || t.type() == node_type::node_element;
}
};

Expand Down
Loading

0 comments on commit ec5e944

Please sign in to comment.