Difference between revisions of "cpp/string/multibyte/mbrtoc16"
(Typo fix) |
YexuanXiao (Talk | contribs) m |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
{{cpp/title|mbrtoc16}} | {{cpp/title|mbrtoc16}} | ||
{{cpp/string/multibyte/navbar}} | {{cpp/string/multibyte/navbar}} | ||
− | {{ddcl | header=cuchar | since=c++11 | | + | {{ddcl|header=cuchar|since=c++11| |
std::size_t mbrtoc16( char16_t* pc16, | std::size_t mbrtoc16( char16_t* pc16, | ||
const char* s, | const char* s, | ||
Line 10: | Line 10: | ||
Converts a narrow multibyte character to UTF-16 character representation. | Converts a narrow multibyte character to UTF-16 character representation. | ||
− | If {{ | + | If {{c|s}} is not a null pointer, inspects at most {{c|n}} bytes of the multibyte character string, beginning with the byte pointed to by {{c|s}} to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in {{c|s}} is complete and valid, converts it to the corresponding 16-bit character and stores it in {{c|*pc16}} (if {{c|pc16}} is not null). |
− | If the multibyte character in {{ | + | If the multibyte character in {{c|*s}} corresponds to a multi-char16_t sequence (e.g. a surrogate pair in UTF-16), then after the first call to this function, {{c|*ps}} is updated in such a way that the next call to {{tt|mbrtoc16}} will write out the additional char16_t, without considering {{c|*s}}. |
− | If {{ | + | If {{c|s}} is a null pointer, the values of {{c|n}} and {{c|pc16}} are ignored and the call is equivalent to {{c|std::mbrtoc16(nullptr, "", 1, ps)}}. |
If the wide character produced is the null character, the conversion state {{c|*ps}} represents the initial shift state. | If the wide character produced is the null character, the conversion state {{c|*ps}} represents the initial shift state. | ||
Line 22: | Line 22: | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | pc16 | pointer to the location where the resulting 16-bit character will be written}} | + | {{par|pc16|pointer to the location where the resulting 16-bit character will be written}} |
− | {{par | s | pointer to the multibyte character string used as input}} | + | {{par|s|pointer to the multibyte character string used as input}} |
− | {{par | n | limit on the number of bytes in s that can be examined}} | + | {{par|n|limit on the number of bytes in s that can be examined}} |
− | {{par | ps | pointer to the conversion state object used when interpreting the multibyte string }} | + | {{par|ps|pointer to the conversion state object used when interpreting the multibyte string}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | |||
The first of the following that applies: | The first of the following that applies: | ||
− | * {{c|0}} if the character converted from {{ | + | * {{c|0}} if the character converted from {{c|s}} (and stored in {{c|*pc16}} if non-null) was the null character. |
− | * the number of bytes {{c|[1...n]}} of the multibyte character successfully converted from {{ | + | * the number of bytes {{c|[1...n]}} of the multibyte character successfully converted from {{c|s}}. |
* {{c|-3}} if the next {{c|char16_t}} from a multi-{{c|char16_t}} character (e.g. a surrogate pair) has now been written to {{c|*pc16}}. No bytes are processed from the input in this case. | * {{c|-3}} if the next {{c|char16_t}} from a multi-{{c|char16_t}} character (e.g. a surrogate pair) has now been written to {{c|*pc16}}. No bytes are processed from the input in this case. | ||
− | * {{c|-2}} if the next {{ | + | * {{c|-2}} if the next {{c|n}} bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to {{c|*pc16}}. |
− | * {{c|-1}} if encoding error occurs. Nothing is written to {{ | + | * {{c|-1}} if encoding error occurs. Nothing is written to {{c|*pc16}}, the value {{lc|EILSEQ}} is stored in {{lc|errno}} and the value of {{c|*ps}} is unspecified. |
===Example=== | ===Example=== | ||
{{example|code= | {{example|code= | ||
− | |||
− | |||
#include <clocale> | #include <clocale> | ||
#include <cstring> | #include <cstring> | ||
+ | #include <cuchar> | ||
#include <cwchar> | #include <cwchar> | ||
− | #include < | + | #include <iomanip> |
+ | #include <iostream> | ||
int main() | int main() | ||
Line 50: | Line 49: | ||
std::setlocale(LC_ALL, "en_US.utf8"); | std::setlocale(LC_ALL, "en_US.utf8"); | ||
− | std::string str = | + | std::string str = "z\u00df\u6c34\U0001F34C"; // or u8"zß水🍌" |
std::cout << "Processing " << str.size() << " bytes: [ " << std::showbase; | std::cout << "Processing " << str.size() << " bytes: [ " << std::showbase; | ||
− | for(unsigned char c: str) std::cout << std::hex << +c << ' '; | + | for (unsigned char c: str) |
+ | std::cout << std::hex << +c << ' '; | ||
std::cout << "]\n"; | std::cout << "]\n"; | ||
std::mbstate_t state{}; // zero-initialized to initial state | std::mbstate_t state{}; // zero-initialized to initial state | ||
char16_t c16; | char16_t c16; | ||
− | const char *ptr = &str[0], *end = &str[0] + str.size(); | + | const char* ptr = &str[0], *end = &str[0] + str.size(); |
− | while( | + | while (std::size_t rc = std::mbrtoc16(&c16, ptr, end - ptr + 1, &state)) |
− | { | + | { |
− | std::cout << "Next UTF-16 char: " << std::hex << c16 << " obtained from "; | + | std::cout << "Next UTF-16 char: " << std::hex |
− | if(rc == -3) | + | << static_cast<int>(c16) << " obtained from "; |
+ | if (rc == (std::size_t)-3) | ||
std::cout << "earlier surrogate pair\n"; | std::cout << "earlier surrogate pair\n"; | ||
− | if(rc | + | else if (rc == (std::size_t) - 2) |
+ | break; | ||
+ | else if (rc == (std::size_t) - 1) | ||
+ | break; | ||
+ | else | ||
+ | { | ||
std::cout << std::dec << rc << " bytes [ "; | std::cout << std::dec << rc << " bytes [ "; | ||
− | for( | + | for (std::size_t n = 0; n < rc; ++n) |
− | std::cout << std::hex << + | + | std::cout << std::hex << +static_cast<unsigned char>(ptr[n]) << ' '; |
std::cout << "]\n"; | std::cout << "]\n"; | ||
ptr += rc; | ptr += rc; | ||
Line 85: | Line 91: | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/string/multibyte/dsc c16rtomb}} | + | {{dsc inc|cpp/string/multibyte/dsc c16rtomb}} |
− | {{dsc inc | cpp/locale/codecvt/dsc do_in | mem=std::codecvt<char16_t, char, std::mbstate_t>}} | + | {{dsc inc|cpp/locale/codecvt/dsc do_in|mem=std::codecvt<char16_t, char, std::mbstate_t>}} |
− | {{dsc see c | c/string/multibyte/mbrtoc16}} | + | {{dsc see c|c/string/multibyte/mbrtoc16}} |
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 02:02, 23 September 2024
Defined in header <cuchar>
|
||
std::size_t mbrtoc16( char16_t* pc16, const char* s, |
(since C++11) | |
Converts a narrow multibyte character to UTF-16 character representation.
If s is not a null pointer, inspects at most n bytes of the multibyte character string, beginning with the byte pointed to by s to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences). If the function determines that the next multibyte character in s is complete and valid, converts it to the corresponding 16-bit character and stores it in *pc16 (if pc16 is not null).
If the multibyte character in *s corresponds to a multi-char16_t sequence (e.g. a surrogate pair in UTF-16), then after the first call to this function, *ps is updated in such a way that the next call to mbrtoc16
will write out the additional char16_t, without considering *s.
If s is a null pointer, the values of n and pc16 are ignored and the call is equivalent to std::mbrtoc16(nullptr, "", 1, ps).
If the wide character produced is the null character, the conversion state *ps represents the initial shift state.
The multibyte encoding used by this function is specified by the currently active C locale.
Contents |
[edit] Parameters
pc16 | - | pointer to the location where the resulting 16-bit character will be written |
s | - | pointer to the multibyte character string used as input |
n | - | limit on the number of bytes in s that can be examined |
ps | - | pointer to the conversion state object used when interpreting the multibyte string |
[edit] Return value
The first of the following that applies:
- 0 if the character converted from s (and stored in *pc16 if non-null) was the null character.
- the number of bytes [1...n] of the multibyte character successfully converted from s.
- -3 if the next char16_t from a multi-char16_t character (e.g. a surrogate pair) has now been written to *pc16. No bytes are processed from the input in this case.
- -2 if the next n bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to *pc16.
- -1 if encoding error occurs. Nothing is written to *pc16, the value EILSEQ is stored in errno and the value of *ps is unspecified.
[edit] Example
#include <clocale> #include <cstring> #include <cuchar> #include <cwchar> #include <iomanip> #include <iostream> int main() { std::setlocale(LC_ALL, "en_US.utf8"); std::string str = "z\u00df\u6c34\U0001F34C"; // or u8"zß水🍌" std::cout << "Processing " << str.size() << " bytes: [ " << std::showbase; for (unsigned char c: str) std::cout << std::hex << +c << ' '; std::cout << "]\n"; std::mbstate_t state{}; // zero-initialized to initial state char16_t c16; const char* ptr = &str[0], *end = &str[0] + str.size(); while (std::size_t rc = std::mbrtoc16(&c16, ptr, end - ptr + 1, &state)) { std::cout << "Next UTF-16 char: " << std::hex << static_cast<int>(c16) << " obtained from "; if (rc == (std::size_t)-3) std::cout << "earlier surrogate pair\n"; else if (rc == (std::size_t) - 2) break; else if (rc == (std::size_t) - 1) break; else { std::cout << std::dec << rc << " bytes [ "; for (std::size_t n = 0; n < rc; ++n) std::cout << std::hex << +static_cast<unsigned char>(ptr[n]) << ' '; std::cout << "]\n"; ptr += rc; } } }
Output:
Processing 10 bytes: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0x7a obtained from 1 bytes [ 0x7a ] Next UTF-16 char: 0xdf obtained from 2 bytes [ 0xc3 0x9f ] Next UTF-16 char: 0x6c34 obtained from 3 bytes [ 0xe6 0xb0 0xb4 ] Next UTF-16 char: 0xd83c obtained from 4 bytes [ 0xf0 0x9f 0x8d 0x8c ] Next UTF-16 char: 0xdf4c obtained from earlier surrogate pair
[edit] See also
(C++11) |
convert a 16-bit wide character to narrow multibyte string (function) |
[virtual] |
converts a string from ExternT to InternT , such as when reading from file (virtual protected member function of std::codecvt<InternT,ExternT,StateT> )
|
C documentation for mbrtoc16
|