-
Notifications
You must be signed in to change notification settings - Fork 0
/
unicode_age.pyx.in
39 lines (28 loc) · 1.15 KB
/
unicode_age.pyx.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cdef extern from "unicode_age.h":
struct versionSpan:
int start
int stop
char major
char minor
# Cython requires a hardcoded size here
versionSpan[$numSpans] versionSpans
NUM_SPANS = $numSpans
cdef _version(codept):
# A linear scan over known version spans is probably fine? There are "only"
# ~2000 of them in 15.0
# NOTE: I want to do `for span in versionSpans` here but the generated C
# confuses the compiler (error messages about the unknown size of the
# versionSpan wrapper)
for n in range(NUM_SPANS):
if versionSpans[n].start <= codept <= versionSpans[n].stop:
return versionSpans[n].major, versionSpans[n].minor
# linear scan failed
raise ValueError("Not found")
def version(codept: int) -> tuple:
"""
Return a tuple `(major, minor)` indicating the Unicode version assigned to the given codepoint
If there is no version assigned to the codepoint, ValueError will be raised
"""
if not isinstance(codept, int):
raise TypeError(f"Input must be the integer value of a single codepoint (got {type(codept)!r})")
return _version(codept)