Get the cell size of a character. Args: character (str): A single character. unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version. Returns: int: Number of cells (0, 1 or 2) occupied by that character.
(character: str, unicode_version: str = "auto")
| 45 | |
| 46 | @lru_cache(maxsize=4096) |
| 47 | def get_character_cell_size(character: str, unicode_version: str = "auto") -> int: |
| 48 | """Get the cell size of a character. |
| 49 | |
| 50 | Args: |
| 51 | character (str): A single character. |
| 52 | unicode_version: Unicode version, `"auto"` to auto detect, `"latest"` for the latest unicode version. |
| 53 | |
| 54 | Returns: |
| 55 | int: Number of cells (0, 1 or 2) occupied by that character. |
| 56 | """ |
| 57 | codepoint = ord(character) |
| 58 | if codepoint and codepoint < 32 or 0x07F <= codepoint < 0x0A0: |
| 59 | return 0 |
| 60 | table = load_cell_table(unicode_version).widths |
| 61 | |
| 62 | last_entry = table[-1] |
| 63 | if codepoint > last_entry[1]: |
| 64 | return 1 |
| 65 | |
| 66 | lower_bound = 0 |
| 67 | upper_bound = len(table) - 1 |
| 68 | |
| 69 | while lower_bound <= upper_bound: |
| 70 | index = (lower_bound + upper_bound) >> 1 |
| 71 | start, end, width = table[index] |
| 72 | if codepoint < start: |
| 73 | upper_bound = index - 1 |
| 74 | elif codepoint > end: |
| 75 | lower_bound = index + 1 |
| 76 | else: |
| 77 | return width |
| 78 | return 1 |
| 79 | |
| 80 | |
| 81 | @lru_cache(4096) |
no outgoing calls