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.""" 

2from __future__ import annotations 

3 

4 

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

6 """ 

7 Binary search in interval table. 

8 

9 :param ucs: Ordinal value of unicode character. 

10 :param table: Tuple of starting and ending ranges of ordinal values, 

11 in form of ``((start, end), ...)``. 

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

13 """ 

14 lbound = 0 

15 ubound = len(table) - 1 

16 

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

18 return 0 

19 

20 while ubound >= lbound: 

21 mid = (lbound + ubound) // 2 

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

23 lbound = mid + 1 

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

25 ubound = mid - 1 

26 else: 

27 return 1 

28 

29 return 0