1 /* demo_utf8towc() against libc mbrtowc()
2 *
3 * Copyright 2017 Rob Landley <rob@landley.net>
4
5 USE_DEMO_UTF8TOWC(NEWTOY(demo_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN))
6
7 config DEMO_UTF8TOWC
8 bool "demo_utf8towc"
9 default n
10 help
11 usage: demo_utf8towc
12
13 Print differences between toybox's utf8 conversion routines vs libc du jour.
14 */
15
16 #include "toys.h"
17
demo_utf8towc_main(void)18 void demo_utf8towc_main(void)
19 {
20 mbstate_t mb;
21 int len1, len2;
22 unsigned u, h;
23 wchar_t wc1, wc2;
24
25 setlocale(LC_ALL, "en_US.UTF-8");
26
27 memset(&mb, 0, sizeof(mb));
28 for (u=1; u; u++) {
29 char *str = (void *)&h;
30
31 wc1 = wc2 = 0;
32 len2 = 4;
33 h = htonl(u);
34 while (!*str) str++, len2--;
35
36 len1 = mbrtowc(&wc1, str, len2, &mb);
37 if (len1<0) memset(&mb, 0, sizeof(mb));
38 len2 = utf8towc(&wc2, str, len2);
39 if (len1 != len2 || wc1 != wc2)
40 printf("%x %d %x %d %x\n", u, len1, wc1, len2, wc2);
41 }
42 }
43