Reverse commit 4011ed4e31.

This commit was too broad and not cognizant of the purpose of the class
UTF8.

Add MAYBE_VERIFY_UTF8() macro, which can trap non-UTF8 encoded strings in
debug builds.

Use that macro conditionally in class UTF8 to trap non-UTF8 encoded strings
being put into UTF8 instances.
This commit is contained in:
Dick Hollenbeck
2017-07-26 08:26:56 -04:00
committed by Wayne Stambaugh
parent 161045f17d
commit 4e7de8a761
36 changed files with 207 additions and 143 deletions
+37 -9
View File
@@ -1,8 +1,8 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2013 KiCad Developers, see CHANGELOG.TXT for contributors.
* Copyright (C) 2013-2017 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2013-2017 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -23,6 +23,7 @@
*/
#include <utf8.h>
#include <ki_exception.h>
/* THROW_IO_ERROR needs this, but it includes this file, so until some
factoring of THROW_IO_ERROR into a separate header, defer and use the asserts.
@@ -31,6 +32,7 @@
#include <assert.h>
/*
These are not inlined so that code space is saved by encapsulating the
creation of intermediate objects and the referencing of wxConvUTF8.
@@ -56,10 +58,6 @@ UTF8& UTF8::operator=( const wxString& o )
}
#ifndef THROW_IO_ERROR
#define THROW_IO_ERROR(x) // nothing
#endif
// There is no wxWidgets function that does this, because wchar_t is 16 bits
// on windows and wx wants to encode the output in UTF16 for such.
@@ -117,7 +115,7 @@ int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
ch = ((s[0] & 0x1f) << 6) +
((s[1] & 0x3f) << 0);
assert( ch > 0x007F && ch <= 0x07FF );
// assert( ch > 0x007F && ch <= 0x07FF );
break;
case 3:
@@ -134,7 +132,7 @@ int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
((s[1] & 0x3f) << 6 ) +
((s[2] & 0x3f) << 0 );
assert( ch > 0x07FF && ch <= 0xFFFF );
// assert( ch > 0x07FF && ch <= 0xFFFF );
break;
case 4:
@@ -152,7 +150,7 @@ int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
((s[2] & 0x3f) << 6 ) +
((s[3] & 0x3f) << 0 );
assert( ch > 0xFFFF && ch <= 0x10ffff );
// assert( ch > 0xFFFF && ch <= 0x10ffff );
break;
}
@@ -163,6 +161,36 @@ int UTF8::uni_forward( const unsigned char* aSequence, unsigned* aResult )
}
bool IsUTF8( const char* aString )
{
int len = strlen( aString );
if( len )
{
const unsigned char* next = (unsigned char*) aString;
const unsigned char* end = next + len;
try
{
while( next < end )
{
next += UTF8::uni_forward( next, NULL );
}
// uni_forward() should find the exact end if it is truly UTF8
if( next > end )
return false;
}
catch( const IO_ERROR& ioe )
{
return false;
}
}
return true;
}
UTF8::UTF8( const wchar_t* txt ) :
// size initial string safely large enough, then shrink to known size later.
std::string( wcslen( txt ) * 4, 0 )