Merge pull request #8655 from jepler/save-flash-share-utf8
Save flash by sharing the 'to utf-8' implementation
This commit is contained in:
commit
1cfb894c50
|
@ -137,30 +137,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
|
|||
STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
|
||||
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
||||
mp_uint_t c = mp_obj_get_int(o_in);
|
||||
uint8_t str[4];
|
||||
int len = 0;
|
||||
if (c < 0x80) {
|
||||
*str = c;
|
||||
len = 1;
|
||||
} else if (c < 0x800) {
|
||||
str[0] = (c >> 6) | 0xC0;
|
||||
str[1] = (c & 0x3F) | 0x80;
|
||||
len = 2;
|
||||
} else if (c < 0x10000) {
|
||||
str[0] = (c >> 12) | 0xE0;
|
||||
str[1] = ((c >> 6) & 0x3F) | 0x80;
|
||||
str[2] = (c & 0x3F) | 0x80;
|
||||
len = 3;
|
||||
} else if (c < 0x110000) {
|
||||
str[0] = (c >> 18) | 0xF0;
|
||||
str[1] = ((c >> 12) & 0x3F) | 0x80;
|
||||
str[2] = ((c >> 6) & 0x3F) | 0x80;
|
||||
str[3] = (c & 0x3F) | 0x80;
|
||||
len = 4;
|
||||
} else {
|
||||
if (c >= 0x110000) {
|
||||
mp_raise_ValueError(MP_ERROR_TEXT("chr() arg not in range(0x110000)"));
|
||||
}
|
||||
return mp_obj_new_str_via_qstr((char *)str, len);
|
||||
VSTR_FIXED(buf, 4);
|
||||
vstr_add_char(&buf, c);
|
||||
return mp_obj_new_str_via_qstr(buf.buf, buf.len);
|
||||
#else
|
||||
mp_int_t ord = mp_obj_get_int(o_in);
|
||||
if (0 <= ord && ord <= 0xff) {
|
||||
|
|
|
@ -57,37 +57,23 @@ STATIC void get_word(int n, const mchar_t **pos, const mchar_t **end) {
|
|||
*end = *pos + len;
|
||||
}
|
||||
|
||||
STATIC int put_utf8(char *buf, int u) {
|
||||
STATIC void put_utf8(vstr_t *vstr, int u) {
|
||||
if (u >= translation_offstart) {
|
||||
u += translation_offset;
|
||||
}
|
||||
if (u <= 0x7f) {
|
||||
*buf = u;
|
||||
return 1;
|
||||
} else if (word_start <= u && u <= word_end) {
|
||||
if (word_start <= u && u <= word_end) {
|
||||
uint n = (u - word_start);
|
||||
const mchar_t *pos, *end;
|
||||
get_word(n, &pos, &end);
|
||||
int ret = 0;
|
||||
// note that at present, entries in the words table are
|
||||
// guaranteed not to represent words themselves, so this adds
|
||||
// at most 1 level of recursive call
|
||||
for (; pos < end; pos++) {
|
||||
int len = put_utf8(buf, *pos);
|
||||
buf += len;
|
||||
ret += len;
|
||||
put_utf8(vstr, *pos);
|
||||
}
|
||||
return ret;
|
||||
} else if (u <= 0x07ff) {
|
||||
*buf++ = 0b11000000 | (u >> 6);
|
||||
*buf = 0b10000000 | (u & 0b00111111);
|
||||
return 2;
|
||||
} else { // u <= 0xffff
|
||||
*buf++ = 0b11100000 | (u >> 12);
|
||||
*buf++ = 0b10000000 | ((u >> 6) & 0b00111111);
|
||||
*buf = 0b10000000 | (u & 0b00111111);
|
||||
return 3;
|
||||
return;
|
||||
}
|
||||
vstr_add_char(vstr, u);
|
||||
}
|
||||
|
||||
uint16_t decompress_length(mp_rom_error_text_t compressed) {
|
||||
|
@ -123,15 +109,15 @@ static int get_nbits(bitstream_state_t *st, int n) {
|
|||
return r;
|
||||
}
|
||||
|
||||
char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
|
||||
static void decompress_vstr(mp_rom_error_text_t compressed, vstr_t *decompressed) {
|
||||
bitstream_state_t b = {
|
||||
.ptr = &(compressed->data) + (compress_max_length_bits >> 3),
|
||||
.bit = 1 << (7 - ((compress_max_length_bits) & 0x7)),
|
||||
};
|
||||
uint16_t length = decompress_length(compressed);
|
||||
|
||||
size_t alloc = decompressed->alloc - 1;
|
||||
// Stop one early because the last byte is always NULL.
|
||||
for (uint16_t i = 0; i < length - 1;) {
|
||||
for (; decompressed->len < alloc;) {
|
||||
uint32_t bits = 0;
|
||||
uint8_t bit_length = 0;
|
||||
uint32_t max_code = lengths[0];
|
||||
|
@ -148,16 +134,19 @@ char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
|
|||
int v = values[searched_length + bits - max_code];
|
||||
if (v == 1) {
|
||||
qstr q = get_nbits(&b, translation_qstr_bits) + 1; // honestly no idea why "+1"...
|
||||
for (const char *qc = qstr_str(q); *qc;) {
|
||||
decompressed[i++] = *qc++;
|
||||
}
|
||||
vstr_add_str(decompressed, qstr_str(q));
|
||||
} else {
|
||||
i += put_utf8(decompressed + i, v);
|
||||
put_utf8(decompressed, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
decompressed[length - 1] = '\0';
|
||||
return decompressed;
|
||||
|
||||
char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
|
||||
vstr_t vstr;
|
||||
vstr_init_fixed_buf(&vstr, decompress_length(compressed), decompressed);
|
||||
decompress_vstr(compressed, &vstr);
|
||||
return vstr_null_terminated_str(&vstr);
|
||||
}
|
||||
|
||||
#if CIRCUITPY_TRANSLATE_OBJECT == 1
|
||||
|
|
Loading…
Reference in New Issue