Overview
Performs conversions between wide strings and byte strings (on either direction) using a conversion object of type Codecvt.
Details
wstring_convert
Performs conversions between wide strings and byte strings (on either direction) using a conversion object of type Codecvt.
The standard facets suitable for use with wstring_convert are codecvt_utf8 for UTF-8/UCS-2 and UTF-8/UCS-4 conversions and codecvt_utf8_utf16 for UTF-8/UTF-16 conversions.
It's behavior is similar tombsrtowcs() api
.
Syntax
template< class Codecvt, class Elem = wchar_t, class Wide_alloc = std::allocator<Elem>, class Byte_alloc = std::allocator<char> > class wstring_convert;
template parameters
Name | Description |
---|---|
Codecvt | Type of the conversion object: codecvt_utf8 for UTF-8/UCS-2 and UTF-8/UCS-4 conversions and codecvt_utf8_utf16 for UTF-8/UTF-16 conversions. |
Elem | Wide character type. |
Wide_alloc | Allocator for elements of type Elem. Defaults to: allocator<Elem> |
Byte_alloc | Allocator for elements of type char. Defaults to: allocator<char> |
member types
Name | Description |
---|---|
byte_string | basic_string<char> |
wide_string | basic_string<Elem> |
state_type | Codecvt::state_type |
int_type | char_traits<Elem>::int_type |
Fields
Name | Description |
---|---|
byte_string byte_err_string | the byte string to display on errors. |
wide_string wide_err_string | the wide string to display on errors. |
Codecvt* cvtptr | a pointer to the allocated conversion object |
state_type cvtstate | the conversion state object |
size_t cvtcount | the conversion counts |
Constructor
Name | Description |
---|---|
|
|
Example typedef codecvt_utf8<wchar_t> ccvt; //1 wstring_convert<ccvt> wsc; //2 wstring_convert<ccvt> wsc2 (new ccvt); //3 wstring_convert<ccvt> wsc3 (new ccvt, ccvt::state_type()); //4 wstring_convert<ccvt> wsc4 ("[error]",L"[error]"); wstring wstr (L"Khrisha Rao👸"); //str:"Khrisha Rao👸" string str = wsc4.to_bytes (wstr); |
Methods
Name | Description |
---|---|
|
If the conversion succeeds, returns the conversion result. Otherwise, returns wide_err_string or throws an exception. |
Example char str8[] = u8"ಖ್ರಿಷಾ Rao👸"; wstring_convert<codecvt_utf8<char32_t>, char32_t> u8to32; u32string str32 = u8to32.from_bytes(str8); cout << std::showbase << std::hex; //prints:0xc96 0xccd 0xcb0 0xcbf 0xcb7 0xcbe 0x20 0x52 0x61 0x6f 0x1f478 for (char32_t c : str32) cout << static_cast<unsigned long long>(c) << ' '; | |
|
If the conversion succeeds, returns the conversion result. Otherwise, returns wide_err_string or throws an exception. |
Example char32_t str32[] = U"ಖ್ರಿಷಾ Rao👸"; wstring_convert<codecvt_utf8<char32_t>, char32_t> uconv; string str8 = uconv.to_bytes(str32); //prints:ಖ್ರಿಷಾ Rao👸 cout << str8 << endl; | |
size_t converted() | Returns the number of input elements successfully converted by the last conversion operation. |
state_type state() | Returns the current value of the conversion state, which is stored in this wstring_convert object. The conversion state may be explicitly set in the constructor and is updated by all conversion operations. |
wbuffer_convert
wbuffer_convert is a wrapper over stream buffer of type streambuf which gives it the appearance of basic_streambuf<Elem>.
The class uses another stream buffer of bytes (narrow characters of type char) as its underlying byte stream buffer to/from which it converts wide characters of type Elem (its second template argument).
All I/O performed through wbuffer_convert undergoes character conversion as defined by the facet Codecvt. wbuffer_convert assumes ownership of the conversion facet, and cannot use a facet managed by a locale.
The standard facets suitable for use with std::wbuffer_convert are codecvt_utf8 for UTF-8/UCS-2 and UTF-8/UCS-4 conversions and std::codecvt_utf8_utf16 for UTF-8/UTF-16 conversions.
.
Syntax
template< class Codecvt, class Elem = wchar_t, class Tr = std::char_traits<Elem> > class wbuffer_convert : public std::basic_streambuf<Elem, Tr>
template parameters
Name | Description |
---|---|
Codecvt | Type of the conversion object: codecvt_utf8 for UTF-8/UCS-2 and UTF-8/UCS-4 conversions and codecvt_utf8_utf16 for UTF-8/UTF-16 conversions. |
Elem | Wide character type. |
Tr | Character traits class |
member types
Name | Description |
---|---|
state_type | Codecvt::state_type |
Fields
Name | Description |
---|---|
streambuf* bufptr | a pointer to the underlying byte stream buffer. |
Codecvt* cvtptr | a pointer to the allocated conversion object |
state_type cvtstate | the conversion state object |
Constructor
Name | Description |
---|---|
|
|
Example typedef codecvt_utf8<wchar_t> ccvt; //1 wbuffer_convert<ccvt> wbuf; //2stringbuf utf8buf(reinterpret_cast<const char*>(u8"ಖ್ರಿಷಾ Rao👸")); wbuffer_convert<codecvt_utf8<wchar_t>> conv_in(&utf8buf); wistream ucsbuf(&conv_in); cout << hex << showbase; istreambuf_iterator<wchar_t> oit(ucsbuf), end; //prints:0xc96 0xccd 0xcb0 0xcbf 0xcb7 0xcbe 0x20 0x52 0x61 0x6f 0x1f478 for_each(oit,end, [](wchar_t c){cout << (unsigned long long)c << ' '; }); cout << endl; |
Methods
Name | Description |
---|---|
|
|
Example //use clang wbuffer_convert<codecvt_utf8<wchar_t>> conv_out(cout.rdbuf()); wostream out(&conv_out); //prints:ಖ್ರಿಷಾ Rao👸 out << reinterpret_cast<const wchar_t*>(U"ಖ್ರಿಷಾ Rao👸"); | |
state_type state() | Returns the current value of the conversion state, which is stored in this wbuffer_convert object. The conversion state may be explicitly set in the constructor and is updated by all conversion operations. |
No comments:
Post a Comment