Skip to content

Commit b235aef

Browse files
committed
#46 - Implement method CppStringT::rsplit()
Completed.
1 parent 6d3729e commit b235aef

File tree

1 file changed

+99
-21
lines changed

1 file changed

+99
-21
lines changed

cpp-strings/cppstrings.h

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -736,27 +736,6 @@ namespace pcs // i.e. "pythonic c++ strings"
736736
}
737737

738738

739-
//--- rpartition() -------------------------------------
740-
/** Split the string at the last occurrence of sep, and returns a 3-items vector containing the part before the separator, the separator itself, and the part after the separator.
741-
*
742-
* If the separator is not found, returns a 3-items vector
743-
* containing the string itself, followed by two empty strings.
744-
*/
745-
std::vector<CppStringT> rpartition(const CppStringT& sep) const noexcept
746-
{
747-
const size_type sep_index = rfind(sep);
748-
if (sep_index == CppStringT::npos) {
749-
const CppStringT empty{};
750-
return std::vector<CppStringT>({ *this, empty, empty });
751-
}
752-
else {
753-
const size_type third_index = sep_index + sep.size();
754-
const size_type third_size = this->size() - third_index + 1;
755-
return std::vector<CppStringT>({ this->substr(0, sep_index), sep, this->substr(third_index, third_size) });
756-
}
757-
}
758-
759-
760739
//--- rfind() -----------------------------------------
761740
/** 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.
762741
*
@@ -908,6 +887,105 @@ namespace pcs // i.e. "pythonic c++ strings"
908887
}
909888

910889

890+
//--- rpartition() -------------------------------------
891+
/** Split the string at the last occurrence of sep, and returns a 3-items vector containing the part before the separator, the separator itself, and the part after the separator.
892+
*
893+
* If the separator is not found, returns a 3-items vector
894+
* containing the string itself, followed by two empty strings.
895+
*/
896+
std::vector<CppStringT> rpartition(const CppStringT& sep) const noexcept
897+
{
898+
const size_type sep_index = rfind(sep);
899+
if (sep_index == CppStringT::npos) {
900+
const CppStringT empty{};
901+
return std::vector<CppStringT>({ *this, empty, empty });
902+
}
903+
else {
904+
const size_type third_index = sep_index + sep.size();
905+
const size_type third_size = this->size() - third_index + 1;
906+
return std::vector<CppStringT>({ this->substr(0, sep_index), sep, this->substr(third_index, third_size) });
907+
}
908+
}
909+
910+
911+
//--- rsplit() ----------------------------------------
912+
/** \brief Returns a vector of the words in the string, as seperated with whitespace strings. */
913+
inline std::vector<CppStringT> rsplit() const noexcept
914+
{
915+
return split();
916+
}
917+
918+
/** \brief Returns a vector of the words in the string, using sep as the delimiter string. */
919+
inline std::vector<CppStringT> rsplit(const CppStringT& sep) const noexcept
920+
{
921+
return split(sep);
922+
}
923+
924+
/** \brief Returns a vector of the words in the string, as seperated with whitespace strings.
925+
*
926+
* At most maxsplit splits are done, the rightmost ones.
927+
*/
928+
std::vector<CppStringT> rsplit(const size_type maxsplit) const noexcept
929+
{
930+
if (maxsplit == 0)
931+
return *this;
932+
933+
constexpr CppStringT spc2(" ");
934+
constexpr CppStringT spc(value_type(' '));
935+
CppStringT tmp = *this;
936+
while (tmp.contains(spc2))
937+
tmp = tmp.replace(spc2, spc);
938+
939+
return this->rsplit(spc, maxsplit);
940+
}
941+
942+
/** \brief Returns a vector of the words in the string, using sep as the delimiter string.
943+
*
944+
* At most maxsplit splits are done, the rightmost ones. Except
945+
* for splitting from the right, rsplit() behaves like split()].
946+
*/
947+
std::vector<CppStringT> rsplit(const CppStringT& sep, const size_type maxsplit) const noexcept
948+
{
949+
std::vector<CppStringT> res{};
950+
951+
std::vector<CppStringT> indexes{};
952+
CppStringT tmp = *this;
953+
size_type count = maxsplit;
954+
size_type index;
955+
while ((index = tmp.rfind(sep)) != CppStringT::npos && count > 0) {
956+
indexes.insert(indexes.begin(), index);
957+
if (index == 0)
958+
break;
959+
tmp = tmp.substr(0, index-1);
960+
count--;
961+
}
962+
963+
if (indexes.size() == 0)
964+
res.push_back(*this);
965+
else {
966+
index = 0;
967+
for (const size_type ndx: indexes) {
968+
res.push_back(this->substr(index, ndx - index));
969+
index = ndx + 1;
970+
}
971+
res.push_back(this->substr(index, this->size() - index));
972+
}
973+
return res;
974+
}
975+
976+
977+
//--- split() -----------------------------------------
978+
inline std::vector<CppStringT> split() const noexcept
979+
{
980+
return std::vector<CppStringT>();
981+
}
982+
983+
/** \brief Returns a vector of the words in the string, using sep as the delimiter string. */
984+
inline std::vector<CppStringT> split(const CppStringT& sep) const noexcept
985+
{
986+
return std::vector<CppStringT>();
987+
}
988+
911989

912990
//--- title() -----------------------------------------
913991
/** \brief Returns a titlecased copy of the string where words start with an uppercase character and the remaining characters are lowercase. */

0 commit comments

Comments
 (0)