Skip to content

Commit a66f7b5

Browse files
committed
#47 - Implement method CppStringT::rstrip()
Completed.
1 parent 861e82c commit a66f7b5

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

cpp-strings/cppstrings.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ namespace pcs // i.e. "pythonic c++ strings"
621621
{
622622
return value_type(std::tolower(ch));
623623
}
624-
624+
625625

626626
//--- lstrip() ----------------------------------------
627627
/** \brief Returns a copy of the string with leading characters removed.
@@ -1029,6 +1029,32 @@ namespace pcs // i.e. "pythonic c++ strings"
10291029
}
10301030

10311031

1032+
//--- rstrip() ----------------------------------------
1033+
/** \brief Returns a copy of the string with trailing characters removed.
1034+
*
1035+
* The passed string specifies the set of characters to be removed.
1036+
* The chars argument is not a prefix; rather, all combinations of
1037+
* its values are stripped.
1038+
* To remove a suffix, rather call method 'removesuffix()'.
1039+
*/
1040+
inline CppStringT rstrip(const CppStringT& prefix) const noexcept
1041+
{
1042+
for (auto it = this->crbegin(); it != this->crend(); ++it)
1043+
if (std::none_of(prefix.cbegin(), prefix.cend(), [it](const value_type ch) { *it == ch; }))
1044+
return CppStringT(this->cbegin(), it);
1045+
return CppStringT();
1046+
}
1047+
1048+
/** \brief Returns a copy of the string with trailing whitespaces removed. */
1049+
inline CppStringT rstrip() const noexcept
1050+
{
1051+
for (auto it = this->crbegin(); it != this->crend(); ++it)
1052+
if (*it != value_type(' '))
1053+
return CppStringT(this->cbegin(), it);
1054+
return CppStringT();
1055+
}
1056+
1057+
10321058
//--- split() -----------------------------------------
10331059
inline std::vector<CppStringT> split() const noexcept
10341060
{

0 commit comments

Comments
 (0)