tweaks, clarification to line numbers

This commit is contained in:
Lee Thomason 2016-12-24 07:34:39 -08:00
parent 002713856b
commit e90e901041
3 changed files with 55 additions and 50 deletions

View File

@ -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

View File

@ -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;

View File

@ -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<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);
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);
T.TestStringLines(
tester.TestStringLines(
"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");
T.TestStringLines(
tester.TestStringLines(
"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");
T.TestFileLines(
tester.TestFileLines(
"LineNumbers-File",
"resources/utf8test.xml",
"D01L01E02E03A03A03T03E04A04A04T04E05A05A05T05E06A06A06T06E07A07A07T07E08A08A08T08E09T09E10T10");