Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
bisearch.py31 linesDownload Raw Back to wcwidth
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 
codekingpro/portable-devtools · Team Ai