std::ctype:...w,do_narrow(3)
π NAME π
std::ctype::narrow,do_narrow β std::ctype::narrow,do_narrow. yes bestie, the original NAME line said it twice, so we're saying it twice π
this is the std::ctype:...w,do_narrow(3) page from the C++ Standard Libary manual, and it's all about narrowing characters and the protected virtual functions that make it happen.
β¨ Synopsis β¨
okay so here's the synopsis, and the formatting is exactly as chaotic as the original β we do not edit the sacred code block π
Defined in header <locale>
public:
(1)
char narrow( CharT c, char dflt ) const;
public:
const CharT* narrow( const CharT* beg, const CharT* end,
(2)
char dflt, char* dst ) const;
protected:
(3)
virtual char do_narrow( CharT c, char dflt ) const;
protected:
virtual const CharT* do_narrow( const CharT* beg, const CharT* end,
(4)
char dflt, char* dst ) const;
now the overload lore π
- (1), (2) β Public member functions. They call the corresponding protected virtual member function
do_narrowoverload of the most derived class. Overload (1) callsdo_narrow(c, dflt), overload (2) callsdo_narrow(beg, end, dflt, dst). - (3) β Converts the (possibly wide) character
cto multibyte representation if the character can be represented with a single byte (for example, ASCII characters in UTF-8 encoding are single bytes). Returnsdfltif such conversion does not exist. - (4) β For every character in the character array
[beg, end), writes narrowed characters (ordfltwhenever narrowing fails) to the successive locations in the character array pointed to bydst.
Narrowing is always successful and always reversible (by calling widen()) for all characters from the basic source character set (until C++23) / basic character set (since C++23).
- i.e.
do_widen(do_narrow(c, 0)) == calways holds for any charactercin the basic source character set (until C++23) / basic character set (since C++23).
Narrowing, if successful, preserves all character classification categories known to is().
- i.e.
is(m, c) || !ctc.is(m, do_narrow(c, dflt))is always true for any namedctypecategory with actype<char>facetctcandctype_base::maskvaluem(unlessdo_narrowreturnsdflt).
Narrowing of any digit character guarantees that if the result is subtracted from the character literal '0', the difference equals the digit value of the original character.
- i.e. for any digit character
c, the expression(do_narrow(c, dflt) - '0')evaluates to the digit value of the character.
bestie, theuntil C++23/since C++23split is doing the most, but it's real and it's here π«Ά
πΈ 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) β Narrowed character or
dfltif narrowing fails. - (2), (4) β
end.
π§ Example π§
// Run this code
#include <iostream>
#include <locale>
void try_narrow(const std::ctype<wchar_t>& f, wchar_t c)
{
char n = f.narrow(c, 0);
if (n)
std::wcout << '\'' << c << "' narrowed to " << +(unsigned char)n << '\n';
else
std::wcout << '\'' << c << "' could not be narrowed\n";
}
int main()
{
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::wcout << std::hex << std::showbase << "In US English UTF-8 locale:\n";
auto& f = std::use_facet<std::ctype<wchar_t>>(std::locale());
try_narrow(f, L'A');
try_narrow(f, L'οΌ‘');
try_narrow(f, L'Δ');
std::locale::global(std::locale("cs_CZ.iso88592"));
auto& f2 = std::use_facet<std::ctype<wchar_t>>(std::locale());
std::wcout << "In Czech ISO-8859-2 locale:\n";
try_narrow(f2, L'A');
try_narrow(f2, L'οΌ‘');
try_narrow(f2, L'Δ');
}
π« Possible output π«
In US English UTF-8 locale:
'A' narrowed to 0x41
'οΌ‘' could not be narrowed
'Δ' could not be narrowed
In Czech ISO-8859-2 locale:
'A' narrowed to 0x41
'οΌ‘' could not be narrowed
'Δ' narrowed to 0xec
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 126β applied to C++98. Behavior as published: 1. the code representing reversibility wasdo_widen(do_narrow(c), 0) == c; 2. the code representing category preservation wasis(m, c) || !ctc.is(m, do_narrow(c), dflt). Correct behavior: corrected both.LWG 153β applied to C++98. Behavior as published:narrowalways called overload (4). Correct behavior: calls the corresponding overload.
π See also π
widenβ invokesdo_widen(public member function).narrowβ narrows characters (public member function ofstd::basic_ios<CharT,Traits>).wctobβ narrows a wide character to a single-byte narrow character, if possible (function).
bestie, that's the whole page π source/version: http://cppreference.com; date: 2024.06.10. now go out there and narrow something iconic, and if it fails, just return dflt and slay anyway π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms