Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

29 lines
570 B

  1. #include "fmt.h"
  2. size_t fmt_utf8(char *dest,uint32_t n) {
  3. size_t i,j;
  4. if (n<=0x7f) {
  5. if (dest) *dest=(char)n;
  6. return 1;
  7. }
  8. for (i=0x3f,j=1; i<=0x7fffffff; i=(i<<5)|0x1f, ++j) {
  9. if (i>=n) {
  10. --j;
  11. if (dest) {
  12. size_t k=j*6;
  13. *dest++=(char)(((char)0xc0 >> (j-1)) | (char)(n >> k));
  14. while (k) {
  15. *dest++=(char)(0x80 | ((n >> (k-6)) & 0x3f));
  16. k-=6;
  17. }
  18. }
  19. return j+1;
  20. }
  21. if (i==0x7fffffff) return 0;
  22. }
  23. /* we were asked to encode a value that cannot be encoded */
  24. return 0;
  25. }
  26. /* unit tested via scan/scan_utf8.c */