Skip to content

Commit 76a2024

Browse files
authored
Merge pull request #30 from niclasr/removestarfix
Add removeprefix and removesuffix to pystring
2 parents 7d16bc8 + e8e34d0 commit 76a2024

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

pystring.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,31 @@ const std::string colon = ":";
10431043
return os.str();
10441044
}
10451045

1046+
//////////////////////////////////////////////////////////////////////////////////////////////
1047+
///
1048+
///
1049+
std::string removeprefix( const std::string & str, const std::string & prefix )
1050+
{
1051+
if (pystring::startswith(str, prefix))
1052+
{
1053+
return str.substr(prefix.length());
1054+
}
1055+
1056+
return str;
1057+
}
1058+
1059+
//////////////////////////////////////////////////////////////////////////////////////////////
1060+
///
1061+
///
1062+
std::string removesuffix( const std::string & str, const std::string & suffix )
1063+
{
1064+
if (pystring::endswith(str, suffix))
1065+
{
1066+
return str.substr(0, str.length() - suffix.length());
1067+
}
1068+
1069+
return str;
1070+
}
10461071

10471072

10481073
namespace os

pystring.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,18 @@ namespace pystring
157157
return result;
158158
}
159159

160+
//////////////////////////////////////////////////////////////////////////////////////////////
161+
/// @brief If str starts with prefix return a copy of the string with prefix at the start
162+
/// removed otherwise return an unmodified copy of the string.
163+
///
164+
std::string removeprefix( const std::string & str, const std::string & prefix );
165+
166+
//////////////////////////////////////////////////////////////////////////////////////////////
167+
/// @brief If str ends with suffix return a copy of the string with suffix at the end removed
168+
/// otherwise return an unmodified copy of the string.
169+
///
170+
std::string removesuffix( const std::string & str, const std::string & suffix );
171+
160172
//////////////////////////////////////////////////////////////////////////////////////////////
161173
/// @brief Return a copy of the string with all occurrences of substring old replaced by new. If
162174
/// the optional argument count is given, only the first count occurrences are replaced.

test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ PYSTRING_ADD_TEST(pystring, rfind)
9898
PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "abc", 6, 8), -1);
9999
}
100100

101+
PYSTRING_ADD_TEST(pystring, removeprefix)
102+
{
103+
PYSTRING_CHECK_EQUAL(pystring::removeprefix("abcdef", "abc"), "def");
104+
PYSTRING_CHECK_EQUAL(pystring::removeprefix("abcdef", "bcd"), "abcdef");
105+
}
106+
107+
PYSTRING_ADD_TEST(pystring, removesuffix)
108+
{
109+
PYSTRING_CHECK_EQUAL(pystring::removesuffix("abcdef", "def"), "abc");
110+
PYSTRING_CHECK_EQUAL(pystring::removesuffix("abcdef", "cde"), "abcdef");
111+
}
112+
101113
PYSTRING_ADD_TEST(pystring, replace)
102114
{
103115
PYSTRING_CHECK_EQUAL(pystring::replace("abcdef", "foo", "bar"), "abcdef");

0 commit comments

Comments
 (0)