2012-02-06 07:59:56 +00:00
|
|
|
#include "fmt.h"
|
|
|
|
|
|
|
|
size_t fmt_utf8(char *dest,uint32_t n) {
|
|
|
|
size_t i,j;
|
|
|
|
if (n<=0x7f) {
|
2014-02-25 18:39:59 +00:00
|
|
|
if (dest) *dest=(char)n;
|
2012-02-06 07:59:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-03-13 15:44:48 +00:00
|
|
|
for (i=0x3f,j=1; i<=0x7fffffff; i=(i<<5)|0x1f, ++j) {
|
2012-02-06 07:59:56 +00:00
|
|
|
if (i>=n) {
|
|
|
|
--j;
|
|
|
|
if (dest) {
|
|
|
|
size_t k=j*6;
|
2014-02-25 18:39:59 +00:00
|
|
|
*dest++=(char)(((char)0xc0 >> (j-1)) | (char)(n >> k));
|
2012-02-06 07:59:56 +00:00
|
|
|
while (k) {
|
2014-02-25 18:39:59 +00:00
|
|
|
*dest++=(char)(0x80 | ((n >> (k-6)) & 0x3f));
|
2012-02-06 07:59:56 +00:00
|
|
|
k-=6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return j+1;
|
|
|
|
}
|
2017-03-13 15:44:48 +00:00
|
|
|
if (i==0x7fffffff) return 0;
|
2012-02-06 07:59:56 +00:00
|
|
|
}
|
|
|
|
/* we were asked to encode a value that cannot be encoded */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-13 22:52:21 +00:00
|
|
|
/* unit tested via scan/scan_utf8.c */
|