From 0483466f68467694e960497e5f33cf0bf2f2cb90 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 28 Jun 2020 10:45:05 +0200 Subject: sixel: insert: calculate end rows in a single statement --- sixel.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/sixel.c b/sixel.c index ddb0091c..beab0896 100644 --- a/sixel.c +++ b/sixel.c @@ -93,23 +93,36 @@ sixel_insert(struct terminal *term, struct sixel sixel) const int scrollback_end = (term->grid->offset + term->rows) & (term->grid->num_rows - 1); - int end_row = sixel.pos.row + sixel.rows - scrollback_end; - while (end_row < 0) - end_row += term->grid->num_rows; + const int end_row + = (sixel.pos.row + sixel.rows + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + assert(end_row >= 0 && end_row < term->grid->num_rows); tll_foreach(term->grid->sixel_images, it) { - int e = it->item.pos.row + it->item.rows - scrollback_end; - e -= scrollback_end; - while (e < 0) - e += term->grid->num_rows; + const int e + = (it->item.pos.row + it->item.rows + - scrollback_end + + term->grid->num_rows) & (term->grid->num_rows - 1); + assert(e >= 0 && e < term->grid->num_rows); if (e < end_row) { tll_insert_before(term->grid->sixel_images, it, sixel); - return; + goto out; } } tll_push_back(term->grid->sixel_images, sixel); + +out: +#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG + LOG_DBG("sixel list after insertion:"); + tll_foreach(term->grid->sixel_images, it) { + LOG_DBG(" rows=%d+%d", it->item.pos.row, it->item.rows); + } +#else + ; +#endif } /* Row numbers are absolute */ -- cgit v1.3