Move StripTrailingZeros to kicad_string.h

This commit is contained in:
Marek Roszko 2020-10-24 11:23:19 -04:00
parent e928b2d8fd
commit 133b011124
4 changed files with 31 additions and 34 deletions

View File

@ -36,6 +36,7 @@
#include <base_units.h>
#include <common.h>
#include <kicad_string.h>
#include <math/util.h> // for KiROUND
#include <macros.h>
#include <title_block.h>
@ -205,31 +206,6 @@ wxString MessageTextFromValue( EDA_UNITS aUnits, double aValue, bool aAddUnitLab
return text;
}
/* Remove trailing 0 from a string containing a converted float number.
* the trailing 0 are removed if the mantissa has more
* than aTrailingZeroAllowed digits and some trailing 0
*/
void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
{
struct lconv * lc = localeconv();
char sep = lc->decimal_point[0];
unsigned sep_pos = aStringValue.Find( sep );
if( sep_pos > 0 )
{
// We want to keep at least aTrailingZeroAllowed digits after the separator
unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
while( aStringValue.Len() > min_len )
{
if( aStringValue.Last() == '0' )
aStringValue.RemoveLast();
else
break;
}
}
}
/* Convert a value to a string using double notation.
* For readability, the mantissa has 3 or more digits,

View File

@ -26,6 +26,7 @@
* @brief Some useful functions to handle strings.
*/
#include <clocale>
#include <macros.h>
#include <richio.h> // StrPrintf
#include <kicad_string.h>
@ -813,3 +814,25 @@ void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSpli
aStrings.Add( tmp );
}
}
void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed )
{
struct lconv* lc = localeconv();
char sep = lc->decimal_point[0];
unsigned sep_pos = aStringValue.Find( sep );
if( sep_pos > 0 )
{
// We want to keep at least aTrailingZeroAllowed digits after the separator
unsigned min_len = sep_pos + aTrailingZeroAllowed + 1;
while( aStringValue.Len() > min_len )
{
if( aStringValue.Last() == '0' )
aStringValue.RemoveLast();
else
break;
}
}
}

View File

@ -74,15 +74,6 @@ inline int Mils2mm( double x ) { return KiROUND( x * 25.4 / 1000. ); }
*/
std::string Double2Str( double aValue );
/**
* Function StripTrailingZeros
* Remove trailing 0 from a string containing a converted float number.
* The trailing 0 are removed if the mantissa has more
* than aTrailingZeroAllowed digits and some trailing 0
*/
void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
/**
* Function To_User_Unit
* convert \a aValue in internal units to the appropriate user units defined by \a aUnit.

View File

@ -310,5 +310,12 @@ inline void AccumulateDescription( wxString& aDesc, const wxString& aItem )
*/
void wxStringSplit( const wxString& aText, wxArrayString& aStrings, wxChar aSplitter );
/**
* Function StripTrailingZeros
* Remove trailing 0 from a string containing a converted float number.
* The trailing 0 are removed if the mantissa has more
* than aTrailingZeroAllowed digits and some trailing 0
*/
void StripTrailingZeros( wxString& aStringValue, unsigned aTrailingZeroAllowed = 1 );
#endif // KICAD_STRING_H_