interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / fts / gen_uni_tables.py

71 lines · 2977 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Generator (not product code) for the data tables in om/uni_fts.om.
3
4Copies, number for number, the static tables that SQLite 3.45.0's FTS5 unicode61 tokenizer
5uses (sqlite3.c from libsqlite3-sys-0.28.0):
6 aFts5UnicodeBlock, aFts5UnicodeMap, aFts5UnicodeData (sqlite3Fts5UnicodeCategory)
7 aEntry{iCode,flags,nRange}, aiOff (sqlite3Fts5UnicodeFold)
8 aDia, aChar (fts5_remove_diacritic)
9 sqlite3Utf8Trans1 (READ_UTF8, utf.c copy)
10and prints Omega `const` offsets plus one text literal `qutx` of decimal values.
11
12Usage: python3 tests/fts/gen_uni_tables.py PATH/TO/sqlite3.c
13"""
14import hashlib, re, sys
15
16src = open(sys.argv[1], encoding='latin-1').read()
17
18
19def body(start_pat, from_pos=0):
20 i = src.index(start_pat, from_pos)
21 j = src.index('{', i)
22 k = src.index('};', j)
23 return src[j + 1:k], k
24
25
26def nums(txt):
27 return [int(x) for x in re.findall(r'\b\d+\b', re.sub(r'/\*.*?\*/', '', txt, flags=re.S))]
28
29
30def chars(txt):
31 out = []
32 for tok in txt.split(','):
33 tok = tok.strip()
34 if not tok:
35 continue
36 hib = '|HIBIT' in tok
37 tok = tok.replace('|HIBIT', '')
38 v = 0 if tok == "'\\0'" else ord(tok.strip("'"))
39 out.append(v | (0x80 if hib else 0))
40 return out
41
42
43blk = nums(body('static u16 aFts5UnicodeBlock[] =')[0])
44mp = nums(body('static u16 aFts5UnicodeMap[] =')[0])
45dt = nums(body('static u16 aFts5UnicodeData[] =')[0])
46fold_pos = src.index('static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){')
47ent_txt, _ = body('} aEntry[] =', fold_pos)
48ent = re.findall(r'\{(\d+),\s*(\d+),\s*(\d+)\}', ent_txt)
49aio = nums(body('static const unsigned short aiOff[] =', fold_pos)[0])
50dia_pos = src.index('static int fts5_remove_diacritic(int c, int bComplex){')
51dia = nums(body('unsigned short aDia[] =', dia_pos)[0])
52ach = chars(body('unsigned char aChar[] =', dia_pos)[0])
53utf_pos = src.index('static const unsigned char sqlite3Utf8Trans1[] = {')
54tr = [int(x, 16) for x in re.findall(r'0x([0-9a-f]{2})', body('static const unsigned char sqlite3Utf8Trans1[] =', utf_pos)[0])]
55
56assert len(blk) == 17 and len(mp) == len(dt) == 1765, (len(blk), len(mp), len(dt))
57assert len(ent) == 163 and len(aio) == 77 and len(dia) == 126 and len(ach) == 126 and len(tr) == 64
58
59parts = [('QUB', blk), ('QUM', mp), ('QUD', dt),
60 ('QUFC', [int(e[0]) for e in ent]), ('QUFF', [int(e[1]) for e in ent]), ('QUFN', [int(e[2]) for e in ent]),
61 ('QUAO', aio), ('QUDI', dia), ('QUDC', ach), ('QUTR', tr)]
62print('note generated by tests/fts/gen_uni_tables.py from sqlite3.c sha256 %s (SQLite 3.45.0)'
63 % hashlib.sha256(src.encode('latin-1')).hexdigest())
64off = 0
65allv = []
66for name, vals in parts:
67 print('const %s ulong %d' % (name, off))
68 off += len(vals)
69 allv += vals
70print('const QUN ulong %d' % off)
71print('text qutx "' + ' '.join(str(v) for v in allv) + '"')