Skip to content

Commit a38cd71

Browse files
committed
translate: share vstr_add_chr's implementation of utf8 encoding
this saves ~44 bytes on trinket_m0
1 parent 3c8f9f4 commit a38cd71

File tree

1 file changed

+17
-28
lines changed

1 file changed

+17
-28
lines changed

supervisor/shared/translate/translate.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,37 +57,23 @@ STATIC void get_word(int n, const mchar_t **pos, const mchar_t **end) {
5757
*end = *pos + len;
5858
}
5959

60-
STATIC int put_utf8(char *buf, int u) {
60+
STATIC void put_utf8(vstr_t *vstr, int u) {
6161
if (u >= translation_offstart) {
6262
u += translation_offset;
6363
}
64-
if (u <= 0x7f) {
65-
*buf = u;
66-
return 1;
67-
} else if (word_start <= u && u <= word_end) {
64+
if (word_start <= u && u <= word_end) {
6865
uint n = (u - word_start);
6966
const mchar_t *pos, *end;
7067
get_word(n, &pos, &end);
71-
int ret = 0;
7268
// note that at present, entries in the words table are
7369
// guaranteed not to represent words themselves, so this adds
7470
// at most 1 level of recursive call
7571
for (; pos < end; pos++) {
76-
int len = put_utf8(buf, *pos);
77-
buf += len;
78-
ret += len;
72+
put_utf8(vstr, *pos);
7973
}
80-
return ret;
81-
} else if (u <= 0x07ff) {
82-
*buf++ = 0b11000000 | (u >> 6);
83-
*buf = 0b10000000 | (u & 0b00111111);
84-
return 2;
85-
} else { // u <= 0xffff
86-
*buf++ = 0b11100000 | (u >> 12);
87-
*buf++ = 0b10000000 | ((u >> 6) & 0b00111111);
88-
*buf = 0b10000000 | (u & 0b00111111);
89-
return 3;
74+
return;
9075
}
76+
vstr_add_char(vstr, u);
9177
}
9278

9379
uint16_t decompress_length(mp_rom_error_text_t compressed) {
@@ -123,15 +109,15 @@ static int get_nbits(bitstream_state_t *st, int n) {
123109
return r;
124110
}
125111

126-
char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
112+
static void decompress_vstr(mp_rom_error_text_t compressed, vstr_t *decompressed) {
127113
bitstream_state_t b = {
128114
.ptr = &(compressed->data) + (compress_max_length_bits >> 3),
129115
.bit = 1 << (7 - ((compress_max_length_bits) & 0x7)),
130116
};
131-
uint16_t length = decompress_length(compressed);
132117

118+
size_t alloc = decompressed->alloc - 1;
133119
// Stop one early because the last byte is always NULL.
134-
for (uint16_t i = 0; i < length - 1;) {
120+
for (; decompressed->len < alloc;) {
135121
uint32_t bits = 0;
136122
uint8_t bit_length = 0;
137123
uint32_t max_code = lengths[0];
@@ -148,16 +134,19 @@ char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
148134
int v = values[searched_length + bits - max_code];
149135
if (v == 1) {
150136
qstr q = get_nbits(&b, translation_qstr_bits) + 1; // honestly no idea why "+1"...
151-
for (const char *qc = qstr_str(q); *qc;) {
152-
decompressed[i++] = *qc++;
153-
}
137+
vstr_add_str(decompressed, qstr_str(q));
154138
} else {
155-
i += put_utf8(decompressed + i, v);
139+
put_utf8(decompressed, v);
156140
}
157141
}
142+
}
158143

159-
decompressed[length - 1] = '\0';
160-
return decompressed;
144+
145+
char *decompress(mp_rom_error_text_t compressed, char *decompressed) {
146+
vstr_t vstr;
147+
vstr_init_fixed_buf(&vstr, decompress_length(compressed), decompressed);
148+
decompress_vstr(compressed, &vstr);
149+
return vstr_null_terminated_str(&vstr);
161150
}
162151

163152
#if CIRCUITPY_TRANSLATE_OBJECT == 1

0 commit comments

Comments
 (0)