Fix broken GetReferencePrefix function

This function was incorrectly processing refdeses like "U1000".

Change the algorithm to a simpler STL-compatible one and
update the tests.

Fixes: lp:1813669
* https://bugs.launchpad.net/kicad/+bug/1813669
This commit is contained in:
John Beard 2019-01-29 11:46:48 +00:00 committed by Seth Hillbrand
parent 88f9f6f072
commit 0415c89c5b
2 changed files with 22 additions and 25 deletions

View File

@ -25,28 +25,20 @@
#include <kicad_string.h>
#include <algorithm>
#include <cctype>
namespace UTIL
{
wxString GetReferencePrefix( const wxString& aRefDes )
{
wxString prefix = aRefDes;
// find the first non-digit character from the back
auto res = std::find_if( aRefDes.rbegin(), aRefDes.rend(),
[]( wxUniChar aChr ) { return !std::isdigit( aChr ); } );
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;
return { aRefDes.begin(), res.base() };
}

View File

@ -36,30 +36,35 @@
*/
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 ) )
BOOST_AUTO_TEST_CASE( GetPrefix )
{
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!
{ "", "" }, // empty
{ "U", "U" }, // no number
{ "1", "" }, // only number
{ "IC", "IC" }, // >1 char prefix, no number
{ "U1", "U" }, // single digit
{ "IC21", "IC" }, // >1 char prefix + number
{ "U10", "U" }, // double digit
{ "U1000", "U" }, // multi digit
{ "U1U2", "U1U" }, // prefix contains digit
};
for( const auto& c : cases )
{
BOOST_CHECK_EQUAL( UTIL::GetReferencePrefix( c.first ), c.second );
BOOST_TEST_CONTEXT( "Testing: " << c.first )
{
BOOST_CHECK_EQUAL( UTIL::GetReferencePrefix( c.first ), c.second );
}
}
}
#endif
struct REF_DES_COMP_CASE
{