Skip to content

Commit

Permalink
Merge pull request #30 from niclasr/removestarfix
Browse files Browse the repository at this point in the history
Add removeprefix and removesuffix to pystring
  • Loading branch information
grdanny authored Jul 23, 2023
2 parents 7d16bc8 + e8e34d0 commit 76a2024
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pystring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,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 @@ -157,6 +157,18 @@ namespace pystring
return 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 @@ -98,6 +98,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

0 comments on commit 76a2024

Please sign in to comment.