std::codecv...ecvt::do_in(3)
okay so bestie, this is page std::codecv...ecvt::do_in(3) and we're diving into std::codecvt::in and std::codecvt::do_in from the C++ Standard Library β it's giving character conversion realness and honestly I'm obsessed πβ¨
π NAME π
std::codecvt::in, std::codecvt::do_in
β¨ Synopsis β¨
okay so first things first, this is defined in the header <locale> and here's the whole vibe:
public:
result in( StateT& state,
const ExternT* from,
const ExternT* from_end, (1)
const ExternT*& from_next,
InternT* to,
InternT* to_end,
InternT*& to_next ) const;
protected:
virtual result do_in( StateT& state,
const ExternT* from,
const ExternT* from_end, (2)
const ExternT*& from_next,
InternT* to,
InternT* to_end,
InternT*& to_next ) const;
- So this public member function is basically the main character β it calls the
do_inmember function of the most derived class. slay. - And this one, if the codecvt facet defines a conversion, translates the external characters from the source range
[from, from_end)to internal characters, placing the results in the subsequent locations starting atto. It converts no more thanfrom_end - fromexternal characters and writes no more thanto_end - tointernal characters. Leavesfrom_nextandto_nextpointing one beyond the last element successfully converted. periodt.
but wait β if this codecvt facet does not define a conversion, no characters are converted. to_next is set to be equal to to, state is unchanged, and std::codecvt_base::noconv is returned. it's giving "I'm not even gonna try" energy π
also do_in(state, from, from_end, from_next, to, to + 1, to_next) must return ok if:
- this codecvt facet is used by
basic_filebuf, and do_in(state, from, from_end, from_next, to, to_end, to_next)would returnokwhereto != to_end.
π Return value π
a value of type std::codecvt_base::result, indicating the success status as follows:
okβ conversion completed, we love to see it πpartialβ not enough space in the output buffer or unexpected end of source buffer, tragicerrorβ encountered a character that could not be converted, big yikesnoconvβ this facet is non-converting, no output written, it's giving lazy but valid
also the non-converting specialization std::codecvt<char, char, std::mbstate_t> always returns std::codecvt_base::noconv. not me calling it out but okay.
πΈ Notes πΈ
requires that from <= from_end && to <= to_end and that state either representing the initial shift state or obtained by converting the preceding characters in the sequence. like, respect the boundaries bestie.
the effect on state is deliberately unspecified. in standard facets, it's used to maintain shift state like when calling std::mbsrtowcs, and is therefore updated to reflect the conversion state after the last processed external character, but a user-defined facet is free to use it to maintain any other state, e.g. count the number of special characters encountered. so it's giving "choose your own adventure" energy π¦
π¦ Example π¦
okay so here's the whole thing in action:
// Run this code
#include <iostream>
#include <locale>
#include <string>
int main()
{
std::locale::global(std::locale("en_US.utf8"));
auto const& f = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>
(std::locale());
std::string external = "z\u00df\u6c34\U0001d10b"; // or u8"zΓζ°΄π"
// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"
// note that the following can be done with wstring_convert
std::mbstate_t mb = std::mbstate_t(); // initial shift state
std::wstring internal(external.size(), '\0');
const char* from_next;
wchar_t* to_next;
f.in(mb, &external[0], &external[external.size()], from_next,
&internal[0], &internal[internal.size()], to_next);
// error checking skipped for brevity
internal.resize(to_next - &internal[0]);
std::wcout << L"The string in wide encoding: " << internal << '\n';
}
output:
The string in wide encoding: zΓζ°΄π
ate that conversion π
π₯ Defect reports π₯
the following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR LWG 76, applied to C++98 β the behavior as published was that it was unclear whether the conversion is required to support producing one internal character at a time. the correct behavior is that it's only required if used by
basic_filebuf. it's giving "clarified and corrected" realness.
π See also π
underflowβ reads from the associated file[virtual](virtual protected member function ofstd::basic_filebuf<CharT,Traits>)from_bytesβ converts a byte string into a wide string (public member function ofstd::wstring_convert<Codecvt,Elem,Wide_alloc,Byte_alloc>)mbsrtowcsβ converts a narrow multibyte character string to wide string, given state (function)do_outβ converts a string fromInternTtoExternT, such as when writing to file[virtual](virtual protected member function)
and that's the whole vibe bestie π this page is from cppreference.com and the version date is 2024.06.10 β iconic as always β¨
π¦ 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