mbrtoc16.c 726 B

123456789101112131415161718192021222324252627282930
  1. #include <uchar.h>
  2. #include <wchar.h>
  3. size_t mbrtoc16(char16_t *restrict pc16, const char *restrict s, size_t n, mbstate_t *restrict ps)
  4. {
  5. static unsigned internal_state;
  6. if (!ps) ps = (void *)&internal_state;
  7. unsigned *pending = (unsigned *)ps;
  8. if (!s) return mbrtoc16(0, "", 1, ps);
  9. /* mbrtowc states for partial UTF-8 characters have the high bit set;
  10. * we use nonzero states without high bit for pending surrogates. */
  11. if ((int)*pending > 0) {
  12. if (pc16) *pc16 = *pending;
  13. *pending = 0;
  14. return -3;
  15. }
  16. wchar_t wc;
  17. size_t ret = mbrtowc(&wc, s, n, ps);
  18. if (ret <= 4) {
  19. if (wc >= 0x10000) {
  20. *pending = (wc & 0x3ff) + 0xdc00;
  21. wc = 0xd7c0 + (wc >> 10);
  22. }
  23. if (pc16) *pc16 = wc;
  24. }
  25. return ret;
  26. }