decode_utf8_to_wide_string.c 1019 B

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * Decode a string of UTF-8 to a wchar_t string.
  3. */
  4. #include "misc.h"
  5. wchar_t *decode_utf8_to_wide_string(const char *s)
  6. {
  7. wchar_t *ws = NULL;
  8. size_t wlen = 0, wsize = 0;
  9. BinarySource src[1];
  10. BinarySource_BARE_INIT_PL(src, ptrlen_from_asciz(s));
  11. while (get_avail(src) > 0) {
  12. /*
  13. * decode_utf8_to_wchar might emit up to 2 wchar_t if wchar_t
  14. * is 16 bits (because of UTF-16 surrogates), but will emit at
  15. * most one if wchar_t is 32-bit
  16. */
  17. sgrowarrayn(ws, wsize, wlen, 1 + (sizeof(wchar_t) < 4));
  18. /* We ignore 'err': if it is set, then the character decode
  19. * function will have emitted U+FFFD REPLACEMENT CHARACTER,
  20. * which is what we'd have done in response anyway. */
  21. DecodeUTF8Failure err;
  22. wlen += decode_utf8_to_wchar(src, ws + wlen, &err);
  23. }
  24. /* Reallocate to the final size and append the trailing NUL */
  25. ws = sresize(ws, wlen + 1, wchar_t);
  26. ws[wlen] = L'\0';
  27. return ws;
  28. }