Skip to content

Commit 76a222e

Browse files
committed
#38 - Implement method CppStringT::partition()
Completed.
1 parent b6b54d6 commit 76a222e

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

cpp-strings/cppstrings.h

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <cctype>
2626
#include <cwctype>
2727
#include <format>
28+
#include <map>
2829
#include <span>
2930
#include <stdexcept>
3031
#include <string_view>
@@ -620,10 +621,9 @@ namespace pcs // i.e. "pythonic c++ strings"
620621

621622

622623
//--- lstrip() ----------------------------------------
623-
/** \brief Return a copy of the string with leading characters removed.
624+
/** \brief Returns a copy of the string with leading characters removed.
624625
*
625626
* The passed string specifies the set of characters to be removed.
626-
* If omitted, the chars argument defaults to removing whitespace.
627627
* The chars argument is not a prefix; rather, all combinations of
628628
* its values are stripped.
629629
*/
@@ -635,6 +635,35 @@ namespace pcs // i.e. "pythonic c++ strings"
635635
return CppStringT();
636636
}
637637

638+
/** \brief Returns a copy of the string with leading whitespaces removed. */
639+
inline CppStringT lstrip() const noexcept
640+
{
641+
for (auto it = this->cbegin(); it != this->cend(); ++it)
642+
if (*it != value_type(' '))
643+
return CppStringT(it, this->cend());
644+
return CppStringT();
645+
}
646+
647+
648+
//--- partition() -------------------------------------
649+
/** Split the string at the first occurrence of sep, and returns a 3-items vector containing the part before the separator, the separator itself, and the part after the separator.
650+
*
651+
* If the separator is not found, returns a 3-items vector
652+
* containing the string itself, followed by two empty strings.
653+
*/
654+
std::vector<CppStringT> partition(const CppStringT& sep) const noexcept
655+
{
656+
const size_type sep_index = find(sep);
657+
if (sep_index == CppStringT::npos) {
658+
return std::vector<CppStringT>({ *this, CppStringT(), CppStringT() });
659+
}
660+
else {
661+
const size_type third_index = sep_index + sep.size();
662+
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) });
664+
}
665+
}
666+
638667

639668
//--- rfind() -----------------------------------------
640669
/** 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.

0 commit comments

Comments
 (0)