std::ctype::widen,do_widen(3)

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

πŸ’– NAME πŸ’–

Ok bestie, this is std::ctype::widen,do_widen(3) from the C++ Standard Libary manual β€” yes, that's the name on the tin πŸ’…. We're talking std::ctype::widen,do_widen β€” std::ctype::widen,do_widen, the whole iconic character-conversion situation. It's giving "single-byte to wide-character glow-up," and we're gonna keep every detail, fr fr. ✨

✨ Synopsis ✨

Here's the cast, keeping the public: and protected: labels exactly as they appear:

Defined in header <locale>
public:
(1)
CharT widen( char c ) const;
public:
(2)
const char* widen( const char* beg, const char* end, CharT* dst )
const;
protected:
(3)
virtual CharT do_widen( char c ) const;
protected:
virtual const char* do_widen( const char* beg, const char* end,
CharT* dst )    (4)
const;

1,2) These are the public member functions, and they call the corresponding protected virtual member function do_widen overload of the most derived class. Overload (1) calls do_widen(c), and overload (2) calls do_widen(beg, end, dst).

  1. Converts the single-byte character c to the corresponding wide character representation using the simplest reasonable transformation. Typically, this applies only to the characters whose multibyte encoding is a single byte (e.g. U+0000-U+007F in UTF-8).
  2. For every character in the character array [beg, end), writes the corresponding widened character to the successive locations in the character array pointed to by dst.

Widening always returns a wide character, but only the characters from the basic source character set (until C++23) / basic character set (since C++23) are guaranteed to have a unique, well-defined, widening transformation, which is also guaranteed to be reversible (by narrow()). In practice, all characters whose multibyte representation is a single byte are usually widened to their wide character counterparts, and the rest of the possible single-byte values are usually mapped into the same placeholder value, typically CharT(-1). 😭

Widening, if successful, preserves all character classification categories known to is().

πŸŽ€ Parameters πŸŽ€

  • c β€” character to convert
  • dflt β€” default value to produce if the conversion fails
  • 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
  • dst β€” pointer to the first element of the array of characters to fill

🌸 Return value 🌸

  • 1,3 β€” Widened character.
  • 2,4 β€” end

πŸ¦‹ Example πŸ¦‹

Run this code:

// Run this code

        #include <iostream>
        #include <locale>

        void try_widen(const std::ctype<wchar_t>& f, char c)
        {
            wchar_t w = f.widen(c);
            std::cout << "The single-byte character " << +(unsigned char)c
                      << " widens to " << +w << '\n';
        }

        int main()
        {
            std::locale::global(std::locale("cs_CZ.iso88592"));
            auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
            std::cout << std::hex << std::showbase << "In Czech ISO-8859-2
       locale:\n";
            try_widen(f, 'a');
            try_widen(f, '\xdf'); // German letter ß (U+00df) in ISO-8859-2
            try_widen(f, '\xec'); // Czech letter Δ› (U+011b) in ISO-8859-2

            std::locale::global(std::locale("cs_CZ.utf8"));
            auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
            std::cout << "In Czech UTF-8 locale:\n";
            try_widen(f2, 'a');
            try_widen(f2, '\xdf');
            try_widen(f2, '\xec');
        }

Possible output:

        In Czech ISO-8859-2 locale:
        The single-byte character 0x61 widens to 0x61
        The single-byte character 0xdf widens to 0xdf
        The single-byte character 0xec widens to 0x11b
        In Czech UTF-8 locale:
        The single-byte character 0x61 widens to 0x61
        The single-byte character 0xdf widens to 0xffffffff
        The single-byte character 0xec widens to 0xffffffff

πŸ’€ Defect reports πŸ’€

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 153, applied to C++98 β€” behavior as published: widen always called overload (4); correct behavior: calls the corresponding overload.

πŸ‘‘ See also πŸ‘‘

  • narrow β€” invokes do_narrow (public member function)
  • widen β€” widens characters (public member function of std::basic_ios<CharT,Traits>)
  • btowc β€” widens a single-byte narrow character to wide character, if possible (function)

Later, bestie 🫢 β€” this page is from http://cppreference.com, version/date 2024.06.10, and it's literally the std::ctype::widen,do_widen(3) page. Slay. πŸ’–βœ¨

πŸ“¦ where this page comes from