10f66f451Sopenharmony_ci/* demo_utf8towc() against libc mbrtowc() 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2017 Rob Landley <rob@landley.net> 40f66f451Sopenharmony_ci 50f66f451Sopenharmony_ciUSE_DEMO_UTF8TOWC(NEWTOY(demo_utf8towc, 0, TOYFLAG_USR|TOYFLAG_BIN)) 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciconfig DEMO_UTF8TOWC 80f66f451Sopenharmony_ci bool "demo_utf8towc" 90f66f451Sopenharmony_ci default n 100f66f451Sopenharmony_ci help 110f66f451Sopenharmony_ci usage: demo_utf8towc 120f66f451Sopenharmony_ci 130f66f451Sopenharmony_ci Print differences between toybox's utf8 conversion routines vs libc du jour. 140f66f451Sopenharmony_ci*/ 150f66f451Sopenharmony_ci 160f66f451Sopenharmony_ci#include "toys.h" 170f66f451Sopenharmony_ci 180f66f451Sopenharmony_civoid demo_utf8towc_main(void) 190f66f451Sopenharmony_ci{ 200f66f451Sopenharmony_ci mbstate_t mb; 210f66f451Sopenharmony_ci int len1, len2; 220f66f451Sopenharmony_ci unsigned u, h, wc2; 230f66f451Sopenharmony_ci wchar_t wc1; 240f66f451Sopenharmony_ci 250f66f451Sopenharmony_ci memset(&mb, 0, sizeof(mb)); 260f66f451Sopenharmony_ci for (u = 1; u<=0x10ffff; u++) { 270f66f451Sopenharmony_ci char *str = (void *)&h; 280f66f451Sopenharmony_ci 290f66f451Sopenharmony_ci wc1 = wc2 = 0; 300f66f451Sopenharmony_ci len2 = 4; 310f66f451Sopenharmony_ci h = htonl(u); 320f66f451Sopenharmony_ci while (!*str) str++, len2--; 330f66f451Sopenharmony_ci 340f66f451Sopenharmony_ci len1 = mbrtowc(&wc1, str, len2, &mb); 350f66f451Sopenharmony_ci if (len1<0) memset(&mb, 0, sizeof(mb)); 360f66f451Sopenharmony_ci len2 = utf8towc(&wc2, str, len2); 370f66f451Sopenharmony_ci if (len1 != len2 || wc1 != wc2) 380f66f451Sopenharmony_ci printf("%x %d %x %d %x\n", u, len1, wc1, len2, wc2); 390f66f451Sopenharmony_ci } 400f66f451Sopenharmony_ci} 41