xref: /relibc/tests/wchar/wcrtomb.c (revision ed19381547d66b76981ea1e4ff942c5a4da45ab7)
1 #include <string.h>
2 #include <stdio.h>
3 #include <wchar.h>
4 #include <stdlib.h>
5 
6 #include "test_helpers.h"
7 
8 int main(void) {
9     mbstate_t state;
10     memset(&state, 0, sizeof state);
11     wchar_t in[] = L"zß水��"; // or "z\u00df\u6c34\U0001F34C"
12     size_t in_sz = sizeof in / sizeof *in;
13 
14     printf("Processing %zu wchar_t units: [ ", in_sz);
15     for(size_t n = 0; n < in_sz; ++n) printf("%#x ", (unsigned int)in[n]);
16     puts("]");
17 
18     char out[MB_CUR_MAX * in_sz];
19     char *p = out;
20     for(size_t n = 0; n < in_sz; ++n) {
21         int rc = wcrtomb(p, in[n], &state);
22         if(rc == -1) break;
23         p += rc;
24     }
25 
26     size_t out_sz = p - out;
27     printf("into %zu UTF-8 code units: [ ", out_sz);
28     for(size_t x = 0; x < out_sz; ++x) printf("%#x ", +(unsigned char)out[x]);
29     puts("]");
30 }
31