From ecb9b07476c1b03849c1c2cafeb1597cfdc15ef2 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Wed, 12 Oct 2016 16:44:59 +0300 Subject: [PATCH] Reuse "is element with name" check --- tinyxml2.cpp | 37 +++++++++++++++++++++++-------------- tinyxml2.h | 1 + 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 7df8443..e82482e 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -897,11 +897,9 @@ XMLNode* XMLNode::InsertAfterChild( XMLNode* afterThis, XMLNode* addThis ) const XMLElement* XMLNode::FirstChildElement( const char* name ) const { for( const XMLNode* node = _firstChild; node; node = node->_next ) { - const XMLElement* element = node->ToElement(); + const XMLElement* element = node->ToElementWithName( name ); if ( element ) { - if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) { - return element; - } + return element; } } return 0; @@ -911,11 +909,9 @@ const XMLElement* XMLNode::FirstChildElement( const char* name ) const const XMLElement* XMLNode::LastChildElement( const char* name ) const { for( const XMLNode* node = _lastChild; node; node = node->_prev ) { - const XMLElement* element = node->ToElement(); + const XMLElement* element = node->ToElementWithName( name ); if ( element ) { - if ( !name || XMLUtil::StringEqual( element->Name(), name ) ) { - return element; - } + return element; } } return 0; @@ -925,9 +921,8 @@ const XMLElement* XMLNode::LastChildElement( const char* name ) const const XMLElement* XMLNode::NextSiblingElement( const char* name ) const { for( const XMLNode* node = _next; node; node = node->_next ) { - const XMLElement* element = node->ToElement(); - if ( element - && (!name || XMLUtil::StringEqual( name, element->Name() ))) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { return element; } } @@ -938,9 +933,8 @@ const XMLElement* XMLNode::NextSiblingElement( const char* name ) const const XMLElement* XMLNode::PreviousSiblingElement( const char* name ) const { for( const XMLNode* node = _prev; node; node = node->_prev ) { - const XMLElement* element = node->ToElement(); - if ( element - && (!name || XMLUtil::StringEqual( name, element->Name() ))) { + const XMLElement* element = node->ToElementWithName( name ); + if ( element ) { return element; } } @@ -1056,6 +1050,21 @@ void XMLNode::InsertChildPreamble( XMLNode* insertThis ) const insertThis->_memPool->SetTracked(); } +const XMLElement* XMLNode::ToElementWithName( const char* name ) const +{ + const XMLElement* element = this->ToElement(); + if ( element == 0 ) { + return 0; + } + if ( name == 0 ) { + return element; + } + if ( XMLUtil::StringEqual( element->Name(), name ) ) { + return element; + } + return 0; +} + // --------- XMLText ---------- // char* XMLText::ParseDeep( char* p, StrPair* ) { diff --git a/tinyxml2.h b/tinyxml2.h index fbfe4f6..986431b 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -908,6 +908,7 @@ private: void Unlink( XMLNode* child ); static void DeleteNode( XMLNode* node ); void InsertChildPreamble( XMLNode* insertThis ) const; + const XMLElement* ToElementWithName( const char* name ) const; XMLNode( const XMLNode& ); // not supported XMLNode& operator=( const XMLNode& ); // not supported