From 133b011124f20bdeb17e47304546e0fb24029589 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sat, 24 Oct 2020 11:23:19 -0400 Subject: [PATCH] Move StripTrailingZeros to kicad_string.h --- common/base_units.cpp | 26 +------------------------- common/string.cpp | 23 +++++++++++++++++++++++ include/base_units.h | 9 --------- include/kicad_string.h | 7 +++++++ 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/common/base_units.cpp b/common/base_units.cpp index 5c70bb2f3d..c21bc5768e 100644 --- a/common/base_units.cpp +++ b/common/base_units.cpp @@ -36,6 +36,7 @@ #include #include +#include #include // for KiROUND #include #include @@ -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, diff --git a/common/string.cpp b/common/string.cpp index bb5c6e1feb..cce77c880b 100644 --- a/common/string.cpp +++ b/common/string.cpp @@ -26,6 +26,7 @@ * @brief Some useful functions to handle strings. */ +#include #include #include // StrPrintf #include @@ -812,4 +813,26 @@ 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; + } + } } \ No newline at end of file diff --git a/include/base_units.h b/include/base_units.h index c08f01350b..b325594693 100644 --- a/include/base_units.h +++ b/include/base_units.h @@ -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. diff --git a/include/kicad_string.h b/include/kicad_string.h index fc5437a7cb..c3eec95961 100644 --- a/include/kicad_string.h +++ b/include/kicad_string.h @@ -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_