diff --git a/common/refdes_utils.cpp b/common/refdes_utils.cpp index aeeb446b6b..aa12c359d9 100644 --- a/common/refdes_utils.cpp +++ b/common/refdes_utils.cpp @@ -32,16 +32,25 @@ namespace UTIL { -wxString GetReferencePrefix( const wxString& aRefDes ) +wxString GetRefDesPrefix( const wxString& aRefDes ) { - // find the first non-digit character from the back + // find the first non-digit, non-question-mark character from the back auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(), - []( wxUniChar aChr ) { return !std::isdigit( aChr ); } ); + []( wxUniChar aChr ) + { + return aChr != '?' && !std::isdigit( aChr ); + } ); return { aRefDes.begin(), res.base() }; } +wxString GetRefDesUnannotated( const wxString& aSource ) +{ + return UTIL::GetRefDesPrefix( aSource ) + wxT( "?" ); +} + + int RefDesStringCompare( const wxString& aFirst, const wxString& aSecond ) { // Compare unescaped text diff --git a/eeschema/sch_symbol.cpp b/eeschema/sch_symbol.cpp index bb0c7a2a4a..516a7b2dd2 100644 --- a/eeschema/sch_symbol.cpp +++ b/eeschema/sch_symbol.cpp @@ -34,7 +34,7 @@ #include #include #include - +#include /** * Convert a wxString to UTF8 and replace any control characters with a ~, @@ -92,29 +92,6 @@ static LIB_PART* dummy() } -wxString refDesPrefix( const wxString& aSource ) -{ - wxString result; - size_t sourceLen = aSource.length(); - - for( size_t i = 0; i < sourceLen; ++i ) - { - if( aSource[i] == '?' || wxIsdigit( aSource[i] ) ) - break; - - result += aSource[i]; - } - - return result; -} - - -wxString refDesUnannotated( const wxString& aSource ) -{ - return refDesPrefix( aSource ) + wxT( "?" ); -} - - SCH_COMPONENT::SCH_COMPONENT( const wxPoint& aPos, SCH_ITEM* aParent ) : SCH_ITEM( aParent, SCH_COMPONENT_T ) { @@ -147,10 +124,10 @@ SCH_COMPONENT::SCH_COMPONENT( const LIB_PART& aPart, const LIB_ID& aLibId, true, /* reset ref */ true /* reset other fields */ ); - m_prefix = refDesPrefix( m_part->GetReferenceField().GetText() ); + m_prefix = UTIL::GetRefDesPrefix( m_part->GetReferenceField().GetText() ); if( aSheet ) - SetRef( aSheet, refDesUnannotated( m_prefix ) ); + SetRef( aSheet, UTIL::GetRefDesUnannotated( m_prefix ) ); // Inherit the include in bill of materials and board netlist settings from library symbol. m_inBom = aPart.GetIncludeInBom(); @@ -476,7 +453,7 @@ const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet, bool aInclude } if( ref.IsEmpty() ) - ref = refDesUnannotated( m_prefix ); + ref = UTIL::GetRefDesUnannotated( m_prefix ); if( aIncludeUnit && GetUnitCount() > 1 ) ref += LIB_PART::SubReference( GetUnit() ); @@ -487,7 +464,7 @@ const wxString SCH_COMPONENT::GetRef( const SCH_SHEET_PATH* sheet, bool aInclude bool SCH_COMPONENT::IsReferenceStringValid( const wxString& aReferenceString ) { - return !refDesPrefix( aReferenceString ).IsEmpty(); + return !UTIL::GetRefDesPrefix( aReferenceString ).IsEmpty(); } @@ -526,7 +503,7 @@ void SCH_COMPONENT::SetRef( const SCH_SHEET_PATH* sheet, const wxString& ref ) rf->SetText( ref ); // for drawing. // Reinit the m_prefix member if needed - m_prefix = refDesPrefix( ref ); + m_prefix = UTIL::GetRefDesPrefix( ref ); if( m_prefix.IsEmpty() ) m_prefix = wxT( "U" ); @@ -581,7 +558,7 @@ void SCH_COMPONENT::SetUnitSelection( const SCH_SHEET_PATH* aSheet, int aUnitSel } // didn't find it; better add it - AddHierarchicalReference( path, refDesUnannotated( m_prefix ), aUnitSelection ); + AddHierarchicalReference( path, UTIL::GetRefDesUnannotated( m_prefix ), aUnitSelection ); } @@ -638,7 +615,8 @@ void SCH_COMPONENT::SetValue( const SCH_SHEET_PATH* sheet, const wxString& aValu } // didn't find it; better add it - AddHierarchicalReference( path, refDesUnannotated( m_prefix ), m_unit, aValue, wxEmptyString ); + AddHierarchicalReference( path, UTIL::GetRefDesUnannotated( m_prefix ), m_unit, + aValue, wxEmptyString ); } @@ -688,8 +666,8 @@ void SCH_COMPONENT::SetFootprint( const SCH_SHEET_PATH* sheet, const wxString& a } // didn't find it; better add it - AddHierarchicalReference( path, refDesUnannotated( m_prefix ), m_unit, wxEmptyString, - aFootprint ); + AddHierarchicalReference( path, UTIL::GetRefDesUnannotated( m_prefix ), m_unit, + wxEmptyString, aFootprint ); } @@ -1039,7 +1017,7 @@ bool SCH_COMPONENT::ResolveTextVar( wxString* token, int aDepth ) const void SCH_COMPONENT::ClearAnnotation( const SCH_SHEET_PATH* aSheetPath ) { // Build a reference with no annotation, i.e. a reference ending with a single '?' - wxString defRef = refDesUnannotated( m_prefix ); + wxString defRef = UTIL::GetRefDesUnannotated( m_prefix ); if( aSheetPath ) { diff --git a/include/refdes_utils.h b/include/refdes_utils.h index 9b1b643426..feb87f9678 100644 --- a/include/refdes_utils.h +++ b/include/refdes_utils.h @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2019 KiCad Developers, see CHANGELOG.TXT for contributors. + * Copyright (C) 2019-2021 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 @@ -38,10 +38,21 @@ namespace UTIL * Get the (non-numeric) prefix from a refdes - e.g. * R1 -> R * IC34 -> IC + * U? -> U * @param aRefDes full refdes * @return the prefix, or empty string if nothing found */ -wxString GetReferencePrefix( const wxString& aRefDes ); +wxString GetRefDesPrefix( const wxString& aRefDes ); + +/** + * Return an unannotated refdes from either a prefix or an existing refdes. + * R -> R? + * IC34 -> IC? + * U? -> U? + * @param aRefDes + * @return + */ +wxString GetRefDesUnannotated( const wxString& aRefDes ); /** * Acts just like the strcmp function but treats numbers within the string text diff --git a/pcbnew/footprint.cpp b/pcbnew/footprint.cpp index 3d83b296e1..b00f5d3a8f 100644 --- a/pcbnew/footprint.cpp +++ b/pcbnew/footprint.cpp @@ -1705,7 +1705,9 @@ wxString FOOTPRINT::GetNextPadName( const wxString& aLastPadName ) const for( PAD* pad : m_pads ) usedNames.insert( pad->GetName() ); - wxString prefix = UTIL::GetReferencePrefix( aLastPadName ); + // Pad names aren't technically reference designators, but the formatting is close enough + // for these to give us what we need. + wxString prefix = UTIL::GetRefDesPrefix( aLastPadName ); int num = GetTrailingInt( aLastPadName ); while( usedNames.count( wxString::Format( "%s%d", prefix, num ) ) ) @@ -1720,8 +1722,8 @@ void FOOTPRINT::IncrementReference( int aDelta ) const wxString& refdes = GetReference(); SetReference( wxString::Format( wxT( "%s%i" ), - UTIL::GetReferencePrefix( refdes ), - GetTrailingInt( refdes ) + aDelta ) ); + UTIL::GetRefDesPrefix( refdes ), + GetTrailingInt( refdes ) + aDelta ) ); } diff --git a/qa/common/test_refdes_utils.cpp b/qa/common/test_refdes_utils.cpp index 6aa8443508..440ce7ef62 100644 --- a/qa/common/test_refdes_utils.cpp +++ b/qa/common/test_refdes_utils.cpp @@ -38,7 +38,7 @@ BOOST_AUTO_TEST_SUITE( RefdesUtils ) /** - * Test the #UTIL::GetReferencePrefix function + * Test the #UTIL::GetRefDesPrefix function */ BOOST_AUTO_TEST_CASE( GetPrefix ) { @@ -60,7 +60,7 @@ BOOST_AUTO_TEST_CASE( GetPrefix ) { BOOST_TEST_CONTEXT( "Testing: " << c.first ) { - BOOST_CHECK_EQUAL( UTIL::GetReferencePrefix( c.first ), c.second ); + BOOST_CHECK_EQUAL( UTIL::GetRefDesPrefix( c.first ), c.second ); } } }