From 7b6978c99b26953abe7361ae4ed0a07111e6bbd0 Mon Sep 17 00:00:00 2001 From: Florian Becker Date: Sat, 3 Sep 2022 12:47:23 +0200 Subject: [PATCH 1/3] Insert simplified function for string_utils #5 --- source/StringUtils.cpp | 21 +++++++++++++++++++++ source/StringUtils.h | 8 ++++++++ 2 files changed, 29 insertions(+) diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index 8b6af6f..e1cf54f 100644 --- a/source/StringUtils.cpp +++ b/source/StringUtils.cpp @@ -74,6 +74,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. From 505e1151c50173f814651cabfa9af4ec14698889 Mon Sep 17 00:00:00 2001 From: Florian Becker Date: Sat, 3 Sep 2022 13:02:20 +0200 Subject: [PATCH 2/3] Insert missing header for clang_8 build #5 --- source/StringUtils.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp index e1cf54f..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" From b4c75113f467d070c443c308f659a179fc49e745 Mon Sep 17 00:00:00 2001 From: Florian Becker Date: Sat, 3 Sep 2022 13:04:56 +0200 Subject: [PATCH 3/3] Update documentation #5 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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