From e90e9010412d8758f2125f7322357c00c142c928 Mon Sep 17 00:00:00 2001 From: Lee Thomason Date: Sat, 24 Dec 2016 07:34:39 -0800 Subject: [PATCH] tweaks, clarification to line numbers --- tinyxml2.cpp | 10 ++++---- tinyxml2.h | 23 +++++++++-------- xmltest.cpp | 72 +++++++++++++++++++++++++++------------------------- 3 files changed, 55 insertions(+), 50 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index ea894de..18d6912 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -193,6 +193,7 @@ char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLin { TIXMLASSERT( p ); TIXMLASSERT( endTag && *endTag ); + TIXMLASSERT(curLineNumPtr); char* start = p; char endChar = *endTag; @@ -238,8 +239,7 @@ void StrPair::CollapseWhitespace() // Adjusting _start would cause undefined behavior on delete[] TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 ); // Trim leading space. - int unusedLineNum = 0; - _start = XMLUtil::SkipWhiteSpace( _start, &unusedLineNum ); + _start = XMLUtil::SkipWhiteSpace( _start, 0 ); if ( *_start ) { const char* p = _start; // the read pointer @@ -247,7 +247,7 @@ void StrPair::CollapseWhitespace() while( *p ) { if ( XMLUtil::IsWhiteSpace( *p )) { - p = XMLUtil::SkipWhiteSpace( p, &unusedLineNum ); + p = XMLUtil::SkipWhiteSpace( p, 0 ); if ( *p == 0 ) { break; // don't write to q; this trims the trailing space. } @@ -2247,7 +2247,7 @@ void XMLDocument::SetError( XMLError error, const char* str1, const char* str2, _errorStr2.SetStr(str2); } -const char* XMLDocument::ErrorName(XMLError errorID) +/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID) { TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT ); const char* errorName = _errorNames[errorID]; @@ -2257,7 +2257,7 @@ const char* XMLDocument::ErrorName(XMLError errorID) const char* XMLDocument::ErrorName() const { - return ErrorName(_errorID); + return ErrorIDToName(_errorID); } void XMLDocument::PrintError() const diff --git a/tinyxml2.h b/tinyxml2.h index a72699c..de589bd 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -532,8 +532,9 @@ class XMLUtil public: static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr ) { TIXMLASSERT( p ); + while( IsWhiteSpace(*p) ) { - if (*p == '\n') { + if (curLineNumPtr && *p == '\n') { ++(*curLineNumPtr); } ++p; @@ -1765,7 +1766,7 @@ public: return _errorID; } const char* ErrorName() const; - static const char* ErrorName(XMLError errorID); + static const char* ErrorIDToName(XMLError errorID); /// Return a possibly helpful diagnostic location or string. const char* GetErrorStr1() const { @@ -1800,15 +1801,15 @@ private: XMLDocument( const XMLDocument& ); // not supported void operator=( const XMLDocument& ); // not supported - bool _writeBOM; - bool _processEntities; - XMLError _errorID; - Whitespace _whitespace; - mutable StrPair _errorStr1; - mutable StrPair _errorStr2; - int _errorLineNum; - char* _charBuffer; - int _parseCurLineNum; + bool _writeBOM; + bool _processEntities; + XMLError _errorID; + Whitespace _whitespace; + mutable StrPair _errorStr1; + mutable StrPair _errorStr2; + int _errorLineNum; + char* _charBuffer; + int _parseCurLineNum; MemPoolT< sizeof(XMLElement) > _elementPool; MemPoolT< sizeof(XMLAttribute) > _attributePool; diff --git a/xmltest.cpp b/xmltest.cpp index f598113..b6bb76f 100644 --- a/xmltest.cpp +++ b/xmltest.cpp @@ -65,7 +65,7 @@ bool XMLTest (const char* testString, const char* expected, const char* found, b bool XMLTest(const char* testString, XMLError expected, XMLError found, bool echo = true, bool extraNL = false) { - return XMLTest(testString, XMLDocument::ErrorName(expected), XMLDocument::ErrorName(found), echo, extraNL); + return XMLTest(testString, XMLDocument::ErrorIDToName(expected), XMLDocument::ErrorIDToName(found), echo, extraNL); } bool XMLTest(const char* testString, bool expected, bool found, bool echo = true, bool extraNL = false) @@ -1647,7 +1647,7 @@ int main( int argc, const char ** argv ) // ----------- Line Number Tracking -------------- { - struct Functor: XMLVisitor + struct TestUtil: XMLVisitor { void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine) { @@ -1725,46 +1725,50 @@ int main( int argc, const char ** argv ) str.Push(0); XMLTest(testString, expectedLines, str.Mem()); } - } T; + } tester; - T.TestParseError("ErrorLine-Parsing", "\n\n foo \n", XML_ERROR_PARSING, 2); - T.TestParseError("ErrorLine-Declaration", "\n", XML_ERROR_PARSING_DECLARATION, 2); - T.TestParseError("ErrorLine-Mismatch", "\n\n", XML_ERROR_MISMATCHED_ELEMENT, 2); - T.TestParseError("ErrorLine-CData", "\n\n foo bar \n", XML_ERROR_PARSING_TEXT, 3); - T.TestParseError("ErrorLine-Comment", "\n\n\n" - "", + + "\n" // 1 Doc, DecL + " d \n" // 3 Attribute Text Element + "newline in text \n" // 4 Text + "and second \n" // 6 Comment + "", // 7 Unknown + "D01L01E02A02A03T03E03T04E05T05C06U07"); - T.TestStringLines( + tester.TestStringLines( "LineNumbers-CRLF", - "\r\n" - "\n" - "\r\n" - "\n" - "text contining new line \n" - " and also containing crlf \r\n" - "", + + "\r\n" // 1 Doc (arguably should be line 2) + "\n" // 2 DecL + "\r\n" // 3 Element + "\n" // 4 + "text contining new line \n" // 5 Text + " and also containing crlf \r\n" // 6 + "", // 10 Element + "D01L02E03T05E07T07E10"); - T.TestFileLines( + tester.TestFileLines( "LineNumbers-File", "resources/utf8test.xml", "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");