Skip to content

Commit ce395da

Browse files
Merge pull request #6 from VXAPPS/5-insert-simplified-method-for-string_utils
Insert simplified function for string_utils #5
2 parents 7c90497 + b4c7511 commit ce395da

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ make -j`nproc`
1111

1212
## Classes
1313
- **CPU** - Get CPU information.
14-
- **Double** - Less, Greater, Equal, Between, Round, Split.
14+
- **DoubleUtils** - Less, Greater, Equal, Between, Round, Split.
1515
- **Exec** - Run command and return stdout or mixed (stdout and stderr) and result code.
1616
- **Keyboard** - Check for caps lock state.
1717
- **Serial** - Serial communication class (Not for Windows).
18-
- **StringExtra** - LeftTrim, RightTrim, Trim, StartsWith, EndWith, Tokenize.
18+
- **StringUtils** - LeftTrim, RightTrim, Trim, StartsWith, EndWith, Tokenize, Simplified.
1919
- **Timing** - Measuring time, cpu and real time.
2020

2121
## Template classes

source/StringUtils.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
/* c header */
3232
#include <cstring>
3333

34+
/* stl header */
35+
#include <algorithm>
36+
3437
/* local header */
3538
#include "StringUtils.h"
3639

@@ -74,6 +77,27 @@ namespace vx::string_utils {
7477
return false;
7578
}
7679

80+
constexpr bool BothAreSpaces( char lhs, char rhs ) { return ( lhs == rhs ) && ( lhs == ' ' ); }
81+
82+
std::string simplified( std::string &_string ) {
83+
84+
/* Replace every control with a space */
85+
std::replace( std::begin( _string ), std::end( _string ), '\t', ' ');
86+
std::replace( std::begin( _string ), std::end( _string ), '\n', ' ');
87+
std::replace( std::begin( _string ), std::end( _string ), '\r', ' ');
88+
std::replace( std::begin( _string ), std::end( _string ), '\f', ' ');
89+
std::replace( std::begin( _string ), std::end( _string ), '\v', ' ');
90+
91+
/* Normalize spaces to just one */
92+
std::string::iterator new_end = std::unique( std::begin( _string ), std::end( _string ), BothAreSpaces);
93+
_string.erase( new_end, std::end( _string ) );
94+
95+
/* Trim */
96+
trim( _string );
97+
98+
return _string;
99+
}
100+
77101
std::vector<std::string> tokenize( const std::string &_string,
78102
std::string_view _separator ) {
79103

source/StringUtils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ namespace vx::string_utils {
9090
[[nodiscard]] bool endsWith( std::string_view _string,
9191
std::string_view _end );
9292

93+
/**
94+
* @brief Simplify a string.
95+
* @param _string String to simplify.
96+
* Default: Space, tabs, return, new line and form feed.
97+
* @return Simplified string.
98+
*/
99+
std::string simplified( std::string &_string );
100+
93101
/**
94102
* @brief Tokenize string by separator.
95103
* @param _string String to split.

0 commit comments

Comments
 (0)