encode_utf8.c 746 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Encode a single code point as UTF-8.
  3. */
  4. #include "defs.h"
  5. #include "misc.h"
  6. void BinarySink_put_utf8_char(BinarySink *output, unsigned ch)
  7. {
  8. if (ch < 0x80) {
  9. put_byte(output, ch);
  10. } else if (ch < 0x800) {
  11. put_byte(output, 0xC0 | (ch >> 6));
  12. put_byte(output, 0x80 | (ch & 0x3F));
  13. } else if (ch < 0x10000) {
  14. put_byte(output, 0xE0 | (ch >> 12));
  15. put_byte(output, 0x80 | ((ch >> 6) & 0x3F));
  16. put_byte(output, 0x80 | (ch & 0x3F));
  17. } else {
  18. assert(ch <= 0x10FFFF);
  19. put_byte(output, 0xF0 | (ch >> 18));
  20. put_byte(output, 0x80 | ((ch >> 12) & 0x3F));
  21. put_byte(output, 0x80 | ((ch >> 6) & 0x3F));
  22. put_byte(output, 0x80 | (ch & 0x3F));
  23. }
  24. }