Browse Source
Break out ref-des-centric functions to own header
Break out ref-des-centric functions to own header
This breaks the following functions out to a general-purposed refdes utils header: * MODULE::GetReferencePrefix() * kicad_string.h RefDesStringCompare() This acheives: * Slimming of MODULE interface * Placement of refdes code in common rather than pcbnew ** Testing of this code in qa_common * Tighter and smaller includes for code that only needed refdes functions Note: there are failing tests commited (as expected failures). These are the cause of lp:1813669 and will be fixed as a follow-up commit.pull/13/head
committed by
Seth Hillbrand
16 changed files with 282 additions and 112 deletions
-
1common/CMakeLists.txt
-
94common/refdes_utils.cpp
-
41common/string.cpp
-
11eeschema/component_references_lister.cpp
-
5eeschema/dialogs/dialog_fields_editor_global.cpp
-
16eeschema/netlist_exporters/netlist_exporter.cpp
-
11eeschema/netlist_exporters/netlist_exporter_generic.cpp
-
4eeschema/netlist_exporters/netlist_exporter_generic.h
-
10include/kicad_string.h
-
57include/refdes_utils.h
-
6pcb_calculator/class_regulator_data.h
-
28pcbnew/class_module.cpp
-
9pcbnew/class_module.h
-
4pcbnew/pcb_netlist.cpp
-
1qa/common/CMakeLists.txt
-
96qa/common/test_refdes_utils.cpp
@ -0,0 +1,94 @@ |
|||
/*
|
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2019 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 |
|||
* as published by the Free Software Foundation; either version 2 |
|||
* of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program; if not, you may find one here: |
|||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|||
* or you may search the http://www.gnu.org website for the version 2 license,
|
|||
* or you may write to the Free Software Foundation, Inc., |
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|||
*/ |
|||
|
|||
#include <refdes_utils.h>
|
|||
|
|||
#include <kicad_string.h>
|
|||
|
|||
namespace UTIL |
|||
{ |
|||
|
|||
wxString GetReferencePrefix( const wxString& aRefDes ) |
|||
{ |
|||
wxString prefix = aRefDes; |
|||
|
|||
int strIndex = prefix.length() - 1; |
|||
while( strIndex >= 0 ) |
|||
{ |
|||
const wxUniChar chr = prefix.GetChar( strIndex ); |
|||
|
|||
// numeric suffix
|
|||
if( chr >= '0' && chr <= '9' ) |
|||
break; |
|||
|
|||
strIndex--; |
|||
} |
|||
|
|||
prefix = prefix.Mid( 0, strIndex ); |
|||
|
|||
return prefix; |
|||
} |
|||
|
|||
|
|||
int RefDesStringCompare( const wxString& aFirst, const wxString& aSecond ) |
|||
{ |
|||
// Compare unescaped text
|
|||
wxString strFWord = UnescapeString( aFirst ); |
|||
wxString strSWord = UnescapeString( aSecond ); |
|||
|
|||
// The different sections of the two strings
|
|||
wxString strFWordBeg, strFWordMid, strFWordEnd; |
|||
wxString strSWordBeg, strSWordMid, strSWordEnd; |
|||
|
|||
// Split the two strings into separate parts
|
|||
SplitString( strFWord, &strFWordBeg, &strFWordMid, &strFWordEnd ); |
|||
SplitString( strSWord, &strSWordBeg, &strSWordMid, &strSWordEnd ); |
|||
|
|||
// Compare the Beginning section of the strings
|
|||
int isEqual = strFWordBeg.CmpNoCase( strSWordBeg ); |
|||
|
|||
if( isEqual > 0 ) |
|||
return 1; |
|||
else if( isEqual < 0 ) |
|||
return -1; |
|||
else |
|||
{ |
|||
// If the first sections are equal compare their digits
|
|||
long lFirstDigit = 0; |
|||
long lSecondDigit = 0; |
|||
|
|||
strFWordMid.ToLong( &lFirstDigit ); |
|||
strSWordMid.ToLong( &lSecondDigit ); |
|||
|
|||
if( lFirstDigit > lSecondDigit ) |
|||
return 1; |
|||
else if( lFirstDigit < lSecondDigit ) |
|||
return -1; |
|||
// If the first two sections are equal compare the endings
|
|||
else |
|||
return strFWordEnd.CmpNoCase( strSWordEnd ); |
|||
} |
|||
} |
|||
|
|||
|
|||
} // namespace UTIL
|
|||
@ -0,0 +1,57 @@ |
|||
/* |
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2019 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 |
|||
* as published by the Free Software Foundation; either version 2 |
|||
* of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program; if not, you may find one here: |
|||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
|||
* or you may search the http://www.gnu.org website for the version 2 license, |
|||
* or you may write to the Free Software Foundation, Inc., |
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|||
*/ |
|||
|
|||
/** |
|||
* @file |
|||
* Collection of utility functions for component reference designators (refdes) |
|||
*/ |
|||
|
|||
#ifndef REFDES_UTILS__H |
|||
#define REFDES_UTILS__H |
|||
|
|||
#include <wx/string.h> |
|||
|
|||
namespace UTIL |
|||
{ |
|||
|
|||
/** |
|||
* Get the (non-numeric) prefix from a refdes - e.g. |
|||
* R1 -> R |
|||
* IC34 -> IC |
|||
* @param aRefDes full refdes |
|||
* @return the prefix, or empty string if nothing found |
|||
*/ |
|||
wxString GetReferencePrefix( const wxString& aRefDes ); |
|||
|
|||
/** |
|||
* Acts just like the strcmp function but treats numbers within the string text |
|||
* correctly for sorting. eg. A10 > A2 |
|||
* return -1 if first string is less than the second |
|||
* return 0 if the strings are equal |
|||
* return 1 if the first string is greater than the second |
|||
*/ |
|||
int RefDesStringCompare( const wxString& lhs, const wxString& rhs ); |
|||
|
|||
} // namespace UTIL |
|||
|
|||
#endif // REFDES_UTILS__H |
|||
@ -0,0 +1,96 @@ |
|||
/*
|
|||
* This program source code file is part of KiCad, a free EDA CAD application. |
|||
* |
|||
* Copyright (C) 2018 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 |
|||
* as published by the Free Software Foundation; either version 2 |
|||
* of the License, or (at your option) any later version. |
|||
* |
|||
* This program is distributed in the hope that it will be useful, |
|||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
* GNU General Public License for more details. |
|||
* |
|||
* You should have received a copy of the GNU General Public License |
|||
* along with this program; if not, you may find one here: |
|||
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|||
* or you may search the http://www.gnu.org website for the version 2 license,
|
|||
* or you may write to the Free Software Foundation, Inc., |
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|||
*/ |
|||
|
|||
/**
|
|||
* @file |
|||
* Test suite for refdes functions |
|||
*/ |
|||
|
|||
#include <unit_test_utils/unit_test_utils.h>
|
|||
|
|||
// Code under test
|
|||
#include <refdes_utils.h>
|
|||
|
|||
/**
|
|||
* Declare the test suite |
|||
*/ |
|||
BOOST_AUTO_TEST_SUITE( RefdesUtils ) |
|||
|
|||
#ifdef HAVE_EXPECTED_FAILURES
|
|||
|
|||
/**
|
|||
* Test the #UTIL::GetReferencePrefix function |
|||
*/ |
|||
BOOST_AUTO_TEST_CASE( GetPrefix, *boost::unit_test::expected_failures( 2 ) ) |
|||
{ |
|||
using CASE = std::pair<std::string, std::string>; |
|||
|
|||
const std::vector<CASE> cases = { |
|||
{ "", "" }, // empty
|
|||
{ "U", "U" }, // no number
|
|||
{ "U1", "U" }, // single digit
|
|||
{ "U10", "U" }, // double digit // fails!
|
|||
{ "U1000", "U" }, //multi digit // fails!
|
|||
}; |
|||
|
|||
for( const auto& c : cases ) |
|||
{ |
|||
BOOST_CHECK_EQUAL( UTIL::GetReferencePrefix( c.first ), c.second ); |
|||
} |
|||
} |
|||
|
|||
#endif
|
|||
|
|||
struct REF_DES_COMP_CASE |
|||
{ |
|||
std::string m_refdes_a; |
|||
std::string m_refdes_b; |
|||
int m_exp_res; |
|||
}; |
|||
|
|||
/**
|
|||
* Test the #UTIL::RefDesStringCompare function |
|||
*/ |
|||
BOOST_AUTO_TEST_CASE( RefDesComp ) |
|||
{ |
|||
const int SAME = 0; |
|||
const int LESS = -1; |
|||
const int MORE = 1; |
|||
|
|||
const std::vector<REF_DES_COMP_CASE> cases = { |
|||
{ "", "", SAME }, |
|||
{ "U", "U", SAME }, |
|||
{ "U1", "U1", SAME }, |
|||
{ "U1", "U2", LESS }, |
|||
{ "U2", "U1", MORE }, |
|||
{ "U1000", "U2000", LESS }, |
|||
}; |
|||
|
|||
for( const auto& c : cases ) |
|||
{ |
|||
BOOST_CHECK_EQUAL( UTIL::RefDesStringCompare( c.m_refdes_a, c.m_refdes_b ), c.m_exp_res ); |
|||
} |
|||
} |
|||
|
|||
|
|||
BOOST_AUTO_TEST_SUITE_END() |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue