Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/wcwidth/bisearch.py: 20%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

15 statements  

1"""Binary search function for Unicode interval tables.""" 

2 

3from __future__ import annotations 

4 

5 

6def bisearch(ucs: int, table: tuple[tuple[int, int], ...]) -> int: 

7 """ 

8 Binary search in interval table. 

9 

10 :param ucs: Ordinal value of unicode character. 

11 :param table: Tuple of starting and ending ranges of ordinal values, in form of ``((start, end), 

12 ...)``. 

13 :returns: 1 if ordinal value ucs is found within lookup table, else 0. 

14 """ 

15 lbound = 0 

16 ubound = len(table) - 1 

17 

18 if ucs < table[0][0] or ucs > table[ubound][1]: 

19 return 0 

20 

21 while ubound >= lbound: 

22 mid = (lbound + ubound) // 2 

23 if ucs > table[mid][1]: 

24 lbound = mid + 1 

25 elif ucs < table[mid][0]: 

26 ubound = mid - 1 

27 else: 

28 return 1 

29 

30 return 0