2016-10-18 11:21:50 -07:00
|
|
|
#include "private.h"
|
2012-06-12 03:10:01 -07:00
|
|
|
#include "utf8.h"
|
|
|
|
|
2012-06-19 12:59:35 -07:00
|
|
|
int
|
2014-03-25 13:34:48 -07:00
|
|
|
codepoint_to_utf8(Eina_Unicode g, char *txt)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
|
|
|
if (g < (1 << (7)))
|
|
|
|
{ // 0xxxxxxx
|
|
|
|
txt[0] = g & 0x7f;
|
|
|
|
txt[1] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 1;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else if (g < (1 << (5 + 6)))
|
|
|
|
{ // 110xxxxx 10xxxxxx
|
|
|
|
txt[0] = 0xc0 | ((g >> 6) & 0x1f);
|
|
|
|
txt[1] = 0x80 | ((g ) & 0x3f);
|
|
|
|
txt[2] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 2;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else if (g < (1 << (4 + 6 + 6)))
|
|
|
|
{ // 1110xxxx 10xxxxxx 10xxxxxx
|
|
|
|
txt[0] = 0xe0 | ((g >> 12) & 0x0f);
|
|
|
|
txt[1] = 0x80 | ((g >> 6 ) & 0x3f);
|
|
|
|
txt[2] = 0x80 | ((g ) & 0x3f);
|
|
|
|
txt[3] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 3;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else if (g < (1 << (3 + 6 + 6 + 6)))
|
|
|
|
{ // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
txt[0] = 0xf0 | ((g >> 18) & 0x07);
|
|
|
|
txt[1] = 0x80 | ((g >> 12) & 0x3f);
|
|
|
|
txt[2] = 0x80 | ((g >> 6 ) & 0x3f);
|
|
|
|
txt[3] = 0x80 | ((g ) & 0x3f);
|
|
|
|
txt[4] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 4;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else if (g < (1 << (2 + 6 + 6 + 6 + 6)))
|
|
|
|
{ // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
txt[0] = 0xf8 | ((g >> 24) & 0x03);
|
|
|
|
txt[1] = 0x80 | ((g >> 18) & 0x3f);
|
|
|
|
txt[2] = 0x80 | ((g >> 12) & 0x3f);
|
|
|
|
txt[3] = 0x80 | ((g >> 6 ) & 0x3f);
|
|
|
|
txt[4] = 0x80 | ((g ) & 0x3f);
|
|
|
|
txt[5] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 5;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2014-03-25 13:35:57 -07:00
|
|
|
else if ((unsigned int)g < (unsigned int)(1 << (1 + 6 + 6 + 6 + 6 + 6)))
|
2012-06-12 03:10:01 -07:00
|
|
|
{ // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
txt[0] = 0xfc | ((g >> 30) & 0x01);
|
|
|
|
txt[1] = 0x80 | ((g >> 24) & 0x3f);
|
|
|
|
txt[2] = 0x80 | ((g >> 18) & 0x3f);
|
|
|
|
txt[3] = 0x80 | ((g >> 12) & 0x3f);
|
|
|
|
txt[4] = 0x80 | ((g >> 6 ) & 0x3f);
|
|
|
|
txt[5] = 0x80 | ((g ) & 0x3f);
|
|
|
|
txt[6] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 6;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // error - cant encode this in utf8
|
|
|
|
txt[0] = 0;
|
2012-06-19 12:59:35 -07:00
|
|
|
return 0;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
}
|