diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html | |
new repository
Diffstat (limited to 'devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html')
| -rw-r--r-- | devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html b/devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html new file mode 100644 index 00000000..df3061d9 --- /dev/null +++ b/devdocs/c/string%2Fmultibyte%2Fmbrtoc16.html @@ -0,0 +1,76 @@ + <h1 id="firstHeading" class="firstHeading">mbrtoc16</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><uchar.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">size_t mbrtoc16( char16_t* restrict pc16, const char* restrict s, + size_t n, mbstate_t* restrict ps );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Converts a single code point from its narrow multibyte character representation to its variable-length 16-bit wide character representation (typically, UTF-16).</p> +<p>If <code>s</code> is not a null pointer, inspects at most <code>n</code> bytes of the multibyte character string, beginning with the byte pointed to by <code>s</code> to determine the number of bytes necessary to complete the next multibyte character (including any shift sequences, and taking into account the current multibyte conversion state <code>*ps</code>). If the function determines that the next multibyte character in <code>s</code> is complete and valid, converts it to the corresponding 16-bit wide character and stores it in <code>*pc16</code> (if <code>pc16</code> is not null).</p> +<p>If the multibyte character in <code>*s</code> corresponds to a multi-char16_t sequence (e.g. a surrogate pair in UTF-16), then after the first call to this function, <code>*ps</code> is updated in such a way that the next call to <code>mbrtoc16</code> will write out the additional <code>char16_t</code>, without considering <code>*s</code>.</p> +<p>If <code>s</code> is a null pointer, the values of <code>n</code> and <code>pc16</code> are ignored and the call is equivalent to <code>mbrtoc16<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a>, <span class="st0">""</span>, <span class="nu0">1</span>, ps<span class="br0">)</span></code>.</p> +<p>If the wide character produced is the null character, the conversion state <code>*ps</code> represents the initial shift state.</p> +<p>If the macro <code>__STDC_UTF_16__</code> is defined, the 16-bit encoding used by this function is UTF-16; otherwise, it is implementation-defined. <span class="t-rev-inl t-since-c23"><span>The macro is always defined and the encoding is always UTF-16.</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span> In any case, the multibyte character encoding used by this function is specified by the currently active C locale.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> pc16 </td> <td> - </td> <td> pointer to the location where the resulting 16-bit wide character will be written </td> +</tr> <tr class="t-par"> <td> s </td> <td> - </td> <td> pointer to the multibyte character string used as input </td> +</tr> <tr class="t-par"> <td> n </td> <td> - </td> <td> limit on the number of bytes in s that can be examined </td> +</tr> <tr class="t-par"> <td> ps </td> <td> - </td> <td> pointer to the conversion state object used when interpreting the multibyte string </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>The first of the following that applies:</p> +<ul> +<li> <code>â0â</code> if the character converted from <code>s</code> (and stored in <code>*pc16</code> if non-null) was the null character </li> +<li> the number of bytes <code>[1...n]</code> of the multibyte character successfully converted from <code>s</code> </li> +<li> <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">3</span></code> if the next <code>char16_t</code> from a multi-<code>char16_t</code> character (e.g. a surrogate pair) has now been written to <code>*pc16</code>. No bytes are processed from the input in this case. </li> +<li> <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">2</span></code> if the next <code>n</code> bytes constitute an incomplete, but so far valid, multibyte character. Nothing is written to <code>*pc16</code>. </li> +<li> <code><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/size_t"><span class="kw100">size_t</span></a><span class="br0">)</span><span class="sy2">-</span><span class="nu0">1</span></code> if encoding error occurs. Nothing is written to <code>*pc16</code>, the value <code><a href="../../error/errno_macros" title="c/error/errno macros">EILSEQ</a></code> is stored in <code><a href="../../error/errno" title="c/error/errno">errno</a></code> and the value of <code>*ps</code> is unspecified. </li> +</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <locale.h> +#include <stdio.h> +#include <uchar.h> + +mbstate_t state; + +int main(void) +{ + setlocale(LC_ALL, "en_US.utf8"); + const char in[] = u8"zĂć°´đ"; // or "z\u00df\u6c34\U0001F34C" + const size_t in_sz = sizeof in / sizeof *in; + + printf("Processing %zu UTF-8 code units: [ ", in_sz); + for (size_t n = 0; n < in_sz; ++n) + printf("%#x ", (unsigned char)in[n]); + puts("]"); + + char16_t out[in_sz]; + const char *p_in = in, *end = in + in_sz; + char16_t *p_out = out; + for (size_t rc; (rc = mbrtoc16(p_out, p_in, end - p_in, &state));) + { + if (rc == (size_t)-1) // invalid input + break; + else if(rc == (size_t)-2) // truncated input + break; + else if(rc == (size_t)-3) // UTF-16 high surrogate + p_out += 1; + else + { + p_in += rc; + p_out += 1; + }; + } + + const size_t out_sz = p_out - out + 1; + printf("into %zu UTF-16 code units: [ ", out_sz); + for (size_t x = 0; x < out_sz; ++x) + printf("%#x ", out[x]); + puts("]"); +}</pre></div> <p>Output:</p> +<div class="text source-text"><pre data-language="c">Processing 11 UTF-8 code units: [ 0x7a 0xc3 0x9f 0xe6 0xb0 0xb4 0xf0 0x9f 0x8d 0x8c 0 ] +into 6 UTF-16 code units: [ 0x7a 0xdf 0x6c34 0xd83c 0xdf4c 0 ]</pre></div> </div> <h3 id="References"> References</h3> <ul> +<li> C23 standard (ISO/IEC 9899:2023): </li> +<ul><li> 7.30.1.3 The mbrtoc16 function (p: 408-409) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.28.1.1 The mbrtoc16 function (p: 398-399) </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="c16rtomb" title="c/string/multibyte/c16rtomb"> <span class="t-lines"><span>c16rtomb</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> converts a 16-bit wide character to narrow multibyte string <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/multibyte/mbrtoc16" title="cpp/string/multibyte/mbrtoc16">C++ documentation</a></span> for <code>mbrtoc16</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/string/multibyte/mbrtoc16" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/mbrtoc16</a> + </p> +</div> |
