Commit e605af77 authored by Chris Müller's avatar Chris Müller
Browse files

add unicode->utf8 converter

parent a26613c2
......@@ -33,6 +33,8 @@ int org_cherry_unicode_isgraph(cy_unicode_t ch);
int org_cherry_unicode_ispunct(cy_unicode_t ch);
int org_cherry_unicode_iscntrl(cy_unicode_t ch);
int org_cherry_unicode_to_utf8(cy_byte_t* out, cy_unicode_t ch);
int org_cherry_utf8_validate(const cy_byte_t* str);
int org_cherry_utf8_compare(const cy_byte_t* str1, const cy_byte_t* str2);
......
......@@ -447,3 +447,43 @@ org_cherry_utf8_validate(const cy_byte_t* str)
return (trails == 0);
}
int
org_cherry_unicode_to_utf8(cy_byte_t* out, cy_unicode_t ch)
{
size_t length = 0;
cy_byte_t first;
int i;
if(ch < 0x80) {
first = 0x00;
length = 1;
} else if(ch < 0x800) {
first = 0xC0;
length = 2;
} else if(ch < 0x10000) {
first = 0xE0;
length = 3;
} else if(ch < 0x200000) {
first = 0xF0;
length = 4;
} else if(ch < 0x4000000) {
first = 0xF8;
length = 5;
} else {
first = 0xFC;
length = 6;
}
if(out) {
for(i = length - 1; i > 0; --i) {
out[i] = (ch & 0x3F) | 0x80;
ch >>= 6;
}
out[0] = ch | first;
}
return length;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment