From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/string%2Fmultibyte%2Fmbrlen.html | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 devdocs/c/string%2Fmultibyte%2Fmbrlen.html (limited to 'devdocs/c/string%2Fmultibyte%2Fmbrlen.html') diff --git a/devdocs/c/string%2Fmultibyte%2Fmbrlen.html b/devdocs/c/string%2Fmultibyte%2Fmbrlen.html new file mode 100644 index 00000000..cf7c2914 --- /dev/null +++ b/devdocs/c/string%2Fmultibyte%2Fmbrlen.html @@ -0,0 +1,58 @@ +

mbrlen

Defined in header <wchar.h>
size_t mbrlen( const char *s, size_t n, mbstate_t *ps );
+
(since C95)
(until C99)
size_t mbrlen( const char *restrict s, size_t n, mbstate_t *restrict ps );
+
(since C99)

Determines the size, in bytes, of the representation of a multibyte character.

+

This function is equivalent to the call mbrtowc(NULL, s, n, ps?ps:&internal) for some hidden object internal of type mbstate_t, except that the expression ps is evaluated only once.

+

Parameters

+ + + +
s - pointer to an element of a multibyte character string
n - limit on the number of bytes in s that can be examined
ps - pointer to the variable holding the conversion state

Return value

The first of the following that applies:

+

Example

#include <locale.h>
+#include <string.h>
+#include <stdio.h>
+#include <wchar.h>
+ 
+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(&mb, 0, sizeof mb);
+    int len1 = mbrlen(str, 1, &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, &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, &mb));
+}

Output:

+
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

References

See also

+ + +
+
(C95)
converts the next multibyte character to wide character, given state
(function)
returns the number of bytes in the next multibyte character
(function)
C++ documentation for mbrlen
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/string/multibyte/mbrlen +

+
-- cgit v1.2.3