std::ctype:...:do_tolower(3)

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

πŸ’… std::ctype::tolower / std::ctype::do_tolower β€” a C++ Standard Libary moment πŸŽ€

ok so bestie, we're in <locale> land today and this one is genuinely iconic. it's the manual page for std::ctype::tolower and std::ctype::do_tolower β€” like the lowercase-ification girlies of the C++ standard library ✨


🌸 NAME 🌸

std::ctype::tolower, std::ctype::do_tolower β€” literally just std::ctype::tolower,std::ctype::do_tolower, no notes πŸ’…


✨ SYNOPSIS ✨

Defined in header <locale> β€” don't forget it, slay 🫢

public:
(1)
CharT tolower( CharT c ) const;
public:
(2)
const CharT* tolower( CharT* beg, const CharT* end ) const;
protected:
(3)
virtual CharT do_tolower( CharT c ) const;
protected:
(4)
virtual const CharT* do_tolower( CharT* beg, const CharT* end ) const;

So here's the tea, numbered like the icons they are:

  1. and 2. are the public member functions, and what do they do? they call the protected virtual member function do_tolower of the most derived class. delegation, we love to see it πŸ‘‘
  2. converts the character c to lower case if a lower case form is defined by this locale. very "if you know, you know" of it.
  3. for every character in the character array [beg, end), for which a lower case form exists, replaces the character with that lower case form. glow-down era πŸ’–

πŸ¦‹ PARAMETERS πŸ¦‹

  • c β€” character to convert
  • beg β€” pointer to the first character in an array of characters to convert
  • end β€” one past the end pointer for the array of characters to convert

πŸŽ€ RETURN VALUE πŸŽ€

  • 1, 3) β€” the lower case character, or c if no lower case form is listed by this locale (no glow-down for you, bestie, you stay as you are πŸ’€)
  • 2, 4) β€” end

πŸ‘‘ NOTES πŸ‘‘

⚠️ okay listen up, this is the important bit: only 1:1 character mapping can be performed by this function. so like, the Greek uppercase letter 'Ξ£' has two lowercase forms depending on the position in a word: 'Οƒ' and 'Ο‚'. a call to do_tolower cannot be used to obtain the correct lowercase form in this case. sobbing 😭 it's just not built for that level of nuance.

🌈 EXAMPLE 🌈

// Run this code

#include <iostream>
#include <locale>

void try_lower(const std::ctype<wchar_t>& f, wchar_t c)
{
    wchar_t up = f.tolower(c);
    if (up != c)
        std::wcout << "Lower case form of \'" << c << "' is " << up << '\n';
    else
        std::wcout << '\'' << c << "' has no lower case form\n";
}

int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    std::wcout.imbue(std::locale());
    std::wcout << "In US English UTF-8 locale:\n";
    auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
    try_lower(f, L'Ξ£');
    try_lower(f, L'Ɛ');
    try_lower(f, L'οΌ‘');

    std::wstring str = L"HELLo, wORLD!";
    std::wcout << "Lowercase form of the string '" << str << "' is ";
    f.tolower(&str[0], &str[0] + str.size());
    std::wcout << '\'' << str << "'\n";
}

(yes the variable is called up even though it holds the lowercase form β€” main character energy, we don't question the naming πŸ™ˆ)


πŸ“ OUTPUT πŸ“

In US English UTF-8 locale:
Lower case form of 'Ξ£' is Οƒ
Lower case form of 'Ɛ' is Ι›
Lower case form of '1' is a
Lowercase form of the string 'HELLo, wORLD!' is 'hello, world!'

look at 'hello, world!' fully lowercase and thriving πŸ₯ΉπŸ’œ


πŸ’– SEE ALSO πŸ’–

  • toupper β€” invokes do_toupper (public member function)
  • tolower β€” converts a character to lowercase (function)
  • towlower β€” converts a wide character to lowercase (function)

that's the whole vibe check bestie β€” this page came from cppreference.com, and the version/date stamp on it is 2024.06.10. go forth and lowercase responsibly βœ¨πŸ¦‹πŸ’…πŸ½

πŸ“¦ where this page comes from