mbrlen
Determine the number of bytes in a string, with the capability of restarting in the middle of a multibyte character if need be, while using the current locale.
size_t mbrlen( const char *str, size_t maxSize, mbstate_t mbstate );
The mbrlen function determines the number of bytes constituting the multibyte character sequence of str, with the capability of restarting in the middle of a multi-byte character if need be, examining at most maxSize bytes. The mbstate_t argument mbstate is used to keep track of the shift state. If it is NULL, mbrlen uses an internal, static mbstate_t object. It is equivalent to:
mbrtowc(NULL, str, maxSize, mbstate)
Except when the case of mbstate is NULL, mbrlen relies upon its own static, internal mbstate_t object to keep track of the shift state.
The mbrlen function differs from _mbclen, mblen, _mblen_l by its restartability. The conversion state is stored in mbstate for subsequent calls to the same or other restartable functions. Results are undefined when mixing the use of restartable and nonrestartable functions. For example, an application would use wcsrlen rather than wcslen, if a subsequent call to wcsrtombs where used instead of wcstombs.
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
|---|---|---|---|
n/a | n/a | mbrlen | n/a |
Routine | Required header |
|---|---|
mbrlen | <wchar.h> |
For additional compatibility information, see Compatibility in the Introduction.
// crt_mbrlen.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
size_t Example(const char * pStr)
{
size_t charLen = 0;
size_t charCount = 0;
mbstate_t mbState;
memset(&mbState, 0, sizeof(mbState));
while ((charLen = mbrlen(pStr, MB_CUR_MAX, &mbState)) != 0 &&
charLen != (size_t)-1 && charLen != (size_t)-2)
{
pStr += charLen;
charCount++;
}
return (charCount);
}
int main( void )
{
size_t charCount = 0;
const char *pSample = "Every good boy does fine.";
charCount = Example(pSample);
printf("%s\nLength: %d\n", pSample, charCount);
}
Every good boy does fine. Length: 25