function
<cstdlib>
mblenint mblen (const char* pmb, size_t max);
Get length of multibyte character
Returns the size of the multibyte character pointed by pmb, examining at most max bytes.mblen has its own internal shift state, which is altered as necessary only by calls to this function. A call to the function with a null pointer as pmb resets the state (and returns whether multibyte characters are state-dependent).
The behavior of this function depends on the LC_CTYPE category of the selected C locale.
-1
is returned.
If the argument passed as pmb is a null pointer, the function returns a nonzero value if multibyte character encodings are state-dependent, and zero otherwise.
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
/* mblen example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* mblen, mbtowc, wchar_t(C) */
void printbuffer (const char* pt, size_t max)
{
int length;
wchar_t dest;
mblen (NULL, 0); /* reset mblen */
mbtowc (NULL, NULL, 0); /* reset mbtowc */
while (max>0) {
length = mblen (pt, max);
if (length<1) break;
mbtowc(&dest,pt,length);
printf ("[%lc]",dest);
pt+=length; max-=length;
}
}
int main()
{
const char str [] = "test string";
printbuffer (str,sizeof(str));
return 0;
}
The example uses a trivial string using the "C"
locale, but locales that interpret multibyte string are supported by the function.
Output:
[t][e][s][t][ ][s][t][r][i][n][g]
If pmb is neither a null pointer nor a pointer to an array long enough (as described above), it causes undefined behavior.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4