Break out getTrailingInt from MODULE

This is not logic specific to MODULE. Breaking it out to
kicad_string.h acheives:

* Slimming of the MODULE interface
* Enables reuse of the function
* Enables testing of the function

Also add a test under qa_common for this function.
This commit is contained in:
John Beard 2019-01-29 10:15:44 +00:00 committed by Seth Hillbrand
parent f85f10930a
commit dc20521cb9
7 changed files with 95 additions and 28 deletions

View File

@ -641,6 +641,29 @@ int SplitString( wxString strToSplit,
}
int GetTrailingInt( const wxString& aStr )
{
int number = 0;
int base = 1;
// Trim and extract the trailing numeric part
int index = aStr.Len() - 1;
while( index >= 0 )
{
const char chr = aStr.GetChar( index );
if( chr < '0' || chr > '9' )
break;
number += ( chr - '0' ) * base;
base *= 10;
index--;
}
return number;
}
wxString GetIllegalFileNameWxChars()
{
return FROM_UTF8( illegalFileNameChars );

View File

@ -71,7 +71,6 @@ class BOARD_ITEM : public EDA_ITEM
protected:
PCB_LAYER_ID m_Layer;
static int getTrailingInt( const wxString& aStr );
static int getNextNumberInSequence( const std::set<int>& aSeq, bool aFillSequenceGaps );
public:

View File

@ -171,6 +171,13 @@ int SplitString( wxString strToSplit,
wxString* strDigits,
wxString* strEnd );
/**
* Gets the trailing int, if any, from a string.
* @param aStr the string to check
* @return the trailing int or 0 if none found
*/
int GetTrailingInt( const wxString& aStr );
/**
* Function GetIllegalFileNameWxChars
* @return a wString object containing the illegal file name characters for all platforms.

View File

@ -95,29 +95,6 @@ void BOARD_ITEM::ViewGetLayers( int aLayers[], int& aCount ) const
}
int BOARD_ITEM::getTrailingInt( const wxString& aStr )
{
int number = 0;
int base = 1;
// Trim and extract the trailing numeric part
int index = aStr.Len() - 1;
while( index >= 0 )
{
const char chr = aStr.GetChar( index );
if( chr < '0' || chr > '9' )
break;
number += ( chr - '0' ) * base;
base *= 10;
index--;
}
return number;
}
int BOARD_ITEM::getNextNumberInSequence( const std::set<int>& aSeq, bool aFillSequenceGaps)
{
if( aSeq.empty() )

View File

@ -1309,7 +1309,7 @@ wxString MODULE::GetNextPadName( bool aFillSequenceGaps ) const
// Create a set of used pad numbers
for( D_PAD* pad = PadsList(); pad; pad = pad->Next() )
{
int padNumber = getTrailingInt( pad->GetName() );
int padNumber = GetTrailingInt( pad->GetName() );
usedNumbers.insert( padNumber );
}
@ -1343,9 +1343,8 @@ wxString MODULE::GetReferencePrefix() const
void MODULE::IncrementReference( int aDelta )
{
SetReference( wxString::Format( wxT( "%s%i" ),
GetReferencePrefix(),
getTrailingInt( GetReference() ) + aDelta ) );
SetReference( wxString::Format(
wxT( "%s%i" ), GetReferencePrefix(), GetTrailingInt( GetReference() ) + aDelta ) );
}

View File

@ -41,6 +41,7 @@ set( common_srcs
test_coroutine.cpp
test_format_units.cpp
test_hotkey_store.cpp
test_kicad_string.cpp
test_title_block.cpp
test_utf8.cpp
test_wildcards_and_files_ext.cpp

View File

@ -0,0 +1,61 @@
/*
* 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 general string functions
*/
#include <unit_test_utils/unit_test_utils.h>
// Code under test
#include <kicad_string.h>
/**
* Declare the test suite
*/
BOOST_AUTO_TEST_SUITE( KicadString )
/**
* Test the #GetTrailingInt method.
*/
BOOST_AUTO_TEST_CASE( TrailingInt )
{
using CASE = std::pair<std::string, int>;
const std::vector<CASE> cases = {
{ "", 0 }, { "foo", 0 }, // no int
{ "0", 0 }, // only int
{ "42", 42 }, // only int
{ "1001", 1001 }, // only int
{ "Foo42", 42 }, { "12Foo42", 42 }, // only the trailing
{ "12Foo4.2", 2 }, // no dots
};
for( const auto& c : cases )
{
BOOST_CHECK_EQUAL( GetTrailingInt( c.first ), c.second );
}
}
BOOST_AUTO_TEST_SUITE_END()