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