mbrtowc.c 860 B

12345678910111213141516171819202122232425262728293031
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <wchar.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. mbstate_t state;
  7. memset(&state, 0, sizeof state);
  8. char in[] = u8"z\u00df\u6c34\U0001F34C"; // or u8"zß水🍌"
  9. size_t in_sz = sizeof in / sizeof *in;
  10. printf("Processing %zu UTF-8 code units: [ ", in_sz); //Should be 11 regular chars
  11. for(size_t n = 0; n < in_sz; ++n) printf("%#x ", (unsigned char)in[n]);
  12. puts("]");
  13. wchar_t out[in_sz];
  14. char *p_in = in, *end = in + in_sz;
  15. wchar_t *p_out = out;
  16. int rc;
  17. while((rc = mbrtowc(p_out, p_in, end - p_in, &state)) > 0)
  18. {
  19. p_in += rc;
  20. p_out += 1;
  21. }
  22. size_t out_sz = p_out - out + 1;
  23. printf("into %zu wchar_t units: [ ", out_sz); //Should be 5 wchars
  24. for(size_t x = 0; x < out_sz; ++x) printf("%#x ", out[x]);
  25. puts("]");
  26. }