Add wuffs_base__utf_8__encode

This commit is contained in:
Nigel Tao committed 2020-03-03 12:44:01 +11:00
1 parent 6892095998
commit b9ad34fad3
5 files changed
+114 -47

No files matched your search

+45
View File
@@ -3512,6 +3512,15 @@ wuffs_base__utf_8__next__output::is_valid() const {
// --------
// wuffs_base__utf_8__encode writes the UTF-8 encoding of code_point to s and
// returns the number of bytes written. If code_point is invalid, or if s is
// shorter than the entire encoding, it returns 0 (and no bytes are written).
//
// s will never be too short if its length is at least 4, also known as
// WUFFS_BASE__UTF_8__BYTE_LENGTH__MAX_INCL.
size_t //
wuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point);
// wuffs_base__utf_8__next returns the next UTF-8 code point (and that code
// point's byte length) at the start of s.
//
@@ -8612,6 +8621,42 @@ fail_out_of_bounds:
// ---------------- Unicode and UTF-8
size_t //
wuffs_base__utf_8__encode(wuffs_base__slice_u8 dst, uint32_t code_point) {
if (code_point <= 0x7F) {
if (dst.len >= 1) {
dst.ptr[0] = (uint8_t)(code_point);
return 1;
}
} else if (code_point <= 0x07FF) {
if (dst.len >= 2) {
dst.ptr[0] = 0xC0 | (uint8_t)((code_point >> 6));
dst.ptr[1] = 0x80 | (uint8_t)((code_point >> 0) & 0x3F);
return 2;
}
} else if (code_point <= 0xFFFF) {
if ((dst.len >= 3) && ((code_point < 0xD800) || (0xDFFF < code_point))) {
dst.ptr[0] = 0xE0 | (uint8_t)((code_point >> 12));
dst.ptr[1] = 0x80 | (uint8_t)((code_point >> 6) & 0x3F);
dst.ptr[2] = 0x80 | (uint8_t)((code_point >> 0) & 0x3F);
return 3;
}
} else if (code_point <= 0x10FFFF) {
if (dst.len >= 4) {
dst.ptr[0] = 0xF0 | (uint8_t)((code_point >> 18));
dst.ptr[1] = 0x80 | (uint8_t)((code_point >> 12) & 0x3F);
dst.ptr[2] = 0x80 | (uint8_t)((code_point >> 6) & 0x3F);
dst.ptr[3] = 0x80 | (uint8_t)((code_point >> 0) & 0x3F);
return 4;
}
}
return 0;
}
// wuffs_base__utf_8__byte_length_minus_1 is the byte length (minus 1) of a
// UTF-8 encoded code point, based on the encoding's initial byte.
// - 0x00 is 1-byte UTF-8 (ASCII).