Skip to content

Commit 95777d7

Browse files
committed
Add erase_all()
1 parent b6e5677 commit 95777d7

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ The following table presents types, values and simplified, short prototypes of t
132132
|   | string **capitalize**(string_view sv) | string transformed to start with capital |
133133
|   | string **substring**(string_view sv, size_t pos \[, size_t count\]); | substring starting at given position of given length, default up to end |
134134
|   | string **erase**(string_view sv, size_t pos \[, size_t count\]) | string with substring at given position of given length removed, default up to end |
135+
|   | string **erase_all**(string_view sv, string_view what) | string with all occurrences of 'what' removed |
135136
|   | string **insert**(string_view sv, size_t pos, string_view what) | string with substring 'what' inserted at given position |
136137
|   | string **replace**(string_view sv, size_t pos, size_t length, string_view with) | string with substring pos to pos+length replaced with 'with' |
137138
|   | string **replace_all**(string_view sv, string_view what, string_view with) | string with all occurrences of 'what' replaced with 'with' |
@@ -233,6 +234,7 @@ to_uppercase: string transformed to uppercase
233234
append: string with second string concatenated to first string
234235
substring: substring starting at given position of given length, default up to end
235236
erase: string with substring at given position of given length removed - default up to end
237+
erase_all: string with all occurrences of substring removed
236238
insert: string with substring inserted at given position
237239
replace: string with substring given by position and length replaced
238240
replace_all: string with all occurrences of substring replaced

include/nonstd/string.hpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,33 @@ string_nodiscard std::basic_string<CharT> to_case( std::basic_string<CharT> text
10341034
return strip_left( strip_right( text, set ), set ); \
10351035
}
10361036

1037-
// insert()
1037+
// erase_all()
1038+
1039+
namespace string {
1040+
namespace detail {
1041+
1042+
template< typename CharT >
1043+
string_nodiscard std::basic_string<CharT>
1044+
erase_all( std17::basic_string_view<CharT> text, std17::basic_string_view<CharT> what )
1045+
{
1046+
std::basic_string<CharT> result( text );
1047+
1048+
for ( auto pos = detail::find_first<CharT>( result, what, 0 ) ;; )
1049+
{
1050+
pos = detail::find_first<CharT>( result, what, pos );
1051+
1052+
if ( pos == std::basic_string<CharT>::npos )
1053+
break;
1054+
1055+
result.erase( pos, what.length() );
1056+
}
1057+
return result;
1058+
}
1059+
1060+
} // detail
1061+
} // namespace string
1062+
1063+
// erase()
10381064

10391065
#define string_MK_ERASE(CharT) \
10401066
string_nodiscard inline std::basic_string<CharT> \
@@ -1046,6 +1072,20 @@ string_nodiscard std::basic_string<CharT> to_case( std::basic_string<CharT> text
10461072
return to_string( text ).erase( pos, len ); \
10471073
}
10481074

1075+
// erase_all()
1076+
1077+
#define string_MK_ERASE_ALL(CharT) \
1078+
string_nodiscard inline std::basic_string<CharT> \
1079+
erase_all( \
1080+
std17::basic_string_view<CharT> text \
1081+
, std17::basic_string_view<CharT> what ) \
1082+
{ \
1083+
return detail::erase_all( text, what ); \
1084+
}
1085+
1086+
// erase_first()
1087+
// erase_last()
1088+
10491089
// insert()
10501090

10511091
#define string_MK_INSERT(CharT) \
@@ -1721,6 +1761,7 @@ string_MK_STARTS_WITH_CHAR ( char )
17211761
string_MK_ENDS_WITH ( char )
17221762
string_MK_ENDS_WITH_CHAR ( char )
17231763
string_MK_ERASE ( char )
1764+
string_MK_ERASE_ALL ( char )
17241765
string_MK_INSERT ( char )
17251766
string_MK_REPLACE ( char )
17261767
string_MK_REPLACE_ALL ( char )
@@ -1765,6 +1806,7 @@ string_MK_FIND_LAST_OF ( wchar_t )
17651806
string_MK_FIND_FIRST_NOT_OF( wchar_t )
17661807
string_MK_FIND_LAST_NOT_OF ( wchar_t )
17671808
string_MK_ERASE ( wchar_t )
1809+
string_MK_ERASE_ALL ( wchar_t )
17681810
string_MK_INSERT ( wchar_t )
17691811
string_MK_REPLACE ( wchar_t )
17701812
string_MK_REPLACE_ALL ( wchar_t )
@@ -1809,6 +1851,7 @@ string_MK_FIND_LAST_OF ( char8_t )
18091851
string_MK_FIND_FIRST_NOT_OF( char8_t )
18101852
string_MK_FIND_LAST_NOT_OF ( char8_t )
18111853
string_MK_ERASE ( char8_t )
1854+
string_MK_ERASE_ALL ( char8_t )
18121855
string_MK_INSERT ( char8_t )
18131856
string_MK_REPLACE ( char8_t )
18141857
string_MK_REPLACE_ALL ( char8_t )
@@ -1853,6 +1896,7 @@ string_MK_FIND_LAST_OF ( char16_t )
18531896
string_MK_FIND_FIRST_NOT_OF( char16_t )
18541897
string_MK_FIND_LAST_NOT_OF ( char16_t )
18551898
string_MK_ERASE ( char16_t )
1899+
string_MK_ERASE_ALL ( char16_t )
18561900
string_MK_INSERT ( char16_t )
18571901
string_MK_REPLACE ( char16_t )
18581902
string_MK_REPLACE_ALL ( char16_t )
@@ -1897,6 +1941,7 @@ string_MK_FIND_LAST_OF ( char32_t )
18971941
string_MK_FIND_FIRST_NOT_OF( char32_t )
18981942
string_MK_FIND_LAST_NOT_OF ( char32_t )
18991943
string_MK_ERASE ( char32_t )
1944+
string_MK_ERASE_ALL ( char32_t )
19001945
string_MK_INSERT ( char32_t )
19011946
string_MK_REPLACE ( char32_t )
19021947
string_MK_REPLACE_ALL ( char32_t )
@@ -1941,6 +1986,7 @@ string_MK_SPLIT_LEFT_STRING( char32_t )
19411986
#undef string_MK_FIND_FIRST_NOT_OF
19421987
#undef string_MK_FIND_LAST_NOT_OF
19431988
#undef string_MK_ERASE
1989+
#undef string_MK_ERASE_ALL
19441990
#undef string_MK_INSERT
19451991
#undef string_MK_REPLACE
19461992
#undef string_MK_REPLACE_ALL

test/string.t.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@ CASE( "erase: string with substring at given position of given length removed -
374374
EXPECT( erase(std17::string_view("abcxyz"), 3 ) == "abc" );
375375
}
376376

377+
// erase_all():
378+
379+
CASE( "erase_all: string with all occurrences of substring removed" )
380+
{
381+
EXPECT( erase_all("abcxyzabcxyzabc", "xyz") == "abcabcabc" );
382+
EXPECT( erase_all(std::string("abcxyzabcxyzabc"), std::string("xyz")) == "abcabcabc" );
383+
EXPECT( erase_all(std17::string_view("abcxyzabcxyzabc"), std17::string_view("xyz")) == "abcabcabc" );
384+
}
385+
377386
// insert():
378387

379388
CASE( "insert: string with substring inserted at given position" )

0 commit comments

Comments
 (0)