-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
42 lines (35 loc) · 1.09 KB
/
Copy pathhelper.py
File metadata and controls
42 lines (35 loc) · 1.09 KB
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
40
41
42
from libif import LTOKENIZE_PY
from libif import LV4
from libif import sizeofTOKEN
from libif import dbg, sizeofTOKEN
from constants import VTO, VTCC
# /////////////////////////////////////////////////////////////
# // helper
def utf8len(s: str, start: int, end: int) -> int:
utf8 = s[start:end].encode('utf-8') # bytes
return len(utf8)
def tokens_splice(t: bytes, a: int = 0, b: int = 0) -> bytes:
if 0 == b:
return t[a * sizeofTOKEN() :]
else:
return t[a * sizeofTOKEN() : b * sizeofTOKEN()]
def tokens_count(t: bytes) -> int:
return int(len(t) / sizeofTOKEN())
def tokens_pos(t: bytes, i: int) -> int:
if not i < tokens_count(t):
return -1
return t[i*sizeofTOKEN()]
def tokens_get(t: bytes, s: str, i: int) -> int:
if not i < tokens_count(t):
return ''
pos = t[i*sizeofTOKEN()]; leng = t[i*sizeofTOKEN() +1]
utf8 = s.encode('utf-8') # bytes
retval = utf8[pos:pos+leng].decode('utf-8')
return retval
def tokens_to_list(t: bytes, s: str) -> list:
tokens = []
i = 0
for b in range(0, len(t), sizeofTOKEN()):
pos = t[b]; tok = t[b+1]
tokens.append(s[pos:pos+tok])
return tokens