Skip to content

Commit

Permalink
Tested findReplaceInPlace with UTF-8 encoded strings. That worked. …
Browse files Browse the repository at this point in the history
…Also tweaked the `filterInPlace` function and comments.
  • Loading branch information
paulhoux committed Jan 3, 2024
1 parent e1b5374 commit 0977798
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
22 changes: 13 additions & 9 deletions include/cinder/Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,15 @@ CI_API void trimRightInPlace( std::string &str, const std::string &characters );
//! removes all specified \a characters from the end of \a str. Not Unicode-aware.
CI_API std::string trimRight( std::string str, const std::string &characters );

//! filters all occurrences of any of \a chars in \a str. Not Unicode-aware.
CI_API void filterInPlace( std::string &str, const std::string &chars );
//! returns a copy of \a str with all occurrences of any of \a chars filtered out. Not Unicode-aware.
CI_API std::string filter( std::string str, const std::string &chars );
//! removes all specified \a characters from \a str. Not Unicode-aware.
CI_API void trimInPlace( std::string &str, const std::string &characters );
//! removes all specified \a characters from \a str. Not Unicode-aware.
CI_API std::string trim( std::string str, const std::string &characters );

//! removes all occurrences of any of \a characters in \a str. Not Unicode-aware.
CI_API void filterInPlace( std::string &str, const std::string &characters );
//! returns a copy of \a str with all occurrences of any of \a characters removed. Not Unicode-aware.
CI_API std::string filter( std::string str, const std::string &characters );

//! Converts the character \a c to lowercase. Not Unicode-aware.
CI_API char charToLower( const char c );
Expand All @@ -139,10 +144,10 @@ CI_API void toUpperInPlace( std::string &str );
//! returns a copy of \a str with all characters converted to uppercase (using std::toupper()). Not Unicode-aware.
CI_API std::string toUpper( std::string str );

//! replaces all instances of \a find with \a replace in \a str. Not Unicode-aware.
CI_API void findReplaceInPlace( const std::string &find, const std::string &replace, std::string &str );
//! replaces all instances of \a find with \a replace in \a str and returns a copy. Not Unicode-aware.
CI_API std::string findReplace( const std::string &find, const std::string &replace, std::string str );
//! replaces all instances of \a find with \a replace in \a str. Unicode-aware.
CI_API void findReplaceInPlace( std::string &str, const std::string &find, const std::string &replace );
//! replaces all instances of \a find with \a replace in \a str and returns a copy. Unicode-aware.
CI_API std::string findReplace( std::string str, const std::string &find, const std::string &replace );

//! returns whether character \a c is considered white space. Not Unicode-aware.
CI_API bool isWhiteSpace( char c );
Expand All @@ -155,7 +160,6 @@ CI_API bool isAlpha( char c );
//! returns whether character \a c is numeric (0-9)+(.+-eE). Not Unicode-aware.
CI_API bool isNumeric( char c );


//! Returns a stack trace (aka backtrace) where \c stackTrace()[0] == caller, \c stackTrace()[1] == caller's parent, etc
CI_API std::vector<std::string> stackTrace();

Expand Down
26 changes: 19 additions & 7 deletions src/cinder/Utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,26 @@ std::string trimRight( std::string str, const std::string &characters )
return str;
}

void filterInPlace( std::string &str, const std::string &chars )
void trimInPlace( std::string &str, const std::string &characters )
{
str.erase( std::remove_if( str.begin(), str.end(), [chars]( char c ) { return chars.find( c ) != std::string::npos; } ), str.end() );
trimLeftInPlace( str, characters );
trimRightInPlace( str, characters );
}

std::string trim( std::string str, const std::string &characters )
{
trimInPlace( str, characters );
return str;
}

void filterInPlace( std::string &str, const std::string &characters )
{
str.erase( std::remove_if( str.begin(), str.end(), [characters]( char c ) { return characters.find( c ) != std::string::npos; } ), str.end() );
}

std::string filter( std::string str, const std::string &chars )
std::string filter( std::string str, const std::string &characters )
{
filterInPlace( str, chars );
filterInPlace( str, characters );
return str;
}

Expand Down Expand Up @@ -274,7 +286,7 @@ std::string toUpper( std::string str )
return str;
}

void findReplaceInPlace( const std::string &find, const std::string &replace, std::string &str )
void findReplaceInPlace( std::string &str, const std::string &find, const std::string &replace )
{
auto pos = str.find( find );
while( pos != std::string::npos ) {
Expand All @@ -283,9 +295,9 @@ void findReplaceInPlace( const std::string &find, const std::string &replace, st
}
}

std::string findReplace( const std::string &find, const std::string &replace, std::string str )
std::string findReplace( std::string str, const std::string &find, const std::string &replace )
{
findReplaceInPlace( find, replace, str );
findReplaceInPlace( str, find, replace );
return str;
}

Expand Down

0 comments on commit 0977798

Please sign in to comment.