From 0977798f7d724e2b3aa2746ca715788df1a58933 Mon Sep 17 00:00:00 2001 From: paulhoux Date: Wed, 3 Jan 2024 11:32:01 +0100 Subject: [PATCH] Tested `findReplaceInPlace` with UTF-8 encoded strings. That worked. Also tweaked the `filterInPlace` function and comments. --- include/cinder/Utilities.h | 22 +++++++++++++--------- src/cinder/Utilities.cpp | 26 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index b545e4d62d..b66eee353f 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -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 ); @@ -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 ); @@ -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 stackTrace(); diff --git a/src/cinder/Utilities.cpp b/src/cinder/Utilities.cpp index e69bec214b..0c30ea2dcf 100644 --- a/src/cinder/Utilities.cpp +++ b/src/cinder/Utilities.cpp @@ -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; } @@ -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 ) { @@ -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; }