From 19e6bde09ad58839ff6776e4169a209b47d265cd Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Tue, 25 Jul 2017 14:14:31 -0500 Subject: [PATCH] Rewrite class UTF8 to contain rather than extend std::string storage. This forces the compiler class specific features rather than borrowing from the base class's std::string. In some cases prior to this, wxString( std::string ) was being called rather than UTF8::operator wxString() leading to garbled wxStrings. Added function UTF8::wx_str() which is of great convenience also. Implicit conversions still work as before, and hopefully more reliably. --- common/fp_lib_table.cpp | 2 +- common/lib_id.cpp | 6 +- common/utf8.cpp | 16 ++- cvpcb/cvpcb_mainframe.cpp | 2 +- eeschema/class_library.cpp | 2 +- eeschema/dialogs/dialog_symbol_remap.cpp | 6 +- eeschema/libarch.cpp | 2 +- eeschema/project_rescue.cpp | 4 +- eeschema/symbol_lib_table.cpp | 2 +- include/lib_id.h | 2 +- include/utf8.h | 133 ++++++++++++++------- pcbnew/eagle_plugin.cpp | 2 +- pcbnew/exporters/gen_modules_placefile.cpp | 4 +- pcbnew/github/github_plugin.cpp | 19 ++- pcbnew/kicad_plugin.cpp | 29 +++-- pcbnew/legacy_plugin.cpp | 6 +- pcbnew/microwave/microwave_inductor.cpp | 2 +- tools/utf8_tests.cpp | 28 ++++- 18 files changed, 170 insertions(+), 97 deletions(-) diff --git a/common/fp_lib_table.cpp b/common/fp_lib_table.cpp index 36f0c5dae7..8ebcbb7f7c 100644 --- a/common/fp_lib_table.cpp +++ b/common/fp_lib_table.cpp @@ -286,7 +286,7 @@ MODULE* FP_LIB_TABLE::FootprintLoad( const wxString& aNickname, const wxString& LIB_ID& fpid = (LIB_ID&) ret->GetFPID(); // Catch any misbehaving plugin, which should be setting internal footprint name properly: - wxASSERT( aFootprintName == (wxString) fpid.GetLibItemName() ); + wxASSERT( aFootprintName == fpid.GetLibItemName().wx_str() ); // and clearing nickname wxASSERT( !fpid.GetLibNickname().size() ); diff --git a/common/lib_id.cpp b/common/lib_id.cpp index debd4c8da5..67b22bfb03 100644 --- a/common/lib_id.cpp +++ b/common/lib_id.cpp @@ -83,7 +83,7 @@ int RevCmp( const char* s1, const char* s2 ) //----------------------------------------- -static inline int okLogical( const std::string& aField ) +static inline int okLogical( const UTF8& aField ) { // std::string::npos is largest positive number, casting to int makes it -1. // Returning that means success. @@ -91,7 +91,7 @@ static inline int okLogical( const std::string& aField ) } -static int okRevision( const std::string& aField ) +static int okRevision( const UTF8& aField ) { char rev[32]; // C string for speed @@ -175,7 +175,7 @@ int LIB_ID::Parse( const UTF8& aId ) } -LIB_ID::LIB_ID( const std::string& aId ) +LIB_ID::LIB_ID( const UTF8& aId ) { int offset = Parse( aId ); diff --git a/common/utf8.cpp b/common/utf8.cpp index a7df690143..1d4bf1d555 100644 --- a/common/utf8.cpp +++ b/common/utf8.cpp @@ -40,11 +40,17 @@ UTF8::UTF8( const wxString& o ) : - std::string( (const char*) o.utf8_str() ) + m_s( (const char*) o.utf8_str() ) { } +wxString UTF8::wx_str() const +{ + return wxString( c_str(), wxConvUTF8 ); +} + + UTF8::operator wxString () const { return wxString( c_str(), wxConvUTF8 ); @@ -53,7 +59,7 @@ UTF8::operator wxString () const UTF8& UTF8::operator=( const wxString& o ) { - std::string::operator=( (const char*) o.utf8_str() ); + m_s = (const char*) o.utf8_str(); return *this; } @@ -193,7 +199,7 @@ bool IsUTF8( const char* aString ) UTF8::UTF8( const wchar_t* txt ) : // size initial string safely large enough, then shrink to known size later. - std::string( wcslen( txt ) * 4, 0 ) + m_s( wcslen( txt ) * 4, 0 ) { /* @@ -206,9 +212,9 @@ UTF8::UTF8( const wchar_t* txt ) : */ - int sz = wxConvUTF8.WC2MB( (char*) data(), txt, size() ); + int sz = wxConvUTF8.WC2MB( (char*) m_s.data(), txt, m_s.size() ); - resize( sz ); + m_s.resize( sz ); } diff --git a/cvpcb/cvpcb_mainframe.cpp b/cvpcb/cvpcb_mainframe.cpp index 0d41232164..8afec3b56d 100644 --- a/cvpcb/cvpcb_mainframe.cpp +++ b/cvpcb/cvpcb_mainframe.cpp @@ -811,7 +811,7 @@ int CVPCB_MAINFRAME::ReadSchematicNetlist( const std::string& aNetlist ) for( unsigned ii = 0; ii < m_netlist.GetCount(); ii++ ) { if( m_netlist.GetComponent( ii )->GetFPID().GetLibItemName() == std::string( "$noname" ) ) - m_netlist.GetComponent( ii )->SetFPID( LIB_ID( wxEmptyString ) ); + m_netlist.GetComponent( ii )->SetFPID( LIB_ID() ); } // Sort components by reference: diff --git a/eeschema/class_library.cpp b/eeschema/class_library.cpp index 3437829882..efdfd85a09 100644 --- a/eeschema/class_library.cpp +++ b/eeschema/class_library.cpp @@ -672,7 +672,7 @@ void PART_LIBS::LoadAllLibraries( PROJECT* aProject, bool aShowProgress ) { // Use a different exception type so catch()er can route to proper use // of the HTML_MESSAGE_BOX. - THROW_PARSE_ERROR( wxEmptyString, UTF8( __func__ ), UTF8( libs_not_found ), 0, 0 ); + THROW_PARSE_ERROR( wxEmptyString, __func__, TO_UTF8(libs_not_found), 0, 0 ); } #if defined(DEBUG) && 1 diff --git a/eeschema/dialogs/dialog_symbol_remap.cpp b/eeschema/dialogs/dialog_symbol_remap.cpp index 64951a3d57..473baad675 100644 --- a/eeschema/dialogs/dialog_symbol_remap.cpp +++ b/eeschema/dialogs/dialog_symbol_remap.cpp @@ -205,14 +205,14 @@ void DIALOG_SYMBOL_REMAP::remapSymbolsToLibTable( REPORTER& aReporter ) if( !remapSymbolToLibTable( symbol ) ) { msg.Printf( _( "No symbol '%s' founded in symbol library table." ), - FROM_UTF8( symbol->GetLibId().GetLibItemName() ) ); + symbol->GetLibId().GetLibItemName().wx_str() ); aReporter.Report( msg, REPORTER::RPT_WARNING ); } else { msg.Printf( _( "Symbol '%s' mapped to symbol library '%s'." ), - FROM_UTF8( symbol->GetLibId().GetLibItemName() ), - FROM_UTF8( symbol->GetLibId().GetLibNickname() ) ); + symbol->GetLibId().GetLibItemName().wx_str(), + symbol->GetLibId().GetLibNickname().wx_str() ); aReporter.Report( msg, REPORTER::RPT_ACTION ); } } diff --git a/eeschema/libarch.cpp b/eeschema/libarch.cpp index 6581250252..2ceefb2331 100644 --- a/eeschema/libarch.cpp +++ b/eeschema/libarch.cpp @@ -106,7 +106,7 @@ bool SCH_EDIT_FRAME::CreateArchiveLibrary( const wxString& aFileName ) catch( ... /* IO_ERROR ioe */ ) { msg.Printf( _( "Failed to add symbol %s to library file '%s'" ), - wxString( component->GetLibId().GetLibItemName() ), aFileName ); + component->GetLibId().GetLibItemName().wx_str(), aFileName ); DisplayError( this, msg ); return false; } diff --git a/eeschema/project_rescue.cpp b/eeschema/project_rescue.cpp index 87ff481df2..c757cde900 100644 --- a/eeschema/project_rescue.cpp +++ b/eeschema/project_rescue.cpp @@ -264,7 +264,7 @@ public: for( SCH_COMPONENT* each_component : *( aRescuer.GetComponents() ) ) { - wxString part_name( each_component->GetLibId().GetLibItemName() ); + wxString part_name = each_component->GetLibId().GetLibItemName(); if( last_part_name != part_name ) { @@ -373,7 +373,7 @@ public: for( SCH_COMPONENT* each_component : *( aRescuer.GetComponents() ) ) { - wxString part_name( each_component->GetLibId().GetLibItemName() ); + wxString part_name = each_component->GetLibId().GetLibItemName(); if( old_part_name != part_name ) { diff --git a/eeschema/symbol_lib_table.cpp b/eeschema/symbol_lib_table.cpp index 6189275bab..70cb6ded15 100644 --- a/eeschema/symbol_lib_table.cpp +++ b/eeschema/symbol_lib_table.cpp @@ -271,7 +271,7 @@ LIB_ALIAS* SYMBOL_LIB_TABLE::LoadSymbol( const wxString& aNickname, const wxStri LIB_ID& id = (LIB_ID&) ret->GetPart()->GetLibId(); // Catch any misbehaving plugin, which should be setting internal alias name properly: - wxASSERT( aAliasName == (wxString) id.GetLibItemName() ); + wxASSERT( aAliasName == id.GetLibItemName().wx_str() ); // and clearing nickname wxASSERT( !id.GetLibNickname().size() ); diff --git a/include/lib_id.h b/include/lib_id.h index 49a53a268f..9a914b403e 100644 --- a/include/lib_id.h +++ b/include/lib_id.h @@ -70,7 +70,7 @@ public: * * @param aId is a string to be parsed into the LIB_ID object. */ - LIB_ID( const std::string& aId ); + LIB_ID( const UTF8& aId ); LIB_ID( const wxString& aId ); diff --git a/include/utf8.h b/include/utf8.h index 998e6afe30..26c467f0d9 100644 --- a/include/utf8.h +++ b/include/utf8.h @@ -50,13 +50,13 @@ bool IsUTF8( const char* aString ); /** * Class UTF8 - * is an 8 bit std::string that is assuredly encoded in UTF8, and supplies special - * conversion support to and from wxString, and has iteration over unicode characters. + * is an 8 bit string that is assuredly encoded in UTF8, and supplies special + * conversion support to and from wxString, to and from std::string, and has + * non-mutating iteration over unicode characters. * *

I've been careful to supply only conversion facilities and not try - * and duplicate wxString() with many member functions. In the end it is - * to be a std::string. There are multiple ways to create text into a std::string - * without the need of too many member functions: + * and duplicate wxString() with many member functions. There are multiple ways + * to create text into a std::string without the need of too many member functions: * *