codekingpro/portable-devtools
115k
1"""Binary search function for Unicode interval tables."""2 3from __future__ import annotations4 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 = 016 ubound = len(table) - 117 18 if ucs < table[0][0] or ucs > table[ubound][1]:19 return 020 21 while ubound >= lbound:22 mid = (lbound + ubound) // 223 if ucs > table[mid][1]:24 lbound = mid + 125 elif ucs < table[mid][0]:26 ubound = mid - 127 else:28 return 129 30 return 031 