Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752605AbdIERel (ORCPT ); Tue, 5 Sep 2017 13:34:41 -0400 Received: from www17.your-server.de ([213.133.104.17]:44265 "EHLO www17.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751955AbdIERei (ORCPT ); Tue, 5 Sep 2017 13:34:38 -0400 From: Thomas Meyer To: linux-kernel@vger.kernel.org Cc: Thomas Meyer Subject: [PATCH] vt: Use bsearch library function in is_double_width Date: Tue, 5 Sep 2017 19:34:12 +0200 Message-Id: <20170905173412.9471-1-thomas@m3y3r.de> X-Mailer: git-send-email 2.11.0 X-Authenticated-Sender: thomas@m3y3r.de Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1747 Lines: 65 Use bsearch library function instead of duplicated functionality. Signed-off-by: Thomas Meyer --- drivers/tty/vt/vt.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 2ebaba16f785..ca55004a639e 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -102,6 +102,7 @@ #include #include #include +#include #define MAX_NR_CON_DRIVER 16 @@ -2142,22 +2143,15 @@ struct interval { uint32_t last; }; -static int bisearch(uint32_t ucs, const struct interval *table, int max) +static int ucs_cmp(const void *key, const void *elt) { - int min = 0; - int mid; + uint32_t ucs = *(uint32_t *)key; + struct interval e = *(struct interval *) elt; - if (ucs < table[0].first || ucs > table[max].last) - return 0; - while (max >= min) { - mid = (min + max) / 2; - if (ucs > table[mid].last) - min = mid + 1; - else if (ucs < table[mid].first) - max = mid - 1; - else - return 1; - } + if (ucs > e.last) + return 1; + else if (ucs < e.first) + return -1; return 0; } @@ -2169,7 +2163,12 @@ static int is_double_width(uint32_t ucs) { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 }, { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD } }; - return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1); + if (ucs < double_width[0].first || + ucs > double_width[ARRAY_SIZE(double_width) - 1].last) + return 0; + + return bsearch(&ucs, double_width, ARRAY_SIZE(double_width), + sizeof(struct interval), ucs_cmp) != NULL; } static void con_flush(struct vc_data *vc, unsigned long draw_from, -- 2.11.0