mirror of
https://github.com/4ian/GDevelop.git
synced 2025-10-15 10:19:04 +00:00
Add a gd::String::replace overload, add a test for this method
This commit is contained in:
@@ -251,16 +251,28 @@ void String::pop_back()
|
||||
m_string.erase((--end()).base(), end().base());
|
||||
}
|
||||
|
||||
String& String::replace( iterator &i1, iterator &i2, const String &str )
|
||||
String& String::replace( iterator i1, iterator i2, const String &str )
|
||||
{
|
||||
std::string strConverted = str.ToUTF8();
|
||||
|
||||
m_string.replace(i1.base(), i2.base(), strConverted);
|
||||
m_string.replace(i1.base(), i2.base(), str.m_string);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
String::iterator String::erase( String::iterator &first, String::iterator &last )
|
||||
String& String::replace( String::size_type pos, String::size_type len, const String &str )
|
||||
{
|
||||
if(pos > size())
|
||||
throw std::out_of_range("[gd::String::replace] starting pos greater than size");
|
||||
|
||||
iterator i1 = begin();
|
||||
std::advance( i1, pos );
|
||||
|
||||
iterator i2 = i1;
|
||||
std::advance( i2, len );
|
||||
|
||||
return replace( i1, i2, str );
|
||||
}
|
||||
|
||||
String::iterator String::erase( String::iterator first, String::iterator last )
|
||||
{
|
||||
return iterator( m_string.erase( first.base(), last.base() ) );
|
||||
}
|
||||
|
@@ -432,13 +432,22 @@ public:
|
||||
void pop_back();
|
||||
|
||||
/**
|
||||
* Replace the portion of the String between i1 and i2 (i2 not
|
||||
* included) by the String str.
|
||||
* Replace the portion of the String between **i1** and **i2** (**i2** not
|
||||
* included) by the String **str**.
|
||||
* \return *this
|
||||
*
|
||||
* **Iterators :** All iterators may be invalidated.
|
||||
*/
|
||||
String& replace( iterator &i1, iterator &i2, const String &str );
|
||||
String& replace( iterator i1, iterator i2, const String &str );
|
||||
|
||||
/**
|
||||
* Replace the portion of the String between **pos** and **pos** + **len**
|
||||
* (the character at **pos** + **len** is not included)
|
||||
* \return *this
|
||||
*
|
||||
* **Iterators :** All iterators may be invalidated.
|
||||
*/
|
||||
String& replace( size_type pos, size_type len, const String &str );
|
||||
|
||||
/**
|
||||
* Erase the characters between first and last (last not included).
|
||||
@@ -446,7 +455,7 @@ public:
|
||||
* \param last an iterator to the character next to the last one to remove
|
||||
* \return an iterator at the old position of the first deleted character
|
||||
*/
|
||||
iterator erase( iterator &first, iterator &last );
|
||||
iterator erase( iterator first, iterator last );
|
||||
|
||||
/**
|
||||
* \}
|
||||
|
@@ -40,6 +40,31 @@ TEST_CASE( "Utf8 String", "[common][utf8]") {
|
||||
REQUIRE_THROWS_AS( str.substr(50, 5), std::out_of_range );
|
||||
}
|
||||
|
||||
SECTION("Replace") {
|
||||
//Testing the interval version of replace
|
||||
gd::String str = u8"UTF8 a été testé !";
|
||||
|
||||
REQUIRE( str.replace(11, 5, u8"vérifié") == u8"UTF8 a été vérifié !" );
|
||||
REQUIRE( str.replace(11, gd::String::npos, u8"vraiment très testé !")
|
||||
== u8"UTF8 a été vraiment très testé !" );
|
||||
|
||||
REQUIRE_THROWS_AS( str.replace(50, 5, u8"Cela va planter."),
|
||||
std::out_of_range );
|
||||
|
||||
//Testing the iterator version of replace
|
||||
gd::String str2 = u8"UTF8 a été testé !";
|
||||
|
||||
gd::String::iterator i1 = str2.begin();
|
||||
std::advance(i1, 11);
|
||||
gd::String::iterator i2 = i1;
|
||||
std::advance(i2, 5);
|
||||
|
||||
REQUIRE( str.replace(i1, i2, u8"vérifié") == u8"UTF8 a été vérifié !" );
|
||||
|
||||
REQUIRE( str.replace(i1, str2.end(), u8"vraiment très testé !")
|
||||
== u8"UTF8 a été vraiment très testé !" );
|
||||
}
|
||||
|
||||
SECTION("Find") {
|
||||
gd::String str = u8"UTF8 a été testé !";
|
||||
|
||||
|
Reference in New Issue
Block a user