fix BOM preservation bugs. add new tests

This commit is contained in:
Lee Thomason (grinliz) 2013-04-29 09:15:37 -07:00
parent 1377fe7ec0
commit d0a38c3a20
2 changed files with 38 additions and 5 deletions

View File

@ -1663,6 +1663,7 @@ XMLError XMLDocument::SaveFile( FILE* fp, bool compact )
XMLError XMLDocument::Parse( const char* p, size_t len ) XMLError XMLDocument::Parse( const char* p, size_t len )
{ {
const char* start = p;
Clear(); Clear();
if ( !p || !*p ) { if ( !p || !*p ) {
@ -1683,7 +1684,8 @@ XMLError XMLDocument::Parse( const char* p, size_t len )
return _errorID; return _errorID;
} }
ParseDeep( _charBuffer, 0 ); int delta = p - start; // skip initial whitespace, BOM, etc.
ParseDeep( _charBuffer+delta, 0 );
return _errorID; return _errorID;
} }

View File

@ -25,7 +25,7 @@ int gPass = 0;
int gFail = 0; int gFail = 0;
bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true ) bool XMLTest (const char* testString, const char* expected, const char* found, bool echo=true, bool extraNL=false )
{ {
bool pass = !strcmp( expected, found ); bool pass = !strcmp( expected, found );
if ( pass ) if ( pass )
@ -33,10 +33,19 @@ bool XMLTest (const char* testString, const char* expected, const char* found, b
else else
printf ("[fail]"); printf ("[fail]");
if ( !echo ) if ( !echo ) {
printf (" %s\n", testString); printf (" %s\n", testString);
else }
printf (" %s [%s][%s]\n", testString, expected, found); else {
if ( extraNL ) {
printf( " %s\n", testString );
printf( "%s\n", expected );
printf( "%s\n", found );
}
else {
printf (" %s [%s][%s]\n", testString, expected, found);
}
}
if ( pass ) if ( pass )
++gPass; ++gPass;
@ -1164,6 +1173,28 @@ int main( int argc, const char ** argv )
XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error ); XMLTest( "Loading an empty file", XML_ERROR_EMPTY_DOCUMENT, error );
} }
{
// BOM preservation
static const char* xml_bom_preservation = "\xef\xbb\xbf<element/>\n";
{
XMLDocument doc;
XMLTest( "BOM preservation (parse)", XML_NO_ERROR, doc.Parse( xml_bom_preservation ), false );
XMLPrinter printer;
doc.Print( &printer );
XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
doc.SaveFile( "resources/bomtest.xml" );
}
{
XMLDocument doc;
doc.LoadFile( "resources/bomtest.xml" );
XMLTest( "BOM preservation (load)", true, doc.HasBOM(), false );
XMLPrinter printer;
doc.Print( &printer );
XMLTest( "BOM preservation (compare)", xml_bom_preservation, printer.CStr(), false, true );
}
}
// ----------- Performance tracking -------------- // ----------- Performance tracking --------------
{ {