Merge pull request #260 from Dmitry-Me/placeChecksInMoreNaturalOrder
Rearrange checks in more natural order
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 4bb4d1f..5633397 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -186,6 +186,8 @@
const char* StrPair::GetStr()
{
+ TIXMLASSERT( _start );
+ TIXMLASSERT( _end );
if ( _flags & NEEDS_FLUSH ) {
*_end = 0;
_flags ^= NEEDS_FLUSH;
@@ -267,6 +269,7 @@
}
_flags = (_flags & NEEDS_DELETE);
}
+ TIXMLASSERT( _start );
return _start;
}
@@ -672,11 +675,7 @@
TIXMLASSERT( false );
return 0;
}
-
- if (addThis->_parent)
- addThis->_parent->Unlink( addThis );
- else
- addThis->_memPool->SetTracked();
+ BeforeInsertChild( addThis );
if ( _lastChild ) {
TIXMLASSERT( _firstChild );
@@ -706,11 +705,7 @@
TIXMLASSERT( false );
return 0;
}
-
- if (addThis->_parent)
- addThis->_parent->Unlink( addThis );
- else
- addThis->_memPool->SetTracked();
+ BeforeInsertChild( addThis );
if ( _firstChild ) {
TIXMLASSERT( _lastChild );
@@ -753,10 +748,7 @@
// The last node or the only node.
return InsertEndChild( addThis );
}
- if (addThis->_parent)
- addThis->_parent->Unlink( addThis );
- else
- addThis->_memPool->SetTracked();
+ BeforeInsertChild( addThis );
addThis->_prev = afterThis;
addThis->_next = afterThis->_next;
afterThis->_next->_prev = addThis;
@@ -860,19 +852,19 @@
}
XMLElement* ele = node->ToElement();
- // We read the end tag. Return it to the parent.
- if ( ele && ele->ClosingType() == XMLElement::CLOSING ) {
- if ( parentEnd ) {
- ele->_value.TransferTo( parentEnd );
- }
- node->_memPool->SetTracked(); // created and then immediately deleted.
- DeleteNode( node );
- return p;
- }
-
- // Handle an end tag returned to this level.
- // And handle a bunch of annoying errors.
if ( ele ) {
+ // We read the end tag. Return it to the parent.
+ if ( ele->ClosingType() == XMLElement::CLOSING ) {
+ if ( parentEnd ) {
+ ele->_value.TransferTo( parentEnd );
+ }
+ node->_memPool->SetTracked(); // created and then immediately deleted.
+ DeleteNode( node );
+ return p;
+ }
+
+ // Handle an end tag returned to this level.
+ // And handle a bunch of annoying errors.
bool mismatch = false;
if ( endTag.Empty() && ele->ClosingType() == XMLElement::OPEN ) {
mismatch = true;
@@ -906,6 +898,17 @@
pool->Free( node );
}
+void XMLNode::BeforeInsertChild( XMLNode* insertThis ) const
+{
+ TIXMLASSERT( insertThis );
+ TIXMLASSERT( insertThis->_document == _document );
+
+ if ( insertThis->_parent )
+ insertThis->_parent->Unlink( insertThis );
+ else
+ insertThis->_memPool->SetTracked();
+}
+
// --------- XMLText ---------- //
char* XMLText::ParseDeep( char* p, StrPair* )
{
@@ -1819,15 +1822,7 @@
_charBuffer[size] = 0;
- const char* p = _charBuffer;
- p = XMLUtil::SkipWhiteSpace( p );
- p = XMLUtil::ReadBOM( p, &_writeBOM );
- if ( !*p ) {
- SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
- return _errorID;
- }
-
- ParseDeep( _charBuffer + (p-_charBuffer), 0 );
+ Parse();
return _errorID;
}
@@ -1868,16 +1863,7 @@
memcpy( _charBuffer, p, len );
_charBuffer[len] = 0;
- const char* start = p;
- p = XMLUtil::SkipWhiteSpace( p );
- p = XMLUtil::ReadBOM( p, &_writeBOM );
- if ( !*p ) {
- SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
- return _errorID;
- }
-
- ptrdiff_t delta = p - start; // skip initial whitespace, BOM, etc.
- ParseDeep( _charBuffer+delta, 0 );
+ Parse();
if ( Error() ) {
// clean up now essentially dangling memory.
// and the parse fail can put objects in the
@@ -1935,6 +1921,19 @@
}
}
+void XMLDocument::Parse()
+{
+ TIXMLASSERT( NoChildren() ); // Clear() must have been called previously
+ TIXMLASSERT( _charBuffer );
+ const char* p = _charBuffer;
+ p = XMLUtil::SkipWhiteSpace( p );
+ p = XMLUtil::ReadBOM( p, &_writeBOM );
+ if ( !*p ) {
+ SetError( XML_ERROR_EMPTY_DOCUMENT, 0, 0 );
+ return;
+ }
+ ParseDeep( _charBuffer + (p-_charBuffer), 0 );
+}
XMLPrinter::XMLPrinter( FILE* file, bool compact, int depth ) :
_elementJustOpened( false ),
diff --git a/tinyxml2.h b/tinyxml2.h
index 530160f..9f130fb 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -552,9 +552,14 @@
}
inline static bool IsNameStartChar( unsigned char ch ) {
- return ( ( ch < 128 ) ? isalpha( ch ) : 1 )
- || ch == ':'
- || ch == '_';
+ if ( ch >= 128 ) {
+ // This is a heuristic guess in attempt to not implement Unicode-aware isalpha()
+ return true;
+ }
+ if ( isalpha( ch ) ) {
+ return true;
+ }
+ return ch == ':' || ch == '_';
}
inline static bool IsNameChar( unsigned char ch ) {
@@ -894,6 +899,7 @@
MemPool* _memPool;
void Unlink( XMLNode* child );
static void DeleteNode( XMLNode* node );
+ void BeforeInsertChild( XMLNode* insertThis ) const;
};
@@ -1730,6 +1736,8 @@
MemPoolT< sizeof(XMLComment) > _commentPool;
static const char* _errorNames[XML_ERROR_COUNT];
+
+ void Parse();
};