summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fmultibyte%2Fmbrlen.html
blob: cf7c2914f90afa99f9318c1acce6cd3050d1a15a (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    <h1 id="firstHeading" class="firstHeading">mbrlen</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;wchar.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c95 t-until-c99"> <td> <pre data-language="c">size_t mbrlen( const char *s, size_t n, mbstate_t *ps );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c95">(since C95)</span> <br><span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">size_t mbrlen( 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-c99">(since C99)</span> </td> </tr>  </table> <p>Determines the size, in bytes, of the representation of a multibyte character.</p>
<p>This function is equivalent to the call <code><a href="http://en.cppreference.com/w/c/string/multibyte/mbrtowc"><span class="kw583">mbrtowc</span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a>, s, n, ps<span class="sy4">?</span>ps<span class="sy4">:</span><span class="sy3">&amp;</span>internal<span class="br0">)</span></code> for some hidden object <code>internal</code> of type <code><a href="mbstate_t" title="c/string/multibyte/mbstate t">mbstate_t</a></code>, except that the expression <code>ps</code> is evaluated only once.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> s </td> <td> - </td> <td> pointer to an element of a multibyte character string </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 variable holding the conversion state </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 next <code>n</code> or fewer bytes complete the null character or if <code>s</code> is a null pointer. Both cases reset the conversion state. </li>
<li> the number of bytes <code>[1...n]</code> that complete a valid multibyte character </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 are part of a possibly valid multibyte character, which is still incomplete after examining all <code>n</code> bytes </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. The value of <code><a href="../../error/errno" title="c/error/errno">errno</a></code> is <code>EILSEQ</code>; the conversion state is unspecified. </li>
</ul> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;locale.h&gt;
#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;wchar.h&gt;
 
int main(void)
{   
    // allow mbrlen() to work with UTF-8 multibyte encoding
    setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    const char* str = u8"水";
    size_t sz = strlen(str);
 
    mbstate_t mb;
    memset(&amp;mb, 0, sizeof mb);
    int len1 = mbrlen(str, 1, &amp;mb);
    if(len1 == -2) 
        printf("The first 1 byte of %s is an incomplete multibyte char"
               " (mbrlen returns -2)\n", str);
 
    int len2 = mbrlen(str+1, sz-1, &amp;mb);
    printf("The remaining %zu  bytes of %s hold %d bytes of the multibyte"
           " character\n", sz-1, str, len2);
 
    printf("Attempting to call mbrlen() in the middle of %s while in initial"
           " shift state returns %zd\n", str, mbrlen(str+1, sz-1, &amp;mb));
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">The first 1 byte of 水 is an incomplete multibyte char (mbrlen returns -2)
The remaining 2  bytes of 水 hold 2 bytes of the multibyte character
Attempting to call mbrlen() in the middle of 水 while in initial shift state returns -1</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.29.6.3.1 The mbrlen function (p: 442) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.24.6.3.1 The mbrlen function (p: 388) </li></ul>
</ul>      <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="mbrtowc" title="c/string/multibyte/mbrtowc"> <span class="t-lines"><span>mbrtowc</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> converts the next multibyte character to wide character, given state <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="mblen" title="c/string/multibyte/mblen"> <span class="t-lines"><span>mblen</span></span></a></div> </td> <td> returns the number of bytes in the next multibyte character <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/mbrlen" title="cpp/string/multibyte/mbrlen">C++ documentation</a></span> for <code>mbrlen</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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/mbrlen" class="_attribution-link">https://en.cppreference.com/w/c/string/multibyte/mbrlen</a>
  </p>
</div>