interplane.Get Omega
Read in your language

cortex-rs

Pure compiled native Rust memory engine with vector search and SQLite WAL

Brought by drakestapleton

branch main Branches Tags History

cortex-rs / om / like.om

277 lines · 4997 bytes Raw Blame History
1note ===== like.om : SQLite 3.45.0 LIKE (likeFunc + patternCompare), case_sensitive_like off =====
2note Owner FtsEngine. Globals q*, functions like_*.
3note `X LIKE P` with no ESCAPE: '%' any run, '_' one UTF-8 character (sqlite3Utf8Read rules,
4note including invalid sequences), ASCII-only case folding, both strings end at their first NUL.
5note API
6note like_set -> bool pattern bytes qlp[0..qlpl); false when SQLite raises
7note "LIKE or GLOB pattern too complex" (qlpl > 50000, the default
8note SQLITE_LIMIT_LIKE_PATTERN_LENGTH); cortex-rs then fails the request
9note like_test -> bool subject bytes qct[0..qctl) (the same buffer fts_put reads content
10note from); true when SQLite's like() returns 1
11note like_lit -> bool after like_set: true when the pattern is '%' L '%' with L one or more
12note bytes in 1..127 other than '%' and '_' (cortex-rs's substring fallback)
13note like_find -> bool for such a pattern, like_test's answer by a byte search: X LIKE P is
14note "X, up to its first NUL, holds L with ASCII letters folded". An ASCII
15note byte is always a whole character for sqlite3Utf8Read and never equals
16note a byte of a longer one, so the byte search and patternCompare agree.
17global qlp uchar 1048576
18global qlpl ulong 1
19global qlpe ulong 1
20global qlok ulong 1
21global qlq ulong 1
22global qlln ulong 1
23fn like_set to bool
24uf_init
25n = get qlpl 0
26if gt n 1048576
27put qlok 0 0
28ret false
29end
30e = n
31i = ulong 0
32for a ulong 2
33dn = false
34for j ulong 999999
35if ge i n
36dn = true
37break
38end
39if eq get qlp i 0
40e = i
41dn = true
42break
43end
44i = add i 1
45end
46if dn
47break
48end
49end
50put qlpe 0 e
51if gt n 50000
52put qlok 0 0
53ret false
54end
55put qlok 0 1
56ret true
57end
58fn like_test to bool
59if eq get qlok 0 0
60ret false
61end
62ret eq like_cmp 0 0 0
63end
64note byte i of the pattern (src 0) or the subject (src 1); 0 past the end
65fn like_b src ulong i ulong to ulong
66if eq src 0
67if lt i get qlpe 0
68ret ulong get qlp i
69end
70ret 0
71end
72if lt i get qctl 0
73ret ulong get qct i
74end
75ret 0
76end
77note sqlite3Utf8Read at i; returns the character, the next index in qlq
78fn like_rd src ulong i ulong to ulong
79c = like_b src i
80k = add i 1
81if ge c 192
82c = get qutab add QUTR sub c 192
83for a ulong 17
84dn = false
85for j ulong 999999
86b = like_b src k
87if ne and b 192 128
88dn = true
89break
90end
91c = and add shl c 6 and b 63 4294967295
92k = add k 1
93end
94if dn
95break
96end
97end
98if or lt c 128 or eq and c 4294965248 55296 eq and c 4294967294 65534
99c = ulong 65533
100end
101end
102put qlq 0 k
103ret c
104end
105fn like_up c ulong to ulong
106if and ge c 97 le c 122
107ret sub c 32
108end
109ret c
110end
111note patternCompare(zPattern + p, zString + s): 0 MATCH, 1 NOMATCH, 2 NOWILDCARDMATCH
112fn like_cmp p0 ulong s0 ulong to ulong
113p = p0
114s = s0
115for a ulong 17
116for j ulong 999999
117c = like_rd 0 p
118p = get qlq 0
119if eq c 0
120if eq like_b 1 s 0
121ret 0
122end
123ret 1
124end
125if eq c 37
126ret like_all p s
127end
128c2 = like_rd 1 s
129s = get qlq 0
130if ne c c2
131if not and and eq fts_lower c fts_lower c2 lt c 128 lt c2 128
132if not and eq c 95 ne c2 0
133ret 1
134end
135end
136end
137end
138end
139trap 9
140end
141note the '%' branch of patternCompare; p is just past the '%'
142fn like_all p0 ulong s0 ulong to ulong
143p = p0
144s = s0
145c = ulong 0
146for a ulong 17
147dn = false
148for j ulong 999999
149c = like_rd 0 p
150p = get qlq 0
151if eq c 95
152if eq like_rd 1 s 0
153ret 2
154end
155s = get qlq 0
156else
157if ne c 37
158dn = true
159break
160end
161end
162end
163if dn
164break
165end
166end
167if eq c 0
168ret 0
169end
170if lt c 128
171ret like_scan p s like_up c fts_lower c
172end
173for a ulong 17
174for j ulong 999999
175c2 = like_rd 1 s
176s = get qlq 0
177if eq c2 0
178ret 2
179end
180if eq c2 c
181r = like_cmp p s
182if ne r 1
183ret r
184end
185end
186end
187end
188trap 9
189end
190note strcspn-driven search for an ASCII stop character (either case)
191fn like_scan p ulong s0 ulong u ulong l ulong to ulong
192s = s0
193for a ulong 17
194for j ulong 999999
195b = like_b 1 s
196if eq b 0
197ret 2
198end
199s = add s 1
200if or eq b u eq b l
201r = like_cmp p s
202if ne r 1
203ret r
204end
205end
206end
207end
208trap 9
209end
210fn like_lit to bool
211put qlln 0 0
212e = get qlpe 0
213if or eq get qlok 0 0 lt e 3
214ret false
215end
216if or ne get qlp 0 37 ne get qlp sub e 1 37
217ret false
218end
219i = ulong 1
220for a ulong 4611686018427387904
221if ge i sub e 1
222break
223end
224c = ulong get qlp i
225if or or ge c 128 eq c 37 eq c 95
226ret false
227end
228i = add i 1
229end
230put qlln 0 sub e 2
231ret true
232end
233fn like_find to bool
234m = get qlln 0
235n = get qctl 0
236if lt n m
237ret false
238end
239l0 = fts_lower ulong get qlp 1
240u0 = like_up l0
241last = sub n m
242i = ulong 0
243for a ulong 4611686018427387904
244if gt i last
245ret false
246end
247b = ulong get qct i
248if eq b 0
249ret false
250end
251if or eq b l0 eq b u0
252if like_rest i m
253ret true
254end
255end
256i = add i 1
257end
258ret false
259end
260note the bytes after the first of L match at subject position i (a NUL never matches)
261fn like_rest i ulong m ulong to bool
262k = ulong 1
263for a ulong 4611686018427387904
264if ge k m
265ret true
266end
267b = ulong get qct add i k
268if and ge b 65 le b 90
269b = add b 32
270end
271if ne b fts_lower ulong get qlp add 1 k
272ret false
273end
274k = add k 1
275end
276ret true
277end