diff --git a/include/ACGL/Utils/StringHelpers.hh b/include/ACGL/Utils/StringHelpers.hh index 307549f6eacfc4c1a722c03d4868c06138484bb3..6c1f41cc65f5d983efd994a2a6023eb5ef54baa6 100644 --- a/include/ACGL/Utils/StringHelpers.hh +++ b/include/ACGL/Utils/StringHelpers.hh @@ -48,6 +48,9 @@ namespace StringHelpers //! strips a string of all leading and trailing characters out of a given list (but leaves the ones in between) std::string stripOfCharacters( const std::string &_string, const std::string &_charsToStrip ); + //! converts an int to a string but adds leading zeros (e.g. _maxPadding = 2, 1 -> 01) + std::string intToString( const int _number, const int _maxPadding = 0 ); + //! Convert a primitive type to a string (e.g. string s = toString(1.5f)), also supports some GLM types (but not complete) template std::string toString(const T& _t); diff --git a/src/ACGL/Utils/StringHelpers.cc b/src/ACGL/Utils/StringHelpers.cc index 48ee48418de8d34ad0cbcd6880574ca554e34954..073f90f7a3dc0b26ab5a9a26764a1e020c7f72b3 100644 --- a/src/ACGL/Utils/StringHelpers.cc +++ b/src/ACGL/Utils/StringHelpers.cc @@ -7,6 +7,7 @@ #include #include #include // transform +#include // setfill namespace ACGL{ namespace Utils{ @@ -109,6 +110,17 @@ namespace StringHelpers return stripOfCharacters( _string, whitespaces ); } + std::string intToString( const int _number, const int _maxPadding ) + { + std::ostringstream stream; + if (_maxPadding > 0) { + stream << std::setfill('0') << std::setw(_maxPadding) << _number; + } else { + stream << _number; + } + return stream.str(); + } + template std::string toString(const T& _t) {