Skip to content

Commit 00815f4

Browse files
committed
#52 - Implement method CppStringT::swapcase()
Completed.
1 parent 7351b16 commit 00815f4

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

cpp-strings/cppstrings.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ namespace pcs // i.e. "pythonic c++ strings"
8181
template<class CharT>
8282
inline const bool is_upper(const CharT ch) noexcept; //!< Returns true if character is uppercase, or false otherwise.
8383

84+
template<class CharT>
85+
inline const CharT swap_case(const CharT ch) noexcept; //!< Returns the swapped case form of character ch if it exists, or ch itself otherwise.
86+
87+
template<class CharT>
88+
inline const CharT to_lower(const CharT ch) noexcept; //!< Returns the lowercase form of character ch if it exists, or ch itself otherwise.
89+
90+
template<class CharT>
91+
inline const CharT to_upper(const CharT ch) noexcept; //!< Returns the uppercase form of character ch if it exists, or ch itself otherwise.
8492

8593

8694
//===== CppStringT<> ======================================
@@ -1324,6 +1332,18 @@ namespace pcs // i.e. "pythonic c++ strings"
13241332
}
13251333

13261334

1335+
//--- swapcase() --------------------------------------
1336+
/** \brief Returns a copy of the string with uppercase characters converted to lowercase and vice versa.
1337+
*
1338+
* Note that it is not necessarily true that s.swapcase().swapcase() == s.
1339+
*/
1340+
inline CppStringT swapcase() const noexcept
1341+
{
1342+
CppStringT res( *this );
1343+
std::transform(this->cbegin(), this->cend(), res.begin(), pcs::swap_case);
1344+
return res;
1345+
}
1346+
13271347

13281348
//--- title() -----------------------------------------
13291349
/** \brief Returns a titlecased copy of the string where words start with an uppercase character and the remaining characters are lowercase. */
@@ -1523,4 +1543,65 @@ namespace pcs // i.e. "pythonic c++ strings"
15231543
return std::iswupper(ch);
15241544
}
15251545

1546+
1547+
//--- swap_case() -----------------------------------------
1548+
/** \brief Returns the swapped case form of character ch if it exists, or ch itself otherwise. */
1549+
template<class CharT>
1550+
inline const CharT swap_case(const CharT ch) noexcept
1551+
{
1552+
if (pcs::is_lower(ch))
1553+
return pcs::to_upper(ch);
1554+
else if (pcs::is_upper(ch))
1555+
return pcs::to_lower(ch);
1556+
else
1557+
return ch;
1558+
}
1559+
1560+
1561+
//--- to_lower() ------------------------------------------
1562+
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
1563+
template<class CharT>
1564+
inline const CharT to_lower(const CharT ch) noexcept
1565+
{
1566+
return ch;
1567+
}
1568+
1569+
/** \brief Returns the lowercase form of character ch if it exists, or ch itself otherwise. Conforms to the current locale settings. */
1570+
template<>
1571+
inline const char to_lower<char>(const char ch) noexcept
1572+
{
1573+
return std::tolower(static_cast<unsigned char>(ch));
1574+
}
1575+
1576+
/** \brief Returns the lowercase form of character ch if it exists, or ch itself otherwise. Conforms to the current locale settings. */
1577+
template<>
1578+
inline const wchar_t to_lower<wchar_t>(const wchar_t ch) noexcept
1579+
{
1580+
return std::towlower(ch);
1581+
}
1582+
1583+
1584+
//--- to_upper() ------------------------------------------
1585+
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
1586+
template<class CharT>
1587+
inline const CharT to_upper(const CharT ch) noexcept
1588+
{
1589+
return ch;
1590+
}
1591+
1592+
/** \brief Returns the uppercase form of character ch if it exists, or ch itself otherwise. Conforms to the current locale settings. */
1593+
template<>
1594+
inline const char to_upper<char>(const char ch) noexcept
1595+
{
1596+
return std::toupper(static_cast<unsigned char>(ch));
1597+
}
1598+
1599+
/** \brief Returns the uppercase form of character ch if it exists, or ch itself otherwise. Conforms to the current locale settings. */
1600+
template<>
1601+
inline const wchar_t to_upper<wchar_t>(const wchar_t ch) noexcept
1602+
{
1603+
return std::towupper(ch);
1604+
}
1605+
1606+
15261607
} // end of namespace pcs // (pythonic c++ strings)

0 commit comments

Comments
 (0)