Added String::replaceAll() and String::replace() for String type.

Added Action::on().

--HG--
branch : dev
This commit is contained in:
Martín Lucas Golini
2019-11-14 16:15:09 -03:00
parent fce51fc4d1
commit c84d528ae4
4 changed files with 32 additions and 0 deletions

View File

@@ -171,9 +171,15 @@ class EE_API String {
/** Replace all occurrences of the search string with the replacement string. */
static void replaceAll( std::string &target, const std::string& that, const std::string& with );
/** Replace all occurrences of the search string with the replacement string. */
static void replaceAll( String &target, const String& that, const String& with );
/** Replace the first ocurrence of the search string with the replacement string. */
static void replace( std::string& target, const std::string& that, const std::string& with );
/** Replace the first ocurrence of the search string with the replacement string. */
static void replace( String& target, const String& that, const String& with );
/** Removes the numbers at the end of the string */
static std::string removeNumbersAtEnd( std::string txt );

View File

@@ -50,6 +50,8 @@ class EE_API Action {
Uint32 addEventListener( const ActionType & actionType, const ActionCallback & callback );
Action * on( const ActionType & actionType, const ActionCallback & callback );
void removeEventListener( const Uint32 & callbackId );
void sendEvent( const ActionType & actionType );

View File

@@ -203,6 +203,16 @@ void String::replaceAll( std::string &target, const std::string& that, const std
}
}
void String::replaceAll( String &target, const String& that, const String& with ) {
std::string::size_type pos=0;
while( ( pos = target.find( that, pos ) ) != String::InvalidPos ) {
target.erase( pos, that.length() );
target.insert( pos, with );
pos += with.length();
}
}
void String::replace( std::string& target, const std::string& that, const std::string& with ) {
std::size_t start_pos = target.find( that );
@@ -212,6 +222,15 @@ void String::replace( std::string& target, const std::string& that, const std::s
target.replace( start_pos, that.length(), with );
}
void String::replace( String& target, const String& that, const String& with ) {
std::size_t start_pos = target.find( that );
if( start_pos == String::InvalidPos )
return;
target.replace( start_pos, that.length(), with );
}
std::string String::removeNumbersAtEnd( std::string txt ) {
while ( txt.size() && txt[ txt.size() - 1 ] >= '0' && txt[ txt.size() - 1 ] <= '9' ) {
txt.resize( txt.size() - 1 );

View File

@@ -54,6 +54,11 @@ Uint32 Action::addEventListener( const ActionType& actionType, const ActionCallb
return mNumCallBacks;
}
Action * Action::on(const Action::ActionType& actionType, const Action::ActionCallback& callback) {
addEventListener( actionType, callback );
return this;
}
void Action::removeEventListener( const Uint32& callbackId ) {
for ( auto it = mCallbacks.begin(); it != mCallbacks.end(); ++it ) {
std::map<Uint32, ActionCallback> event = it->second;