diff --git a/README.md b/README.md index 0314105..2ba9012 100644 --- a/README.md +++ b/README.md @@ -11,11 +11,11 @@ make -j`nproc` ## Classes - **CPU** - Get CPU information. -- **Double** - Less, Greater, Equal, Between, Round, Split. +- **DoubleUtils** - Less, Greater, Equal, Between, Round, Split. - **Exec** - Run command and return stdout or mixed (stdout and stderr) and result code. - **Keyboard** - Check for caps lock state. - **Serial** - Serial communication class (Not for Windows). -- **StringExtra** - LeftTrim, RightTrim, Trim, StartsWith, EndWith, Tokenize. +- **StringUtils** - LeftTrim, RightTrim, Trim, StartsWith, EndWith, Tokenize, Simplified. - **Timing** - Measuring time, cpu and real time. ## Template classes diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index 8b6af6f..ea3c53d 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -31,6 +31,9 @@ /* c header */ #include +/* stl header */ +#include + /* local header */ #include "StringUtils.h" @@ -74,6 +77,27 @@ namespace vx::string_utils { return false; } + constexpr bool BothAreSpaces( char lhs, char rhs ) { return ( lhs == rhs ) && ( lhs == ' ' ); } + + std::string simplified( std::string &_string ) { + + /* Replace every control with a space */ + std::replace( std::begin( _string ), std::end( _string ), '\t', ' '); + std::replace( std::begin( _string ), std::end( _string ), '\n', ' '); + std::replace( std::begin( _string ), std::end( _string ), '\r', ' '); + std::replace( std::begin( _string ), std::end( _string ), '\f', ' '); + std::replace( std::begin( _string ), std::end( _string ), '\v', ' '); + + /* Normalize spaces to just one */ + std::string::iterator new_end = std::unique( std::begin( _string ), std::end( _string ), BothAreSpaces); + _string.erase( new_end, std::end( _string ) ); + + /* Trim */ + trim( _string ); + + return _string; + } + std::vector tokenize( const std::string &_string, std::string_view _separator ) { diff --git a/source/StringUtils.h b/source/StringUtils.h index e5ab82e..f13aeb3 100644 --- a/source/StringUtils.h +++ b/source/StringUtils.h @@ -90,6 +90,14 @@ namespace vx::string_utils { [[nodiscard]] bool endsWith( std::string_view _string, std::string_view _end ); + /** + * @brief Simplify a string. + * @param _string String to simplify. + * Default: Space, tabs, return, new line and form feed. + * @return Simplified string. + */ + std::string simplified( std::string &_string ); + /** * @brief Tokenize string by separator. * @param _string String to split.