cortex
aien-dev/cortex
Brought by drakestapleton
cortex / tests / fts / gen_uni_tables.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generator (not product code) for the data tables in om/uni_fts.om. |
| 3 | |
| 4 | Copies, number for number, the static tables that SQLite 3.45.0's FTS5 unicode61 tokenizer |
| 5 | uses (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) |
| 10 | and prints Omega `const` offsets plus one text literal `qutx` of decimal values. |
| 11 | |
| 12 | Usage: python3 tests/fts/gen_uni_tables.py PATH/TO/sqlite3.c |
| 13 | """ |
| 14 | import hashlib, re, sys |
| 15 | |
| 16 | src = open(sys.argv[1], encoding='latin-1').read() |
| 17 | |
| 18 | |
| 19 | def 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 | |
| 26 | def nums(txt): |
| 27 | return [int(x) for x in re.findall(r'\b\d+\b', re.sub(r'/\*.*?\*/', '', txt, flags=re.S))] |
| 28 | |
| 29 | |
| 30 | def 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 | |
| 43 | blk = nums(body('static u16 aFts5UnicodeBlock[] =')[0]) |
| 44 | mp = nums(body('static u16 aFts5UnicodeMap[] =')[0]) |
| 45 | dt = nums(body('static u16 aFts5UnicodeData[] =')[0]) |
| 46 | fold_pos = src.index('static int sqlite3Fts5UnicodeFold(int c, int eRemoveDiacritic){') |
| 47 | ent_txt, _ = body('} aEntry[] =', fold_pos) |
| 48 | ent = re.findall(r'\{(\d+),\s*(\d+),\s*(\d+)\}', ent_txt) |
| 49 | aio = nums(body('static const unsigned short aiOff[] =', fold_pos)[0]) |
| 50 | dia_pos = src.index('static int fts5_remove_diacritic(int c, int bComplex){') |
| 51 | dia = nums(body('unsigned short aDia[] =', dia_pos)[0]) |
| 52 | ach = chars(body('unsigned char aChar[] =', dia_pos)[0]) |
| 53 | utf_pos = src.index('static const unsigned char sqlite3Utf8Trans1[] = {') |
| 54 | tr = [int(x, 16) for x in re.findall(r'0x([0-9a-f]{2})', body('static const unsigned char sqlite3Utf8Trans1[] =', utf_pos)[0])] |
| 55 | |
| 56 | assert len(blk) == 17 and len(mp) == len(dt) == 1765, (len(blk), len(mp), len(dt)) |
| 57 | assert len(ent) == 163 and len(aio) == 77 and len(dia) == 126 and len(ach) == 126 and len(tr) == 64 |
| 58 | |
| 59 | parts = [('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)] |
| 62 | print('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()) |
| 64 | off = 0 |
| 65 | allv = [] |
| 66 | for name, vals in parts: |
| 67 | print('const %s ulong %d' % (name, off)) |
| 68 | off += len(vals) |
| 69 | allv += vals |
| 70 | print('const QUN ulong %d' % off) |
| 71 | print('text qutx "' + ' '.join(str(v) for v in allv) + '"') |