8000 small (170 bytes) code size optimization · FoamyGuy/circuitpython@8813308 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8813308

Browse files
committed
small (170 bytes) code size optimization
1 parent 3515a38 commit 8813308

File tree

1 file changed

+43
-72
lines changed

1 file changed

+43
-72
lines changed

shared-module/terminalio/Terminal.c

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -114,43 +114,30 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
114114
}
115115
} else if (c == 0x1b) {
116116
// Handle commands of the form [ESC].<digits><command-char> where . is not yet known.
117-
uint16_t n = 0;
117+
uint8_t vt_args[3] = {0,-1,-1};
118+
uint8_t n_args = 1;
118119
uint8_t j = 1;
119120
for (; j < 6; j++) {
120121
if ('0' <= i[j] && i[j] <= '9') {
121-
n = n * 10 + (i[j] - '0');
122+
vt_args[0] = vt_args[0] * 10 + (i[j] - '0');
122123
} else {
123124
c = i[j];
124125
break;
125126
}
126127
}
127128
if (i[0] == '[') {
128-
uint16_t m = -1;
129-
if (c == ';') {
130-
m = 0;
129+
for (uint8_t i_args = 1; i_args < 3 && c == ';'; i_args++) {
130+
vt_args[i_args] = 0;
131131
for (++j; j < 9; j++) {
132132
if ('0' <= i[j] && i[j] <= '9') {
133-
m = m * 10 + (i[j] - '0');
133+
vt_args[i_args] = vt_args[i_args] * 10 + (i[j] - '0');
134+
n_args = i_args + 1;
134135
} else {
135136
c = i[j];
136137
break;
137138
}
138139
}
139140
}
140-
#if CIRCUITPY_TERMINALIO_VT100
141-
uint8_t m2 = -1;
142-
if (c == ';') {
143-
m2 = 0;
144-
for (++j; j < 12; j++) {
145-
if ('0' <= i[j] && i[j] <= '9') {
146-
m2 = m2 * 10 + (i[j] - '0');
147-
} else {
148-
c = i[j];
149-
break;
150-
}
151-
}
152-
}
153-
#endif
154141
if (c == '?') {
155142
#if CIRCUITPY_TERMINALIO_VT100
156143
if (i[2] == '2' && i[3] == '5') {
@@ -167,78 +154,62 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
167154
#endif
168155
} else {
169156
if (c == 'K') {
170-
if (n == 0) {
171-
// Clear the rest of the line.
172-
for (uint16_t k = self->cursor_x; k < self->scroll_area->width_in_tiles; k++) {
173-
common_hal_displayio_tilegrid_set_tile(self->scroll_area, k, self->cursor_y, 0);
174-
}
157+
uint8_t clr_start = self->cursor_x;
158+
uint8_t clr_end = self->scroll_area->width_in_tiles;
175159
#if CIRCUITPY_TERMINALIO_VT100
176-
} else if (n == 1) {
177-
// Clear start to cursor
178-
for (uint16_t k = 0; k < self->cursor_x; k++) {
179-
common_hal_displayio_tilegrid_set_tile(self->scroll_area, k, self->cursor_y, 0);
180-
}
181-
} else if (n == 2) {
182-
// Clear entire line
183-
for (uint16_t k = 0; k < self->scroll_area->width_in_tiles; k++) {
184-
common_hal_displayio_tilegrid_set_tile(self->scroll_area, k, self->cursor_y, 0);
185-
}
160+
if (vt_args[0] == 1) {
161+
clr_start = 0;
162+
clr_end = self->cursor_x;
163+
} else if (vt_args[0] == 2) {
164+
clr_start = 0;
165+
}
186166
#endif
167+
// Clear the (start/rest/all) of the line.
168+
for (uint16_t k = clr_start; k < clr_end; k++) {
169+
common_hal_displayio_tilegrid_set_tile(self->scroll_area, k, self->cursor_y, 0);
187170
}
188171
} else if (c == 'D') {
189-
if (n > self->cursor_x) {
172+
if (vt_args[0] > self->cursor_x) {
190173
self->cursor_x = 0;
191174
} else {
192-
self->cursor_x -= n;
175+
self->cursor_x -= vt_args[0];
193176
}
194177
} else if (c == 'J') {
195-
if (n == 2) {
178+
if (vt_args[0] == 2) {
196179
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, 0);
197180
self->cursor_x = self->cursor_y = start_y = 0;
198181
common_hal_displayio_tilegrid_set_all_tiles(self->scroll_area, 0);
199182
}
200183
} else if (c == 'H') {
201-
if (n > 0) {
202-
n--;
184+
if (vt_args[0] > 0) {
185+
vt_args[0]--;
203186
}
204-
if (m == -1) {
205-
m = 0;
187+
if (vt_args[1] == -1) {
188+
vt_args[1] = 0;
206189
}
207-
if (m > 0) {
208-
m--;
190+
if (vt_args[1] > 0) {
191+
vt_args[1]--;
209192
}
210-
if (n >= self->scroll_area->height_in_tiles) {
211-
n = self->scroll_area->height_in_tiles - 1;
193+
if (vt_args[0] >= self->scroll_area->height_in_tiles) {
194+
vt_args[0] = self->scroll_area->height_in_tiles - 1;
212195
}
213-
if (m >= self->scroll_area->width_in_tiles) {
214-
m = self->scroll_area->width_in_tiles - 1;
196+
if (vt_args[1] >= self->scroll_area->width_in_tiles) {
197+
vt_args[1] = self->scroll_area->width_in_tiles - 1;
215198
}
216-
n = (n + self->scroll_area->top_left_y) % self->scroll_area->height_in_tiles;
217-
self->cursor_x = m;
218-
self->cursor_y = n;
199+
vt_args[0] = (vt_args[0] + self->scroll_area->top_left_y) % self->scroll_area->height_in_tiles;
200+
self->cursor_x = vt_args[1];
201+
self->cursor_y = vt_args[0];
219202
start_y = self->cursor_y;
220203
#if CIRCUITPY_TERMINALIO_VT100
221204
} else if (c == 'm') {
222-
if ((n >= 40 && n <= 47) || (n >= 30 && n <= 37)) {
223-
common_hal_displayio_palette_set_color(terminal_palette, 1 - (n / 40), _select_color(n % 10));
224-
}
225-
if (n == 0) {
226-
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
227-
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
228-
}
229-
if ((m >= 40 && m <= 47) || (m >= 30 && m <= 37)) {
230-
common_hal_displayio_palette_set_color(terminal_palette, 1 - (m / 40), _select_color(m % 10));
231-
}
232-
if (m == 0) {
233-
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
234-
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
235-
}
236-
if ((m2 >= 40 && m2 <= 47) || (m2 >= 30 && m2 <= 37)) {
237-
common_hal_displayio_palette_set_color(terminal_palette, 1 - (m2 / 40), _select_color(m2 % 10));
238-
}
239-
if (m2 == 0) {
240-
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
241-
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
205+
for (uint8_t i_args = 0; i_args < n_args; i_args++) {
206+
if ((vt_args[i_args] >= 40 && vt_args[i_args] <= 47) || (vt_args[i_args] >= 30 && vt_args[i_args] <= 37)) {
207+
common_hal_displayio_palette_set_color(terminal_palette, 1 - (vt_args[i_args] / 40), _select_color(vt_args[i_args] % 10));
208+
}
209+
if (vt_args[i_args] == 0) {
210+
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
211+
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
212+
}
242213
}
243214
#endif
244215
}
@@ -281,7 +252,7 @@ size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, con
281252
#endif
282253
} else if (i[0] == ']' && c == ';') {
283254
self->in_osc_command = true;
284-
self->osc_command = n;
255+
self->osc_command = vt_args[0];
285256
i += j + 1;
286257
}
287258
}

0 commit comments

Comments
 (0)
0