diff --git a/common/string.cpp b/common/string.cpp index 4fb9fe65e6..ed2ac00e9c 100644 --- a/common/string.cpp +++ b/common/string.cpp @@ -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 ); diff --git a/include/class_board_item.h b/include/class_board_item.h index 92b01463ac..47c67d16b9 100644 --- a/include/class_board_item.h +++ b/include/class_board_item.h @@ -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& aSeq, bool aFillSequenceGaps ); public: diff --git a/include/kicad_string.h b/include/kicad_string.h index d93b49cabe..4b61988dae 100644 --- a/include/kicad_string.h +++ b/include/kicad_string.h @@ -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. diff --git a/pcbnew/class_board_item.cpp b/pcbnew/class_board_item.cpp index b9190edc8f..9af3ee7569 100644 --- a/pcbnew/class_board_item.cpp +++ b/pcbnew/class_board_item.cpp @@ -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& aSeq, bool aFillSequenceGaps) { if( aSeq.empty() ) diff --git a/pcbnew/class_module.cpp b/pcbnew/class_module.cpp index 87d2b645ff..5c5d2d5ac5 100644 --- a/pcbnew/class_module.cpp +++ b/pcbnew/class_module.cpp @@ -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 ) ); } diff --git a/qa/common/CMakeLists.txt b/qa/common/CMakeLists.txt index 5adca44353..115982f8eb 100644 --- a/qa/common/CMakeLists.txt +++ b/qa/common/CMakeLists.txt @@ -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 diff --git a/qa/common/test_kicad_string.cpp b/qa/common/test_kicad_string.cpp new file mode 100644 index 0000000000..1a9defcbe8 --- /dev/null +++ b/qa/common/test_kicad_string.cpp @@ -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 + +// Code under test +#include + +/** + * Declare the test suite + */ +BOOST_AUTO_TEST_SUITE( KicadString ) + +/** + * Test the #GetTrailingInt method. + */ +BOOST_AUTO_TEST_CASE( TrailingInt ) +{ + using CASE = std::pair; + + const std::vector 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() \ No newline at end of file