|
26 | 26 |
|
27 | 27 | #include "shared-module/usb/utf16le.h" |
28 | 28 |
|
29 | | -STATIC void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { |
30 | | - // TODO: Check for runover. |
31 | | - (void)utf8_len; |
| 29 | +typedef struct { |
| 30 | + const uint16_t *buf; |
| 31 | + size_t len; |
| 32 | +} utf16_str; |
32 | 33 |
|
33 | | - for (size_t i = 0; i < utf16_len; i++) { |
34 | | - uint16_t chr = utf16[i]; |
35 | | - if (chr < 0x80) { |
36 | | - *utf8++ = chr & 0xff; |
37 | | - } else if (chr < 0x800) { |
38 | | - *utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F)); |
39 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
40 | | - } else if (chr < 0x10000) { |
41 | | - // TODO: Verify surrogate. |
42 | | - *utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F)); |
43 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); |
44 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
45 | | - } else { |
46 | | - // TODO: Handle UTF-16 code points that take two entries. |
47 | | - uint32_t hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */ |
48 | | - chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */ |
49 | | - chr = (hc | chr) + 0x10000; |
50 | | - *utf8++ = (uint8_t)(0xF0 | (chr >> 18 & 0x07)); |
51 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 12 & 0x3F)); |
52 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); |
53 | | - *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
54 | | - } |
| 34 | +STATIC uint32_t utf16str_peek_unit(utf16_str *utf) { |
| 35 | + if (!utf->len) { |
| 36 | + return 0; |
55 | 37 | } |
| 38 | + return *utf->buf; |
56 | 39 | } |
57 | 40 |
|
58 | | -// Count how many bytes a utf-16-le encoded string will take in utf-8. |
59 | | -STATIC mp_int_t _count_utf8_bytes(const uint16_t *buf, size_t len) { |
60 | | - size_t total_bytes = 0; |
61 | | - for (size_t i = 0; i < len; i++) { |
62 | | - uint16_t chr = buf[i]; |
63 | | - if (chr < 0x80) { |
64 | | - total_bytes += 1; |
65 | | - } else if (chr < 0x800) { |
66 | | - total_bytes += 2; |
67 | | - } else if (chr < 0x10000) { |
68 | | - total_bytes += 3; |
69 | | - } else { |
70 | | - total_bytes += 4; |
| 41 | +STATIC uint32_t utf16str_next_unit(utf16_str *utf) { |
| 42 | + uint32_t result = utf16str_peek_unit(utf); |
| 43 | + if (utf->len) { |
| 44 | + utf->len--; |
| 45 | + utf->buf++; |
| 46 | + } |
| 47 | + return result; |
| 48 | +} |
| 49 | +STATIC uint32_t utf16str_next_codepoint(utf16_str *utf) { |
| 50 | + uint32_t unichr = utf16str_next_unit(utf); |
| 51 | + if (unichr >= 0xd800 && unichr < 0xdc00) { |
| 52 | + uint32_t low_surrogate = utf16str_peek_unit(utf); |
| 53 | + if (low_surrogate >= 0xdc00 && low_surrogate < 0xe000) { |
| 54 | + (void)utf16str_next_unit(utf); |
| 55 | + unichr = (unichr - 0xd800) * 0x400 + low_surrogate - 0xdc00 + 0x10000; |
71 | 56 | } |
72 | 57 | } |
73 | | - return total_bytes; |
| 58 | + return unichr; |
| 59 | +} |
| 60 | + |
| 61 | +STATIC void _convert_utf16le_to_utf8(vstr_t *vstr, utf16_str *utf) { |
| 62 | + while (utf->len) { |
| 63 | + vstr_add_char(vstr, utf16str_next_codepoint(utf)); |
| 64 | + } |
74 | 65 | } |
75 | 66 |
|
76 | 67 | mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) { |
77 | | - size_t size = _count_utf8_bytes(buf, utf16_len); |
| 68 | + // will grow if necessary, but will never grow for an all-ASCII descriptor |
78 | 69 | vstr_t vstr; |
79 | | - vstr_init_len(&vstr, size + 1); |
80 | | - byte *p = (byte *)vstr.buf; |
81 | | - // Null terminate. |
82 | | - p[size] = '\0'; |
83 | | - _convert_utf16le_to_utf8(buf, utf16_len, p, size); |
| 70 | + vstr_init(&vstr, utf16_len); |
| 71 | + utf16_str utf = {buf, utf16_len}; |
| 72 | + _convert_utf16le_to_utf8(&vstr, &utf); |
84 | 73 | return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
85 | 74 | } |
0 commit comments