mirror of https://github.com/rui314/chibicc
29 lines
636 B
C
29 lines
636 B
C
|
#include "chibicc.h"
|
||
|
|
||
|
// Encode a given character in UTF-8.
|
||
|
int encode_utf8(char *buf, uint32_t c) {
|
||
|
if (c <= 0x7F) {
|
||
|
buf[0] = c;
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
if (c <= 0x7FF) {
|
||
|
buf[0] = 0b11000000 | (c >> 6);
|
||
|
buf[1] = 0b10000000 | (c & 0b00111111);
|
||
|
return 2;
|
||
|
}
|
||
|
|
||
|
if (c <= 0xFFFF) {
|
||
|
buf[0] = 0b11100000 | (c >> 12);
|
||
|
buf[1] = 0b10000000 | ((c >> 6) & 0b00111111);
|
||
|
buf[2] = 0b10000000 | (c & 0b00111111);
|
||
|
return 3;
|
||
|
}
|
||
|
|
||
|
buf[0] = 0b11110000 | (c >> 18);
|
||
|
buf[1] = 0b10000000 | ((c >> 12) & 0b00111111);
|
||
|
buf[2] = 0b10000000 | ((c >> 6) & 0b00111111);
|
||
|
buf[3] = 0b10000000 | (c & 0b00111111);
|
||
|
return 4;
|
||
|
}
|