From 7f4c85fb7e135f51ed27b62b3563c597bf9b19dd Mon Sep 17 00:00:00 2001 From: paulhoux Date: Fri, 29 Dec 2023 17:14:51 +0100 Subject: [PATCH 1/6] Added string utility functions to trim, filter and convert. --- include/cinder/Utilities.h | 80 +++++++++++++- src/cinder/Utilities.cpp | 206 ++++++++++++++++++++++++++++++++++++- 2 files changed, 281 insertions(+), 5 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index 586e86c7af..97dcc8555b 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -95,8 +95,86 @@ CI_API bool asciiCaseEqual( const char *a, const char *b ); //! returns equivalent of strcmp() using ASCII case-insensitive comparison CI_API int asciiCaseCmp( const char *a, const char *b ); +//! removes all whitespace (as defined by std::isspace()) from the beginning of \a str. Unicode aware. +CI_API void trimLeftInPlace( std::string &str ); +//! removes all whitespace (as defined by std::isspace()) from the beginning of \a str and returns a copy. Unicode aware. +CI_API std::string trimLeft( std::string str ); + +//! removes all whitespace (as defined by std::isspace()) from the end of \a str. Unicode aware. +CI_API void trimRightInPlace( std::string &str ); +//! removes all whitespace (as defined by std::isspace()) from the end of \a str and returns a copy. Unicode aware. +CI_API std::string trimRight( std::string str ); + +//! removes all whitespace (as defined by std::isspace()) removed from beginning and end of \a str. Unicode aware. +CI_API void trimInPlace( std::string &str ); //! returns a copy of \a str with all whitespace (as defined by std::isspace()) removed from beginning and end. Unicode aware. -CI_API std::string trim( const std::string &str ); +CI_API std::string trim( std::string str ); + +//! removes all specified \a characters from the beginning of \a str. Not Unicode-aware. +CI_API void trimLeftInPlace( std::string &str, const std::string &characters ); +//! removes all specified \a characters from the beginning of \a str. Not Unicode-aware. +CI_API std::string trimLeft( std::string str, const std::string &characters ); + +//! removes all specified \a characters from the end of \a str. Not Unicode-aware. +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. +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. +CI_API std::string filter( std::string str, const std::string &chars ); + +//! Converts the character \a c to lowercase. Not Unicode-aware. +CI_API char charToLower( const char c ); +//! Converts the character \a c to uppercase. Not Unicode-aware. +CI_API char charToUpper( const char c ); + +//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). Not Unicode-aware. +CI_API std::string toLower( std::string str ); +//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). Not Unicode-aware. +CI_API std::string toLower( std::string str, const std::locale &loc ); +//! 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 ); +//! 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, const std::locale &loc ); + +//! replaces all instances of \a find with \a replace in \a str. +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. +CI_API std::string findReplace( const std::string &find, const std::string &replace, std::string str ); + +//! returns whether character \a c is considered white space. +CI_API bool isWhiteSpace( char c ); +//! returns whether character \a c is a digit (0-9). +CI_API bool isDigit( char c ); +//! returns whether character \a c is a hexadecimal digit (0-9)+(a-f). +CI_API bool isHexDigit( char c ); +//! returns whether character \a c is alphabetic (a-z). +CI_API bool isAlpha( char c ); +//! returns whether character \a c is numeric (0-9)+(.+-eE). +CI_API bool isNumeric( char c ); + +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( int value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( unsigned value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( long value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( unsigned long value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( long long value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( unsigned long long value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( float value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( float value, int precision ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( double value ); +//! converts the value to a string without leading and trailing zeroes. +CI_API std::string valueToString( double value, int precision ); //! 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 d4f2ee3886..5755f5bdfe 100644 --- a/src/cinder/Utilities.cpp +++ b/src/cinder/Utilities.cpp @@ -171,11 +171,209 @@ int asciiCaseCmp( const char *a, const char *b ) return ((int)std::toupper(*a)) - ((int)std::toupper(*b)); } -std::string trim( const std::string &str ) +void trimLeftInPlace( std::string &str ) { - size_t wsFront = str.find_first_not_of( " \f\n\r\t\v" ); - size_t wsBack = str.find_last_not_of( " \f\n\r\t\v" ); - return wsBack <= wsFront ? std::string() : str.substr( wsFront, wsBack - wsFront + 1 ); + str.erase( str.begin(), std::find_if( str.begin(), str.end(), []( std::string::value_type ch ) { return !std::isspace( ch ); } ) ); +} + +std::string trimLeft( std::string str ) +{ + trimLeftInPlace( str ); + return str; +} + +void trimRightInPlace( std::string &str ) +{ + str.erase( std::find_if( str.rbegin(), str.rend(), []( std::string::value_type ch ) { return !std::isspace( ch ); } ).base(), str.end() ); +} + +std::string trimRight( std::string str ) +{ + trimRightInPlace( str ); + return str; +} + +void trimInPlace( std::string &str ) +{ + trimLeftInPlace( str ); + trimRightInPlace( str ); +} + +std::string trim( std::string str ) +{ + trimInPlace( str ); + return str; +} + +void trimLeftInPlace( std::string &str, const std::string &characters ) +{ + str.erase( str.begin(), std::find_if( str.begin(), str.end(), [characters]( std::string::value_type ch ) { return characters.find( ch ) == std::string::npos; } ) ); +} + +std::string trimLeft( std::string str, const std::string &characters ) +{ + trimLeftInPlace( str, characters ); + return str; +} + +void trimRightInPlace( std::string &str, const std::string &characters ) +{ + str.erase( std::find_if( str.rbegin(), str.rend(), [characters]( std::string::value_type ch ) { return characters.find( ch ) == std::string::npos; } ).base(), str.end() ); +} + +std::string trimRight( std::string str, const std::string &characters ) +{ + trimRightInPlace( str, characters ); + return str; +} + +void filterInPlace( std::string &str, const std::string &chars ) +{ + str.erase( std::remove_if( str.begin(), str.end(), [chars]( char c ) { return chars.find( c ) != std::string::npos; } ), str.end() ); +} + +std::string filter( std::string str, const std::string &chars ) +{ + filterInPlace( str, chars ); + return str; +} + +char charToLower( const char c ) +{ + if( c >= 'A' && c <= 'Z' ) + return char( c + 32 ); + return c; +} + +char charToUpper( const char c ) +{ + if( c >= 'a' && c <= 'z' ) + return char( c - 32 ); + return c; +} + +std::string toLower( std::string str ) +{ + thread_local static std::locale loc( "" ); + return toLower( std::move( str ), loc ); +} + +std::string toLower( std::string str, const std::locale &loc ) +{ + std::transform( str.begin(), str.end(), str.begin(), [loc]( unsigned char c ) { return std::tolower( c, loc ); } ); + return str; +} + +std::string toUpper( std::string str ) +{ + thread_local static std::locale loc( "" ); + return toUpper( std::move( str ), loc ); +} + +std::string toUpper( std::string str, const std::locale &loc ) +{ + std::transform( str.begin(), str.end(), str.begin(), [loc]( unsigned char c ) { return std::toupper( c, loc ); } ); + return str; +} + +void findReplaceInPlace( const std::string &find, const std::string &replace, std::string &str ) +{ + auto pos = str.find( find ); + while( pos != std::string::npos ) { + str.replace( pos, find.length(), replace ); + pos = str.find( find, pos + replace.length() ); + } +} + +std::string findReplace( const std::string &find, const std::string &replace, std::string str ) +{ + findReplaceInPlace( find, replace, str ); + return str; +} + +bool isWhiteSpace( char c ) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f'; +} + +bool isDigit( char c ) +{ + return !( c < '0' || c > '9' ); +} + +bool isHexDigit( char c ) +{ + c = charToLower( c ); + return isDigit( c ) || c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f'; +} + +bool isAlpha( char c ) +{ + c = charToLower( c ); + return !( c < 'a' || c > 'z' ); +} + +bool isNumeric( char c ) +{ + return isDigit( c ) || c == '.' || c == '-' || c == 'e' || c == 'E' || c == '+'; +} + +std::string valueToString( int value ) +{ + return std::to_string( value ); +} + +std::string valueToString( unsigned value ) +{ + return std::to_string( value ); +} + +std::string valueToString( long value ) +{ + return std::to_string( value ); +} + +std::string valueToString( unsigned long value ) +{ + return std::to_string( value ); +} + +std::string valueToString( long long value ) +{ + return std::to_string( value ); +} + +std::string valueToString( unsigned long long value ) +{ + return std::to_string( value ); +} + +std::string valueToString( float value ) +{ + std::string str = std::to_string( value ); + trimRightInPlace( str, "0.," ); + return str; +} + +std::string valueToString( float value, int precision ) +{ + std::ostringstream str; + str << std::fixed << std::setprecision( precision ) << value; + return str.str(); +} + +std::string valueToString( double value ) +{ + std::string str = std::to_string( value ); + trimRightInPlace( str, "0.," ); + return str; +} + +std::string valueToString( double value, int precision ) +{ + std::ostringstream str; + str << std::fixed << std::setprecision( precision ) << value; + return str.str(); } void sleep( float milliseconds ) From 2af6e242e5fd0a3b1624265062434be7f84dc24c Mon Sep 17 00:00:00 2001 From: paulhoux Date: Fri, 29 Dec 2023 17:33:11 +0100 Subject: [PATCH 2/6] Attempt to fix the MacOS compilation. --- include/cinder/Utilities.h | 20 ++++++++++---------- src/cinder/Utilities.cpp | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index 97dcc8555b..b3e5f9b68b 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -92,7 +92,7 @@ inline double fromString( const std::string &s ) { return atof( s.c_str() ); } CI_API bool asciiCaseEqual( const std::string &a, const std::string &b ); //! returns \c true if ASCII strings \a a and \a b are case-insensitively equal. Not Unicode-aware. CI_API bool asciiCaseEqual( const char *a, const char *b ); -//! returns equivalent of strcmp() using ASCII case-insensitive comparison +//! returns equivalent of strcmp() using ASCII case-insensitive comparison. Not Unicode-aware. CI_API int asciiCaseCmp( const char *a, const char *b ); //! removes all whitespace (as defined by std::isspace()) from the beginning of \a str. Unicode aware. @@ -120,9 +120,9 @@ 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. +//! 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. +//! 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 ); //! Converts the character \a c to lowercase. Not Unicode-aware. @@ -139,20 +139,20 @@ CI_API std::string toUpper( 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, const std::locale &loc ); -//! replaces all instances of \a find with \a replace in \a 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. +//! 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 ); -//! returns whether character \a c is considered white space. +//! returns whether character \a c is considered white space. Not Unicode-aware. CI_API bool isWhiteSpace( char c ); -//! returns whether character \a c is a digit (0-9). +//! returns whether character \a c is a digit (0-9). Not Unicode-aware. CI_API bool isDigit( char c ); -//! returns whether character \a c is a hexadecimal digit (0-9)+(a-f). +//! returns whether character \a c is a hexadecimal digit (0-9)+(a-f). Not Unicode-aware. CI_API bool isHexDigit( char c ); -//! returns whether character \a c is alphabetic (a-z). +//! returns whether character \a c is alphabetic (a-z). Not Unicode-aware. CI_API bool isAlpha( char c ); -//! returns whether character \a c is numeric (0-9)+(.+-eE). +//! returns whether character \a c is numeric (0-9)+(.+-eE). Not Unicode-aware. CI_API bool isNumeric( char c ); //! converts the value to a string without leading and trailing zeroes. diff --git a/src/cinder/Utilities.cpp b/src/cinder/Utilities.cpp index 5755f5bdfe..26328eb3ed 100644 --- a/src/cinder/Utilities.cpp +++ b/src/cinder/Utilities.cpp @@ -254,8 +254,8 @@ char charToUpper( const char c ) std::string toLower( std::string str ) { - thread_local static std::locale loc( "" ); - return toLower( std::move( str ), loc ); + std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::tolower( c ); } ); + return str; } std::string toLower( std::string str, const std::locale &loc ) @@ -266,8 +266,8 @@ std::string toLower( std::string str, const std::locale &loc ) std::string toUpper( std::string str ) { - thread_local static std::locale loc( "" ); - return toUpper( std::move( str ), loc ); + std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::toupper( c ); } ); + return str; } std::string toUpper( std::string str, const std::locale &loc ) From 7414ecf93fc7d7857e35ac672b19e0cae682bdfb Mon Sep 17 00:00:00 2001 From: paulhoux Date: Fri, 29 Dec 2023 17:47:54 +0100 Subject: [PATCH 3/6] Another attempt to fix the MacOS compilation. --- include/cinder/Utilities.h | 8 ++------ src/cinder/Utilities.cpp | 12 ------------ 2 files changed, 2 insertions(+), 18 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index b3e5f9b68b..012e83a0ce 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -130,14 +130,10 @@ CI_API char charToLower( const char c ); //! Converts the character \a c to uppercase. Not Unicode-aware. CI_API char charToUpper( const char c ); -//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). Not Unicode-aware. +//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). CI_API std::string toLower( std::string str ); -//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). Not Unicode-aware. -CI_API std::string toLower( std::string str, const std::locale &loc ); -//! returns a copy of \a str with all characters converted to uppercase (using std::toupper()). Not Unicode-aware. +//! returns a copy of \a str with all characters converted to uppercase (using std::toupper()). CI_API std::string toUpper( 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, const std::locale &loc ); //! 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 ); diff --git a/src/cinder/Utilities.cpp b/src/cinder/Utilities.cpp index 26328eb3ed..4f2134feb9 100644 --- a/src/cinder/Utilities.cpp +++ b/src/cinder/Utilities.cpp @@ -258,24 +258,12 @@ std::string toLower( std::string str ) return str; } -std::string toLower( std::string str, const std::locale &loc ) -{ - std::transform( str.begin(), str.end(), str.begin(), [loc]( unsigned char c ) { return std::tolower( c, loc ); } ); - return str; -} - std::string toUpper( std::string str ) { std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::toupper( c ); } ); return str; } -std::string toUpper( std::string str, const std::locale &loc ) -{ - std::transform( str.begin(), str.end(), str.begin(), [loc]( unsigned char c ) { return std::toupper( c, loc ); } ); - return str; -} - void findReplaceInPlace( const std::string &find, const std::string &replace, std::string &str ) { auto pos = str.find( find ); From 01e9099b44ba79df0ca7bfd7d754f72c97d6ccb3 Mon Sep 17 00:00:00 2001 From: paulhoux Date: Fri, 29 Dec 2023 17:57:49 +0100 Subject: [PATCH 4/6] Added toLowerInPlace and toUpperInPlace and tweaked comments. --- include/cinder/Utilities.h | 8 ++++++-- src/cinder/Utilities.cpp | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index 012e83a0ce..0379482302 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -130,9 +130,13 @@ CI_API char charToLower( const char c ); //! Converts the character \a c to uppercase. Not Unicode-aware. CI_API char charToUpper( const char c ); -//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). +//! converts all characters to lowercase (using std::tolower()) in \a str. Not Unicode-aware. +CI_API void toLowerInPlace( std::string &str ); +//! returns a copy of \a str with all characters converted to lowercase (using std::tolower()). Not Unicode-aware. CI_API std::string toLower( std::string str ); -//! returns a copy of \a str with all characters converted to uppercase (using std::toupper()). +//! converts all characters to uppercase (using std::toupper()) in \a str.Not Unicode-aware. +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. diff --git a/src/cinder/Utilities.cpp b/src/cinder/Utilities.cpp index 4f2134feb9..e69bec214b 100644 --- a/src/cinder/Utilities.cpp +++ b/src/cinder/Utilities.cpp @@ -252,15 +252,25 @@ char charToUpper( const char c ) return c; } -std::string toLower( std::string str ) +void toLowerInPlace( std::string &str ) { std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::tolower( c ); } ); +} + +std::string toLower( std::string str ) +{ + toLowerInPlace( str ); return str; } -std::string toUpper( std::string str ) +void toUpperInPlace( std::string &str ) { std::transform( str.begin(), str.end(), str.begin(), []( unsigned char c ) { return std::toupper( c ); } ); +} + +std::string toUpper( std::string str ) +{ + toUpperInPlace( str ); return str; } From e1b537422acfd8c23ee1a14e7fde92a3deebe45e Mon Sep 17 00:00:00 2001 From: paulhoux Date: Tue, 2 Jan 2024 11:33:35 +0100 Subject: [PATCH 5/6] Removed valueToString() in favor of ci::toString(). --- include/cinder/Utilities.h | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/include/cinder/Utilities.h b/include/cinder/Utilities.h index 0379482302..b545e4d62d 100644 --- a/include/cinder/Utilities.h +++ b/include/cinder/Utilities.h @@ -155,26 +155,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 ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( int value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( unsigned value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( long value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( unsigned long value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( long long value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( unsigned long long value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( float value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( float value, int precision ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( double value ); -//! converts the value to a string without leading and trailing zeroes. -CI_API std::string valueToString( double value, int precision ); //! Returns a stack trace (aka backtrace) where \c stackTrace()[0] == caller, \c stackTrace()[1] == caller's parent, etc CI_API std::vector stackTrace(); From 0977798f7d724e2b3aa2746ca715788df1a58933 Mon Sep 17 00:00:00 2001 From: paulhoux Date: Wed, 3 Jan 2024 11:32:01 +0100 Subject: [PATCH 6/6] 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; }