From: Max Kellermann Date: Sat, 10 Oct 2009 16:39:37 +0000 (+0200) Subject: list_window: moved checks to list_window_validate_index() X-Git-Tag: release-0.16~128 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0a496668d56253b0a03646041ae0978c4233c9a4;p=ncmpc.git list_window: moved checks to list_window_validate_index() --- diff --git a/src/list_window.c b/src/list_window.c index bca8a19..c78ff6e 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -66,28 +66,30 @@ list_window_reset(struct list_window *lw) lw->start = 0; } +static unsigned +list_window_validate_index(const struct list_window *lw, unsigned i) +{ + if (lw->length == 0) + return 0; + else if (i >= lw->length) + return lw->length - 1; + else + return i; +} + static void list_window_check_selected(struct list_window *lw) { - if (lw->length == 0) - lw->selected = 0; - else if (lw->selected >= lw->length) - lw->selected = lw->length - 1; + lw->selected = list_window_validate_index(lw, lw->selected); if(lw->range_selection) { - if (lw->length == 0) { - lw->selected_start = 0; - lw->selected_end = 0; - lw->range_base = 0; - } else { - if (lw->selected_start >= lw->length) - lw->selected_start = lw->length - 1; - if (lw->selected_end >= lw->length) - lw->selected_end = lw->length - 1; - if (lw->range_base >= lw->length) - lw->range_base = lw->length - 1; - } + lw->selected_start = + list_window_validate_index(lw, lw->selected_start); + lw->selected_end = + list_window_validate_index(lw, lw->selected_end); + lw->range_base = + list_window_validate_index(lw, lw->range_base); if(lw->range_base > lw->selected_end) lw->selected_end = lw->selected;