std::codecvt::length, std::codecvt::do_length

< cpp‎ | locale‎ | codecvt
Revision as of 20:49, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <locale>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >

int length( stateT& state,
            const externT* from,
            const externT* from_end,

            std::size_t max ) const

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >

int do_length( stateT& state,
               const externT* from,
               const externT* from_end,

               std::size_t max ) const

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) public member function, calls the member function do_length of the most derived class.

2) attempts to convert the externT characters from the character array defined by [from, from_end), given initial conversion state state, to at most max internT characters, and returns the number of externT characters that such conversion would consume. 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.

Return value

The number of externT characters that would be consumed if converted by Template:rlpf until either all from_end-from characters were consumed or max internT characters were producer, or a conversion error occurred.

The non-converting specialization std::codecvt<char, char, std::mbstate_t> returns std::min(max, from_end-from)


#include <locale>
#include <string>
#include <iostream>
int main()
    //  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::mbstate_t mb = std::mbstate_t();
    std::cout << "Only the first " <<
              std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
              ).length(mb, &s[0], &s[s.size()], 2)
              << " bytes out of " << s.size() << " would be consumed "
                 " to produce the first 2 characters\n";


Only the first 3 bytes out of 10 would be consumed to produce the first 2 characters

See also

Template:cpp/locale/codecvt/dcl list do in