Skip to content

Commit a9733c0

Browse files
committed
#39 - Implement method CppStringT::removeprefix()
Completed.
1 parent 76a222e commit a9733c0

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

cpp-strings/cppstrings.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ namespace pcs // i.e. "pythonic c++ strings"
626626
* The passed string specifies the set of characters to be removed.
627627
* The chars argument is not a prefix; rather, all combinations of
628628
* its values are stripped.
629+
* To remove a prefix, rather call method 'removeprefix()'.
629630
*/
630631
inline CppStringT lstrip(const CppStringT& prefix) const noexcept
631632
{
@@ -655,16 +656,30 @@ namespace pcs // i.e. "pythonic c++ strings"
655656
{
656657
const size_type sep_index = find(sep);
657658
if (sep_index == CppStringT::npos) {
658-
return std::vector<CppStringT>({ *this, CppStringT(), CppStringT() });
659+
const CppStringT empty{};
660+
return std::vector<CppStringT>({ *this, empty, empty });
659661
}
660662
else {
661663
const size_type third_index = sep_index + sep.size();
662664
const size_type third_size = this->size() - third_index + 1;
663-
return std::vector<CppStringT>({ substr(0, sep_index), sep, substr(third_index, third_size) });
665+
return std::vector<CppStringT>({ this->substr(0, sep_index), sep, this->substr(third_index, third_size) });
664666
}
665667
}
666668

667669

670+
//--- removeprefix() ----------------------------------
671+
/** \brief If the string starts with the prefix string, returns a new string with the prefix removed. Otherwise, returns a copy of the original string. */
672+
inline CppStringT removeprefix(const CppStringT& prefix) const noexcept
673+
{
674+
if (this->startswith(prefix)) {
675+
const size_type prefix_length = prefix.size();
676+
return this->substr(prefix_length, this->size - prefix_length + 1);
677+
}
678+
else
679+
return *this;
680+
}
681+
682+
668683
//--- rfind() -----------------------------------------
669684
/** Returns the highest index in the string where substring sub is found within the slice str[start:end], or -1 (i.e. 'npos') if sub is not found.
670685
*
@@ -809,6 +824,13 @@ namespace pcs // i.e. "pythonic c++ strings"
809824
}
810825

811826

827+
//--- startswith --------------------------------------
828+
inline const bool startswith(const CppStringT& prefix) const noexcept
829+
{
830+
return false;
831+
}
832+
833+
812834
//--- upper () -----------------------------------------
813835
/** \brief In-place replaces all characters of the string with their uppercase conversion. Returns a reference to string.
814836
*

0 commit comments

Comments
 (0)