Skip to content

Commit

Permalink
Merge pull request #6 from VXAPPS/5-insert-simplified-method-for-stri…
Browse files Browse the repository at this point in the history
…ng_utils

Insert simplified function for string_utils #5
  • Loading branch information
florianbecker authored Sep 3, 2022
2 parents 7c90497 + b4c7511 commit ce395da
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions source/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
/* c header */
#include <cstring>

/* stl header */
#include <algorithm>

/* local header */
#include "StringUtils.h"

Expand Down Expand Up @@ -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<std::string> tokenize( const std::string &_string,
std::string_view _separator ) {

Expand Down
8 changes: 8 additions & 0 deletions source/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit ce395da

Please sign in to comment.