std::toupper(std::locale)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::toupper(std::locale) β€” literally std::toupper(std::locale), bestie. We’re in the C++ Standard Libary manual, page (3), and this function is serving self-titled main character energy. Periodt.


✨ Synopsis ✨

Defined in header <locale>
template< class CharT >
CharT toupper( CharT ch, const locale& loc );

Converts the character ch to uppercase if possible, using the conversion rules specified by the given locale’s std::ctype facet. Slay.


πŸŽ€ Parameters πŸŽ€

  • ch β€” character. The little guy getting uppercased.
  • loc β€” locale. The vibe check that decides how the conversion goes.

πŸ’– Return value πŸ’–

Returns the uppercase form of ch if one is listed in the locale. Otherwise it returns ch unchanged β€” no glow-up, just the same character back. 😭


🧠 Notes 🧠

Only 1:1 character mapping can be performed by this function. Like, it can’t do glow-up magic that turns one character into a whole string.

For example, the uppercase form of 'ß' is (with some exceptions) the two-character string "SS", which cannot be obtained by std::toupper. It’s giving β€œbestie I can only do one-for-one,” no cap. πŸ’…


πŸ› οΈ Possible implementation πŸ› οΈ

template<class CharT>
CharT toupper(CharT ch, const std::locale& loc)
{
    return std::use_facet<std::ctype<CharT>>(loc).toupper(ch);
}

🌈 Example 🌈

// Run this code

#include <cwctype>
#include <iostream>
#include <locale>

int main()
{
    wchar_t c = L'\u017f'; // Latin small letter Long S ('ΕΏ')

    std::cout << std::hex << std::showbase;

    std::cout << "in the default locale, toupper(" << (std::wint_t)c << ") = "
              << (std::wint_t)std::toupper(c, std::locale()) << '\n';

    std::cout << "in Unicode locale, toupper(" << (std::wint_t)c << ") = "
              << (std::wint_t)std::toupper(c, std::locale("en_US.utf8")) << '\n';
}

πŸ“ Possible output πŸ“

in the default locale, toupper(0x17f) = 0x17f
in Unicode locale, toupper(0x17f) = 0x53

πŸ‘€ See also πŸ‘€

  • tolower(std::locale) β€” converts a character to lowercase using the ctype facet of a locale (function template).
  • toupper β€” converts a character to uppercase (function).
  • towupper β€” converts a wide character to uppercase (function).

mwah πŸ’‹ β€” source: http://cppreference.com, version/date stamp: 2024.06.10. Stay cased, stay iconic 🫢✨

πŸ“¦ where this page comes from