public member function
<locale>
int length ( stateT& state, const externT* from, const externT* from_end, size_t max ) const;
Return length of translated sequence
Returns the amount of external characters in the range
[from,from_end) that could be translated into at maximum
max internal characters, as if done with
codecvt::in.
state is also updated as if
codecvt::in was called for a buffer of
max internal characters.
During its operation, this function simply calls the virtual protected member
codecvt::do_length, which is the member function in charge of performing the actions described above.
Parameters
- state
- State object to keep track of the state of a multibyte character conversion. Typically, this is an object of type mbstate_t.
stateT is the state type (i.e., the third template parameter of codecvt).
- from, from_end
- Pointer to the initial and final characters of the source sequence. The range used is [from,from_end), which contains all the characters between from and from_end, including the character pointed by from but not the character pointed by from_end.
externT is the external character type (i.e., the second template parameter of codecvt).
- max
- Maximum length of the translated sequence (in terms of internal characters).
size_t is an integral type.
Return value
The length of the sequence of characters if translated to internal characters.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// codecvt::length example
#include <iostream>
#include <locale>
using namespace std;
int main ()
{
locale loc;
mbstate_t mystate;
char origin[] = "abcdefghijklmnopqrstuvwxyz";
const char * pc;
wchar_t * pwc;
const codecvt<wchar_t,char,mbstate_t>& myfacet =
use_facet<codecvt<wchar_t,char,mbstate_t> >(loc);
// calculate length for 10 wchar_t's:
int length = myfacet.length (mystate, origin, origin+sizeof(origin), 10);
wchar_t * dest = new wchar_t[length+1];
myfacet.in (mystate, origin, origin+sizeof(origin), pc, dest, dest+length, pwc);
dest[length]=0; // Terminating null-character
cout << "A string of 10 wchar_t elements: ";
wcout << dest << endl;
return 0;
}
|
Output:
A string of 10 wchar_t elements: abcdefghij
|
See also
- codecvt::in
- Translate in characters (public member function)
- codecvt::max_length
- Return max length of one character (public member function)
- codecvt::do_length
- Return length of translated sequence [virtual] (public member function)