Bladeren bron

tweaks, clarification to line numbers

Lee Thomason 9 jaren geleden
bovenliggende
commit
e90e901041
3 gewijzigde bestanden met toevoegingen van 57 en 52 verwijderingen
  1. 5 5
      tinyxml2.cpp
  2. 12 11
      tinyxml2.h
  3. 40 36
      xmltest.cpp

+ 5 - 5
tinyxml2.cpp

@@ -193,6 +193,7 @@ char* StrPair::ParseText( char* p, const char* endTag, int strFlags, int* curLin
 {
 {
     TIXMLASSERT( p );
     TIXMLASSERT( p );
     TIXMLASSERT( endTag && *endTag );
     TIXMLASSERT( endTag && *endTag );
+	TIXMLASSERT(curLineNumPtr);
 
 
     char* start = p;
     char* start = p;
     char  endChar = *endTag;
     char  endChar = *endTag;
@@ -238,8 +239,7 @@ void StrPair::CollapseWhitespace()
     // Adjusting _start would cause undefined behavior on delete[]
     // Adjusting _start would cause undefined behavior on delete[]
     TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
     TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
     // Trim leading space.
     // Trim leading space.
-    int unusedLineNum = 0;
-    _start = XMLUtil::SkipWhiteSpace( _start, &unusedLineNum );
+    _start = XMLUtil::SkipWhiteSpace( _start, 0 );
 
 
     if ( *_start ) {
     if ( *_start ) {
         const char* p = _start;	// the read pointer
         const char* p = _start;	// the read pointer
@@ -247,7 +247,7 @@ void StrPair::CollapseWhitespace()
 
 
         while( *p ) {
         while( *p ) {
             if ( XMLUtil::IsWhiteSpace( *p )) {
             if ( XMLUtil::IsWhiteSpace( *p )) {
-                p = XMLUtil::SkipWhiteSpace( p, &unusedLineNum );
+                p = XMLUtil::SkipWhiteSpace( p, 0 );
                 if ( *p == 0 ) {
                 if ( *p == 0 ) {
                     break;    // don't write to q; this trims the trailing space.
                     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);
 		_errorStr2.SetStr(str2);
 }
 }
 
 
-const char* XMLDocument::ErrorName(XMLError errorID)
+/*static*/ const char* XMLDocument::ErrorIDToName(XMLError errorID)
 {
 {
 	TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
 	TIXMLASSERT( errorID >= 0 && errorID < XML_ERROR_COUNT );
     const char* errorName = _errorNames[errorID];
     const char* errorName = _errorNames[errorID];
@@ -2257,7 +2257,7 @@ const char* XMLDocument::ErrorName(XMLError errorID)
 
 
 const char* XMLDocument::ErrorName() const
 const char* XMLDocument::ErrorName() const
 {
 {
-    return ErrorName(_errorID);
+    return ErrorIDToName(_errorID);
 }
 }
 
 
 void XMLDocument::PrintError() const
 void XMLDocument::PrintError() const

+ 12 - 11
tinyxml2.h

@@ -532,8 +532,9 @@ class XMLUtil
 public:
 public:
     static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr )	{
     static const char* SkipWhiteSpace( const char* p, int* curLineNumPtr )	{
         TIXMLASSERT( p );
         TIXMLASSERT( p );
+
         while( IsWhiteSpace(*p) ) {
         while( IsWhiteSpace(*p) ) {
-            if (*p == '\n') {
+            if (curLineNumPtr && *p == '\n') {
                 ++(*curLineNumPtr);
                 ++(*curLineNumPtr);
             }
             }
             ++p;
             ++p;
@@ -1765,7 +1766,7 @@ public:
         return _errorID;
         return _errorID;
     }
     }
 	const char* ErrorName() const;
 	const char* ErrorName() const;
-    static const char* ErrorName(XMLError errorID);
+    static const char* ErrorIDToName(XMLError errorID);
 
 
     /// Return a possibly helpful diagnostic location or string.
     /// Return a possibly helpful diagnostic location or string.
     const char* GetErrorStr1() const {
     const char* GetErrorStr1() const {
@@ -1800,15 +1801,15 @@ private:
     XMLDocument( const XMLDocument& );	// not supported
     XMLDocument( const XMLDocument& );	// not supported
     void operator=( 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(XMLElement) >	 _elementPool;
     MemPoolT< sizeof(XMLAttribute) > _attributePool;
     MemPoolT< sizeof(XMLAttribute) > _attributePool;

+ 40 - 36
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)
 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)
 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 --------------
     // ----------- Line Number Tracking --------------
     {
     {
-        struct Functor: XMLVisitor
+        struct TestUtil: XMLVisitor
         {
         {
             void TestParseError(const char *testString, const char *docStr, XMLError expected_error, int expectedLine)
             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);
                 str.Push(0);
                 XMLTest(testString, expectedLines, str.Mem());
                 XMLTest(testString, expectedLines, str.Mem());
             }
             }
-        } T;
-
-        T.TestParseError("ErrorLine-Parsing", "\n<root>\n foo \n<unclosed/>", XML_ERROR_PARSING, 2);
-        T.TestParseError("ErrorLine-Declaration", "<root>\n<?xml version=\"1.0\"?>", XML_ERROR_PARSING_DECLARATION, 2);
-        T.TestParseError("ErrorLine-Mismatch", "\n<root>\n</mismatch>", XML_ERROR_MISMATCHED_ELEMENT, 2);
-        T.TestParseError("ErrorLine-CData", "\n<root><![CDATA[ \n foo bar \n", XML_ERROR_PARSING_CDATA, 2);
-        T.TestParseError("ErrorLine-Text", "\n<root>\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
-        T.TestParseError("ErrorLine-Comment", "\n<root>\n<!-- >\n", XML_ERROR_PARSING_COMMENT, 3);
-        T.TestParseError("ErrorLine-Declaration", "\n<root>\n<? >\n", XML_ERROR_PARSING_DECLARATION, 3);
-        T.TestParseError("ErrorLine-Unknown", "\n<root>\n<! \n", XML_ERROR_PARSING_UNKNOWN, 3);
-        T.TestParseError("ErrorLine-Element", "\n<root>\n<unclosed \n", XML_ERROR_PARSING_ELEMENT, 3);
-        T.TestParseError("ErrorLine-Attribute", "\n<root>\n<unclosed \n att\n", XML_ERROR_PARSING_ATTRIBUTE, 4);
-        T.TestParseError("ErrorLine-ElementClose", "\n<root>\n<unclosed \n/unexpected", XML_ERROR_PARSING_ELEMENT, 3);
-
-        T.TestStringLines(
+        } tester;
+
+		tester.TestParseError("ErrorLine-Parsing", "\n<root>\n foo \n<unclosed/>", XML_ERROR_PARSING, 2);
+        tester.TestParseError("ErrorLine-Declaration", "<root>\n<?xml version=\"1.0\"?>", XML_ERROR_PARSING_DECLARATION, 2);
+        tester.TestParseError("ErrorLine-Mismatch", "\n<root>\n</mismatch>", XML_ERROR_MISMATCHED_ELEMENT, 2);
+        tester.TestParseError("ErrorLine-CData", "\n<root><![CDATA[ \n foo bar \n", XML_ERROR_PARSING_CDATA, 2);
+        tester.TestParseError("ErrorLine-Text", "\n<root>\n foo bar \n", XML_ERROR_PARSING_TEXT, 3);
+        tester.TestParseError("ErrorLine-Comment", "\n<root>\n<!-- >\n", XML_ERROR_PARSING_COMMENT, 3);
+        tester.TestParseError("ErrorLine-Declaration", "\n<root>\n<? >\n", XML_ERROR_PARSING_DECLARATION, 3);
+        tester.TestParseError("ErrorLine-Unknown", "\n<root>\n<! \n", XML_ERROR_PARSING_UNKNOWN, 3);
+        tester.TestParseError("ErrorLine-Element", "\n<root>\n<unclosed \n", XML_ERROR_PARSING_ELEMENT, 3);
+        tester.TestParseError("ErrorLine-Attribute", "\n<root>\n<unclosed \n att\n", XML_ERROR_PARSING_ATTRIBUTE, 4);
+        tester.TestParseError("ErrorLine-ElementClose", "\n<root>\n<unclosed \n/unexpected", XML_ERROR_PARSING_ELEMENT, 3);
+
+		tester.TestStringLines(
             "LineNumbers-String",
             "LineNumbers-String",
-            "<?xml version=\"1.0\"?>\n"
-                "<root a='b' \n"
-                "c='d'> d <blah/>  \n"
-                "newline in text \n"
-                "and second <zxcv/><![CDATA[\n"
-                " cdata test ]]><!-- comment -->\n"
-                "<! unknown></root>",
+
+            "<?xml version=\"1.0\"?>\n"					// 1 Doc, DecL
+                "<root a='b' \n"						// 2 Element Attribute
+                "c='d'> d <blah/>  \n"					// 3 Attribute Text Element
+                "newline in text \n"					// 4 Text
+                "and second <zxcv/><![CDATA[\n"			// 5 Element Text
+                " cdata test ]]><!-- comment -->\n"		// 6 Comment
+                "<! unknown></root>",					// 7 Unknown
+
             "D01L01E02A02A03T03E03T04E05T05C06U07");
             "D01L01E02A02A03T03E03T04E05T05C06U07");
 
 
-        T.TestStringLines(
+		tester.TestStringLines(
             "LineNumbers-CRLF",
             "LineNumbers-CRLF",
-            "\r\n"
-            "<?xml version=\"1.0\"?>\n"
-            "<root>\r\n"
-            "\n"
-            "text contining new line \n"
-            " and also containing crlf \r\n"
-            "<sub><![CDATA[\n"
-            "cdata containing new line \n"
-            " and also containing cflr\r\n"
-            "]]></sub><sub2/></root>",
+
+            "\r\n"										// 1 Doc (arguably should be line 2)
+            "<?xml version=\"1.0\"?>\n"					// 2 DecL
+            "<root>\r\n"								// 3 Element
+            "\n"										// 4
+            "text contining new line \n"				// 5 Text
+            " and also containing crlf \r\n"			// 6
+            "<sub><![CDATA[\n"							// 7 Element Text
+            "cdata containing new line \n"				// 8
+            " and also containing cflr\r\n"				// 9
+            "]]></sub><sub2/></root>",					// 10 Element
+
             "D01L02E03T05E07T07E10");
             "D01L02E03T05E07T07E10");
 
 
-        T.TestFileLines(
+		tester.TestFileLines(
             "LineNumbers-File",
             "LineNumbers-File",
             "resources/utf8test.xml",
             "resources/utf8test.xml",
             "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");
             "D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");