Skip to content

Commit 6996af3

Browse files
committed
#53 - Implement method CppStringT::title()
Completed. Fixed a bug in method `capitalize()`.
1 parent 00815f4 commit 6996af3

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

cpp-strings/cppstrings.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ namespace pcs // i.e. "pythonic c++ strings"
174174
/** \brief In-place modifies the string with its first character capitalized and the rest lowercased. Returns a reference to the string*/
175175
inline CppStringT& capitalize() noexcept
176176
{
177-
this->lower();
178-
(*this)[0] = upper((*this)[0]);
177+
if (!this->empty()) {
178+
this->lower();
179+
(*this)[0] = pcs::to_upper((*this)[0]);
180+
}
179181
return *this;
180182
}
181183

@@ -1339,17 +1341,30 @@ namespace pcs // i.e. "pythonic c++ strings"
13391341
*/
13401342
inline CppStringT swapcase() const noexcept
13411343
{
1342-
CppStringT res( *this );
1344+
/*
1345+
CppStringT res(*this);
13431346
std::transform(this->cbegin(), this->cend(), res.begin(), pcs::swap_case);
13441347
return res;
1348+
*/
1349+
CppStringT res;
1350+
std::ranges::copy(std::views::transform(*this, pcs::swap_case), std::back_inserter(res));
1351+
return res;
13451352
}
13461353

13471354

13481355
//--- title() -----------------------------------------
13491356
/** \brief Returns a titlecased copy of the string where words start with an uppercase character and the remaining characters are lowercase. */
1350-
inline CppStringT title() const noexcept
1357+
CppStringT title() const noexcept
13511358
{
1352-
return *this;
1359+
constexpr CppStringT whitespace(value_type(' '));
1360+
1361+
CppStringT res(*this);
1362+
std::vector<CppStringT> words = res.split(whitespace);
1363+
1364+
for (CppStringT& word : words)
1365+
word.capitalize();
1366+
1367+
return whitespace.join(words);
13531368
}
13541369

13551370

0 commit comments

Comments
 (0)