From 1aec6113a6dbae0878f849675ec9d1417cb6a787 Mon Sep 17 00:00:00 2001 From: bptato Date: Sat, 19 Feb 2022 12:12:49 +0100 Subject: twtstr: replace bisearch with stdlib binarySearch --- src/utils/twtstr.nim | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index df8cfa6e..a48f80b2 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -1,3 +1,4 @@ +import algorithm import terminal import strutils import unicode @@ -760,28 +761,9 @@ const ambiguous = [ # to UCS without changing the traditional terminal character-width behaviour. # It is not otherwise recommended for general use. -# auxiliary function for binary search in interval table -func bisearch(ucs: Rune, table: openarray[(int, int)]): bool = - var max = table.high - var min = 0 - var mid: int - - if int(ucs) < table[0][0] or int(ucs) > table[max][1]: - return false - - while max >= min: - mid = (min + max) div 2 - if int(ucs) > table[mid][1]: - min = mid + 1 - elif int(ucs) < table[mid][0]: - max = mid - 1 - else: - return true - return false - func is_dwidth_cjk(r: Rune): bool = # binary search in table of non-spacing characters - if bisearch(r, ambiguous): + if binarySearch(ambiguous, int32(r), (x, y) => (if x[0] < y: -1 elif x[1] > y: 1 else: 0)) != -1: return true return r.is_dwidth() -- cgit 1.4.1-2-gfad0