std::codecv...h,do_length(3)
π
std::codecvt::length, do_length β C++ Standard Library realness (that's the manual name bestie, we're in <locale> land)
Ok so picture this: you're bicultural. You speak ExternT on the street and InternT at home. This page is about the β¨translatorβ¨ and specifically about asking it, "hey, how many bytes of the outside-world string would you eat to give me N inside-world characters?" That's it. That's the vibe check. No conversion actually happens, it's a preview. Iconic behavior honestly.
β¨ NAME β¨
std::codecvt::length, do_length β literally just std::codecvt::length, do_length. Yes the name is the name, we're not doing extra credit here π
πΈ Synopsis πΈ
Defined in header <locale>
public:
int length( StateT& state, const ExternT* from, const ExternT* from_end, (1)
std::size_t max ) const;
protected:
virtual int do_length( StateT& state, const ExternT* from, const ExternT* (2)
from_end,
std::size_t max ) const;
- The public member function. It just calls
do_lengthon the most derived class. Middle management energy, but make it fashion π - The protected virtual one, the actual main character. Here's what she does: she attempts to convert the
ExternTcharacters sitting in the character array defined by[from, from_end), starting from the initial conversion statestate, into at mostmaxInternTcharacters β and then returns the number ofExternTcharacters that such a conversion would consume π
And (this part matters, so pay attention) it modifies state as if by executing do_in(state, from, from_end, from, to, to + max, to) for some imaginary [to, to + max) output buffer. Imaginary. Like her imaginary boyfriend. The buffer isn't real but the state change absolutely is π
π Return value π
The number of ExternT characters that would be consumed if converted by do_in(), stopping at whichever of these hits first:
- all
from_end - fromcharacters got consumed, maxInternTcharacters got produced,- or a conversion error occurred π
And the non-converting specialization, std::codecvt<char, char, std::mbstate_t>? She doesn't convert anything, she just returns std::min(max, from_end - from) and honestly good for her, zero notes, no stress πΈ
π¦ Example π¦
// Run this code
#include <iostream>
#include <locale>
#include <string>
int main()
{
using facet_type = std::codecvt<char16_t, char, std::mbstate_t>;
// narrow multibyte encoding
std::string s = "z\u00df\u6c34\U0001d10b"; // or u8"zΓζ°΄π"
// or "\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"
std::setlocale(LC_ALL, "en_US.UTF-8");
facet_type const& codecvt_facet =
std::use_facet<facet_type>(std::locale());
std::mbstate_t mb = std::mbstate_t();
std::cout << "Only the first "
<< codecvt_facet.length(mb, s.data(), s.data() +
s.size(), 2)
<< " bytes out of " << s.size() << " would be consumed"
" to produce the first 2 characters\n";
}
Output:
Only the first 3 bytes out of 10 would be consumed to produce the first 2 characters
See how she said 3 bytes out of 10 for 2 characters? Multibyte encoding said "not today, one byte per character is a myth" π§β¨
π Defect reports π
So these behavior-changing defect reports got applied retroactively to previously published C++ standards. Time travel but make it committee π°οΈ
- LWG 75 β applied to C++98. Behavior as published: the effect on
statewas not specified. Correct behavior: specified π - LWG 305 β applied to C++98. Behavior as published:
std::codecvt<wchar_t, char, std::mbstate_t>::do_lengthwas required to returnstd::min(max, from_end - from). Correct behavior: not required π₯
π«Ά See also π«Ά
do_inβ converts a string fromExternTtoInternT, such as when reading from file.[virtual](virtual protected member function) π
And that's the page bestie! No cap, you now understand a facet that most people avoid for their whole careers. Go off π
Sign-off time: this lil masterpiece lives at http://cppreference.com, dated 2024.06.10, and yes the footer also says std::codecv...h,do_length(3) because tradition is tradition π
π¦ 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