#include <stdint.h>
|
|
#include <stddef.h>
|
|
#include "unicode.h"
|
|
|
|
size_t utf8_encode(char *str, uint32_t ch) {
|
|
size_t len = 0;
|
|
uint8_t first;
|
|
|
|
if (ch < 0x80) {
|
|
first = 0;
|
|
len = 1;
|
|
} else if (ch < 0x800) {
|
|
first = 0xc0;
|
|
len = 2;
|
|
} else if (ch < 0x10000) {
|
|
first = 0xe0;
|
|
len = 3;
|
|
} else {
|
|
first = 0xf0;
|
|
len = 4;
|
|
}
|
|
|
|
for (size_t i = len - 1; i > 0; --i) {
|
|
str[i] = (ch & 0x3f) | 0x80;
|
|
ch >>= 6;
|
|
}
|
|
|
|
str[0] = ch | first;
|
|
return len;
|
|
}
|