Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pystring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,31 @@ const std::string colon = ":";
return os.str();
}

//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string removeprefix( const std::string & str, const std::string & prefix )
{
if (pystring::startswith(str, prefix))
{
return str.substr(prefix.length());
}

return str;
}

//////////////////////////////////////////////////////////////////////////////////////////////
///
///
std::string removesuffix( const std::string & str, const std::string & suffix )
{
if (pystring::endswith(str, suffix))
{
return str.substr(0, str.length() - suffix.length());
}

return str;
}


namespace os
Expand Down
12 changes: 12 additions & 0 deletions pystring.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ namespace pystring
///
void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result );

//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief If str starts with prefix return a copy of the string with prefix at the start
/// removed otherwise return an unmodified copy of the string.
///
std::string removeprefix( const std::string & str, const std::string & prefix );

//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief If str ends with suffix return a copy of the string with suffix at the end removed
/// otherwise return an unmodified copy of the string.
///
std::string removesuffix( const std::string & str, const std::string & suffix );

//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with all occurrences of substring old replaced by new. If
/// the optional argument count is given, only the first count occurrences are replaced.
Expand Down
12 changes: 12 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ PYSTRING_ADD_TEST(pystring, rfind)
PYSTRING_CHECK_EQUAL(pystring::rfind("abcabcabc", "abc", 6, 8), -1);
}

PYSTRING_ADD_TEST(pystring, removeprefix)
{
PYSTRING_CHECK_EQUAL(pystring::removeprefix("abcdef", "abc"), "def");
PYSTRING_CHECK_EQUAL(pystring::removeprefix("abcdef", "bcd"), "abcdef");
}

PYSTRING_ADD_TEST(pystring, removesuffix)
{
PYSTRING_CHECK_EQUAL(pystring::removesuffix("abcdef", "def"), "abc");
PYSTRING_CHECK_EQUAL(pystring::removesuffix("abcdef", "cde"), "abcdef");
}

PYSTRING_ADD_TEST(pystring, replace)
{
PYSTRING_CHECK_EQUAL(pystring::replace("abcdef", "foo", "bar"), "abcdef");
Expand Down