Fossil SCM

Make the full-scan search and the timeline text filter work with non-ASCII text. Make case folding use Unicode, and search recognise word breaks in scripts and punctuation beyond ASCII. Picked up when doing libfossil Unicode fixes.

danshearer 2026-11-16 11:36 UTC trunk
Commit 51b87a12acbb2ca9ecbcbdb1aeb94f82ce27dc1e9d06ab301466db542374aff2
+3
--- src/db.c
+++ src/db.c
@@ -1646,10 +1646,13 @@
16461646
chat_msg_from_event, 0, 0);
16471647
sqlite3_create_function(db, "inode", 1, SQLITE_UTF8, 0,
16481648
file_inode_sql_func,0,0);
16491649
sqlite3_create_function(db, "artifact_to_json", 1, SQLITE_UTF8, 0,
16501650
artifact_to_json_sql_func,0,0);
1651
+ sqlite3_create_function(db, "unicode_contains", 2,
1652
+ SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
1653
+ unicode_contains_sqlfunc,0,0);
16511654
16521655
}
16531656
16541657
#if USE_SEE
16551658
/*
16561659
--- src/db.c
+++ src/db.c
@@ -1646,10 +1646,13 @@
1646 chat_msg_from_event, 0, 0);
1647 sqlite3_create_function(db, "inode", 1, SQLITE_UTF8, 0,
1648 file_inode_sql_func,0,0);
1649 sqlite3_create_function(db, "artifact_to_json", 1, SQLITE_UTF8, 0,
1650 artifact_to_json_sql_func,0,0);
 
 
 
1651
1652 }
1653
1654 #if USE_SEE
1655 /*
1656
--- src/db.c
+++ src/db.c
@@ -1646,10 +1646,13 @@
1646 chat_msg_from_event, 0, 0);
1647 sqlite3_create_function(db, "inode", 1, SQLITE_UTF8, 0,
1648 file_inode_sql_func,0,0);
1649 sqlite3_create_function(db, "artifact_to_json", 1, SQLITE_UTF8, 0,
1650 artifact_to_json_sql_func,0,0);
1651 sqlite3_create_function(db, "unicode_contains", 2,
1652 SQLITE_UTF8|SQLITE_DETERMINISTIC, 0,
1653 unicode_contains_sqlfunc,0,0);
1654
1655 }
1656
1657 #if USE_SEE
1658 /*
1659
+218 -19
--- src/search.c
+++ src/search.c
@@ -93,10 +93,174 @@
9393
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9494
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9595
};
9696
#define ISALNUM(x) (!isBoundary[(x)&0xff])
9797
98
+/*
99
+** Classes of non-ASCII characters returned by search_char_class()
100
+*/
101
+#define SCHAR_WORD 0 /* Part of a word, or not a character start */
102
+#define SCHAR_UNSPACED 1 /* Letter of a script written without spaces */
103
+#define SCHAR_SEPARATOR 2 /* Punctuation or space that separates words */
104
+
105
+/*
106
+** Return the number of bytes in the UTF-8 character that starts at z.
107
+*/
108
+static int search_utf8_len(const char *z){
109
+ int n = 1;
110
+ if( (z[0]&0xc0)==0xc0 ){
111
+ while( n<4 && (z[n]&0xc0)==0x80 ) n++;
112
+ }
113
+ return n;
114
+}
115
+
116
+/*
117
+** Classify the character that starts at z.
118
+**
119
+** Chinese, Japanese, Thai, Lao, Khmer and Burmese text has no spaces
120
+** between words, and finding word boundaries in it needs a dictionary
121
+** (Unicode UAX #29). The full-scan search instead lets a match begin
122
+** and end at any character of those scripts. The spaces and
123
+** punctuation listed below separate words: Latin-1 punctuation, the
124
+** General Punctuation, CJK and fullwidth punctuation blocks, the
125
+** Vertical Forms and CJK Compatibility Forms blocks (punctuation for
126
+** vertical text), and Arabic and Devanagari punctuation. Other
127
+** characters outside ASCII, including punctuation of other scripts such
128
+** as Armenian and Ethiopic, are treated as part of a word.
129
+*/
130
+static int search_char_class(const char *z){
131
+ const unsigned char *u = (const unsigned char*)z;
132
+ u32 c;
133
+ if( u[0]<0xc0 ) return SCHAR_WORD;
134
+ c = fossil_utf8_read(&u);
135
+ if( c>=0xa0 && c<=0xbf ){
136
+ /* Latin-1 punctuation and symbols, but not ª ² ³ µ ¹ º ¼ ½ ¾ */
137
+ if( c==0xaa || c==0xb2 || c==0xb3 || c==0xb5 || c==0xb9
138
+ || c==0xba || (c>=0xbc && c<=0xbe) ){
139
+ return SCHAR_WORD;
140
+ }
141
+ return SCHAR_SEPARATOR;
142
+ }
143
+ if( c==0xd7 || c==0xf7 ) return SCHAR_SEPARATOR; /* × ÷ */
144
+ if( c==0x60c || c==0x61b || c==0x61f || c==0x6d4 ){
145
+ return SCHAR_SEPARATOR; /* Arabic */
146
+ }
147
+ if( c==0x964 || c==0x965 ) return SCHAR_SEPARATOR; /* Danda */
148
+ if( c>=0x2000 && c<=0x206f ) return SCHAR_SEPARATOR;
149
+ if( c>=0x3000 && c<=0x303f ) return SCHAR_SEPARATOR;
150
+ if( (c>=0xfe10 && c<=0xfe19) || (c>=0xfe30 && c<=0xfe4f) ){
151
+ return SCHAR_SEPARATOR; /* Vertical forms */
152
+ }
153
+ if( (c>=0xff00 && c<=0xff0f) || (c>=0xff1a && c<=0xff20)
154
+ || (c>=0xff3b && c<=0xff40) || (c>=0xff5b && c<=0xff65) ){
155
+ return SCHAR_SEPARATOR; /* Fullwidth */
156
+ }
157
+ if( (c>=0x0e00 && c<=0x0eff) /* Thai, Lao */
158
+ || (c>=0x1000 && c<=0x109f) /* Myanmar */
159
+ || (c>=0x1780 && c<=0x17ff) /* Khmer */
160
+ || (c>=0x3040 && c<=0x30ff) /* Hiragana, Katakana */
161
+ || (c>=0x3400 && c<=0x4dbf) /* CJK Extension A */
162
+ || (c>=0x4e00 && c<=0x9fff) /* CJK Unified Ideographs */
163
+ || (c>=0xf900 && c<=0xfaff) /* CJK Compatibility Ideographs */
164
+ || (c>=0x20000 && c<=0x3ffff) /* CJK Extensions B and later */
165
+ ){
166
+ return SCHAR_UNSPACED;
167
+ }
168
+ return SCHAR_WORD;
169
+}
170
+
171
+/*
172
+** True if the byte at z can begin or continue a word
173
+*/
174
+#define ISWORD(z) (ISALNUM(*(z)) && search_char_class(z)!=SCHAR_SEPARATOR)
175
+
176
+/*
177
+** True if a match that ends just before z ends on a word boundary
178
+*/
179
+#define ISWORDEND(z) (!ISWORD(z) || search_char_class(z)==SCHAR_UNSPACED)
180
+
181
+/*
182
+** Compare the nTerm-byte search term zTerm against the text at zDoc,
183
+** ignoring case. Return the number of bytes of zDoc that match, which
184
+** can differ from nTerm when case folding changes the length of a
185
+** character, or -1 if the text does not match. A match must end on a
186
+** word boundary, unless its last character belongs to a script written
187
+** without spaces or the term ends with "*".
188
+*/
189
+static int search_term_match(const char *zTerm, int nTerm, const char *zDoc){
190
+ const unsigned char *t = (const unsigned char*)zTerm;
191
+ const unsigned char *tEnd = t + nTerm;
192
+ const unsigned char *d = (const unsigned char*)zDoc;
193
+ int bUnspaced = 0;
194
+ while( t<tEnd ){
195
+ if( t[0]<0x80 && d[0]<0x80 ){
196
+ if( fossil_tolower(t[0])!=fossil_tolower(d[0]) ) return -1;
197
+ t++;
198
+ d++;
199
+ bUnspaced = 0;
200
+ }else{
201
+ u32 ct, cd;
202
+ if( d[0]==0 ) return -1;
203
+ bUnspaced = search_char_class((const char*)t)==SCHAR_UNSPACED;
204
+ ct = fossil_utf8_read(&t);
205
+ cd = fossil_utf8_read(&d);
206
+ if( ct!=cd && unicode_fold((int)ct,0)!=unicode_fold((int)cd,0) ){
207
+ return -1;
208
+ }
209
+ }
210
+ }
211
+ if( !bUnspaced && !ISWORDEND((const char*)d) && *tEnd!='*' ){
212
+ return -1;
213
+ }
214
+ return (int)(d - (const unsigned char*)zDoc);
215
+}
216
+
217
+
218
+/*
219
+** Return true if zPat matches the start of zText, ignoring case with
220
+** Unicode case folding.
221
+*/
222
+static int unicode_prefix_nocase(
223
+ const unsigned char *zText,
224
+ const unsigned char *zPat
225
+){
226
+ while( *zPat ){
227
+ u32 a, b;
228
+ if( *zText==0 ) return 0;
229
+ a = fossil_utf8_read(&zText);
230
+ b = fossil_utf8_read(&zPat);
231
+ if( a!=b && unicode_fold((int)a,0)!=unicode_fold((int)b,0) ) return 0;
232
+ }
233
+ return 1;
234
+}
235
+
236
+/*
237
+** Implementation of the SQL function unicode_contains(TEXT,PATTERN),
238
+** which is true if PATTERN occurs in TEXT, ignoring case with Unicode
239
+** case folding. Use it in place of LIKE '%PATTERN%', which folds only
240
+** the ASCII letters A to Z.
241
+*/
242
+void unicode_contains_sqlfunc(
243
+ sqlite3_context *context,
244
+ int argc,
245
+ sqlite3_value **argv
246
+){
247
+ const unsigned char *zText = sqlite3_value_text(argv[0]);
248
+ const unsigned char *zPat = sqlite3_value_text(argv[1]);
249
+ int bFound = 0;
250
+ if( zText && zPat ){
251
+ while( 1 ){
252
+ if( unicode_prefix_nocase(zText, zPat) ){
253
+ bFound = 1;
254
+ break;
255
+ }
256
+ if( *zText==0 ) break;
257
+ zText += search_utf8_len((const char*)zText);
258
+ }
259
+ }
260
+ sqlite3_result_int(context, bFound);
261
+}
98262
99263
/*
100264
** Destroy a full-scan search context.
101265
*/
102266
void search_end(Search *p){
@@ -137,14 +301,14 @@
137301
p->zMarkEnd = mprintf("%s",zMarkEnd);
138302
p->zMarkGap = mprintf("%s",zMarkGap);
139303
p->fSrchFlg = fSrchFlg;
140304
blob_init(&p->snip, 0, 0);
141305
while( *z && p->nTerm<SEARCH_MAX_TERM ){
142
- while( *z && !ISALNUM(*z) ){ z++; }
306
+ while( *z && !ISWORD(z) ){ z += search_utf8_len(z); }
143307
if( *z==0 ) break;
144308
p->a[p->nTerm].z = z;
145
- for(i=1; ISALNUM(z[i]); i++){}
309
+ for(i=1; ISWORD(&z[i]); i++){}
146310
p->a[p->nTerm].n = i;
147311
z += i;
148312
p->nTerm++;
149313
}
150314
return p;
@@ -211,17 +375,19 @@
211375
for(iDoc=0; iDoc<nDoc; iDoc++){
212376
zDoc = azDoc[iDoc];
213377
if( zDoc==0 ) continue;
214378
iWord++;
215379
for(i=0; zDoc[i]; i++){
216
- if( !ISALNUM(zDoc[i]) ) continue;
380
+ int bUnspaced;
381
+ if( !ISWORD(&zDoc[i]) ){
382
+ i += search_utf8_len(&zDoc[i]) - 1;
383
+ continue;
384
+ }
385
+ bUnspaced = search_char_class(&zDoc[i])==SCHAR_UNSPACED;
217386
iWord++;
218387
for(j=0; j<p->nTerm; j++){
219
- int n = p->a[j].n;
220
- if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
221
- && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
222
- ){
388
+ if( search_term_match(p->a[j].z, p->a[j].n, &zDoc[i])>=0 ){
223389
aiWordIdx[j] = iWord;
224390
aiLastDoc[j] = iDoc;
225391
aiLastOfst[j] = i;
226392
for(k=1; j-k>=0 && anMatch[j-k] && aiWordIdx[j-k]==iWord-k; k++){}
227393
for(ii=0; ii<k; ii++){
@@ -232,12 +398,20 @@
232398
}
233399
}
234400
break;
235401
}
236402
}
237
- while( ISALNUM(zDoc[i]) ){ i++; }
403
+ if( bUnspaced ){
404
+ /* The next character can begin a match */
405
+ i += search_utf8_len(&zDoc[i]) - 1;
406
+ continue;
407
+ }
408
+ while( ISWORD(&zDoc[i]) && search_char_class(&zDoc[i])==SCHAR_WORD ){
409
+ i++;
410
+ }
238411
if( zDoc[i]==0 ) break;
412
+ if( ISALNUM(zDoc[i]) ) i--; /* Revisit a character outside ASCII */
239413
}
240414
}
241415
242416
/* Finished search all documents.
243417
** Every term must be seen or else the score is zero
@@ -283,33 +457,45 @@
283457
}
284458
}
285459
zDoc = azDoc[iDoc];
286460
iOfst -= CTX;
287461
if( iOfst<0 ) iOfst = 0;
288
- while( iOfst>0 && ISALNUM(zDoc[iOfst-1]) ) iOfst--;
289
- while( zDoc[iOfst] && !ISALNUM(zDoc[iOfst]) ) iOfst++;
462
+ while( iOfst>0 && ISALNUM(zDoc[iOfst-1])
463
+ && search_char_class(&zDoc[iOfst])==SCHAR_WORD ){
464
+ iOfst--;
465
+ }
466
+ while( zDoc[iOfst] && !ISWORD(&zDoc[iOfst]) ){
467
+ iOfst += search_utf8_len(&zDoc[iOfst]);
468
+ }
290469
for(ii=0; ii<CTX && zDoc[iTail]; ii++, iTail++){}
291
- while( ISALNUM(zDoc[iTail]) ) iTail++;
470
+ while( ISALNUM(zDoc[iTail])
471
+ && search_char_class(&zDoc[iTail])==SCHAR_WORD ){
472
+ iTail++;
473
+ }
292474
if( iOfst>0 || wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
293475
wantGap = zDoc[iTail]!=0;
294476
zDoc += iOfst;
295477
iTail -= iOfst;
296478
297479
/* Add a snippet segment using characters iOfst..iOfst+iTail from zDoc */
298480
for(i=0; i<iTail; i++){
299
- if( !ISALNUM(zDoc[i]) ) continue;
481
+ if( !ISWORD(&zDoc[i]) ){
482
+ i += search_utf8_len(&zDoc[i]) - 1;
483
+ continue;
484
+ }
300485
for(j=0; j<p->nTerm; j++){
301
- int n = p->a[j].n;
302
- if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
303
- && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
304
- ){
486
+ int n = search_term_match(p->a[j].z, p->a[j].n, &zDoc[i]);
487
+ if( n>=0 ){
305488
snippet_text_append(p, &p->snip, zDoc, i);
306489
zDoc += i;
307490
iTail -= i;
308491
blob_append(&p->snip, p->zMarkBegin, -1);
309
- if( p->a[j].z[n]=='*' ){
310
- while( ISALNUM(zDoc[n]) ) n++;
492
+ if( p->a[j].z[p->a[j].n]=='*' ){
493
+ while( ISALNUM(zDoc[n])
494
+ && search_char_class(&zDoc[n])==SCHAR_WORD ){
495
+ n++;
496
+ }
311497
}
312498
snippet_text_append(p, &p->snip, zDoc, n);
313499
zDoc += n;
314500
iTail -= n;
315501
blob_append(&p->snip, p->zMarkEnd, -1);
@@ -316,11 +502,17 @@
316502
i = -1;
317503
break;
318504
} /* end-if */
319505
} /* end for(j) */
320506
if( j<p->nTerm ){
321
- while( ISALNUM(zDoc[i]) && i<iTail ){ i++; }
507
+ while( i<iTail && ISALNUM(zDoc[i])
508
+ && (i<0 || search_char_class(&zDoc[i])==SCHAR_WORD) ){
509
+ i++;
510
+ }
511
+ if( i>=0 && i<iTail && ISALNUM(zDoc[i]) ) i--;
512
+ }else if( search_char_class(&zDoc[i])==SCHAR_UNSPACED ){
513
+ i += search_utf8_len(&zDoc[i]) - 1;
322514
}
323515
} /* end for(i) */
324516
snippet_text_append(p, &p->snip, zDoc, iTail);
325517
}
326518
if( wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
@@ -2273,10 +2465,17 @@
22732465
**
22742466
** tokenizer VALUE Select a tokenizer for indexed search. VALUE
22752467
** may be one of (porter, on, off, trigram, unicode61),
22762468
** and "on" is equivalent to "porter". Unindexed
22772469
** search never uses tokenization or stemming.
2470
+** The porter and unicode61 tokenizers treat an
2471
+** unbroken run of Chinese, Japanese, Thai, Lao,
2472
+** Khmer or Burmese text as a single word. The
2473
+** trigram tokenizer finds words in such text but
2474
+** does not match a query shorter than three
2475
+** characters. Unindexed search matches at any
2476
+** character of those scripts.
22782477
**
22792478
** The current search settings are displayed after any changes are applied.
22802479
** Run this command with no arguments to simply see the settings.
22812480
*/
22822481
void fts_config_cmd(void){
22832482
--- src/search.c
+++ src/search.c
@@ -93,10 +93,174 @@
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 };
96 #define ISALNUM(x) (!isBoundary[(x)&0xff])
97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
99 /*
100 ** Destroy a full-scan search context.
101 */
102 void search_end(Search *p){
@@ -137,14 +301,14 @@
137 p->zMarkEnd = mprintf("%s",zMarkEnd);
138 p->zMarkGap = mprintf("%s",zMarkGap);
139 p->fSrchFlg = fSrchFlg;
140 blob_init(&p->snip, 0, 0);
141 while( *z && p->nTerm<SEARCH_MAX_TERM ){
142 while( *z && !ISALNUM(*z) ){ z++; }
143 if( *z==0 ) break;
144 p->a[p->nTerm].z = z;
145 for(i=1; ISALNUM(z[i]); i++){}
146 p->a[p->nTerm].n = i;
147 z += i;
148 p->nTerm++;
149 }
150 return p;
@@ -211,17 +375,19 @@
211 for(iDoc=0; iDoc<nDoc; iDoc++){
212 zDoc = azDoc[iDoc];
213 if( zDoc==0 ) continue;
214 iWord++;
215 for(i=0; zDoc[i]; i++){
216 if( !ISALNUM(zDoc[i]) ) continue;
 
 
 
 
 
217 iWord++;
218 for(j=0; j<p->nTerm; j++){
219 int n = p->a[j].n;
220 if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
221 && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
222 ){
223 aiWordIdx[j] = iWord;
224 aiLastDoc[j] = iDoc;
225 aiLastOfst[j] = i;
226 for(k=1; j-k>=0 && anMatch[j-k] && aiWordIdx[j-k]==iWord-k; k++){}
227 for(ii=0; ii<k; ii++){
@@ -232,12 +398,20 @@
232 }
233 }
234 break;
235 }
236 }
237 while( ISALNUM(zDoc[i]) ){ i++; }
 
 
 
 
 
 
 
238 if( zDoc[i]==0 ) break;
 
239 }
240 }
241
242 /* Finished search all documents.
243 ** Every term must be seen or else the score is zero
@@ -283,33 +457,45 @@
283 }
284 }
285 zDoc = azDoc[iDoc];
286 iOfst -= CTX;
287 if( iOfst<0 ) iOfst = 0;
288 while( iOfst>0 && ISALNUM(zDoc[iOfst-1]) ) iOfst--;
289 while( zDoc[iOfst] && !ISALNUM(zDoc[iOfst]) ) iOfst++;
 
 
 
 
 
290 for(ii=0; ii<CTX && zDoc[iTail]; ii++, iTail++){}
291 while( ISALNUM(zDoc[iTail]) ) iTail++;
 
 
 
292 if( iOfst>0 || wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
293 wantGap = zDoc[iTail]!=0;
294 zDoc += iOfst;
295 iTail -= iOfst;
296
297 /* Add a snippet segment using characters iOfst..iOfst+iTail from zDoc */
298 for(i=0; i<iTail; i++){
299 if( !ISALNUM(zDoc[i]) ) continue;
 
 
 
300 for(j=0; j<p->nTerm; j++){
301 int n = p->a[j].n;
302 if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
303 && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
304 ){
305 snippet_text_append(p, &p->snip, zDoc, i);
306 zDoc += i;
307 iTail -= i;
308 blob_append(&p->snip, p->zMarkBegin, -1);
309 if( p->a[j].z[n]=='*' ){
310 while( ISALNUM(zDoc[n]) ) n++;
 
 
 
311 }
312 snippet_text_append(p, &p->snip, zDoc, n);
313 zDoc += n;
314 iTail -= n;
315 blob_append(&p->snip, p->zMarkEnd, -1);
@@ -316,11 +502,17 @@
316 i = -1;
317 break;
318 } /* end-if */
319 } /* end for(j) */
320 if( j<p->nTerm ){
321 while( ISALNUM(zDoc[i]) && i<iTail ){ i++; }
 
 
 
 
 
 
322 }
323 } /* end for(i) */
324 snippet_text_append(p, &p->snip, zDoc, iTail);
325 }
326 if( wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
@@ -2273,10 +2465,17 @@
2273 **
2274 ** tokenizer VALUE Select a tokenizer for indexed search. VALUE
2275 ** may be one of (porter, on, off, trigram, unicode61),
2276 ** and "on" is equivalent to "porter". Unindexed
2277 ** search never uses tokenization or stemming.
 
 
 
 
 
 
 
2278 **
2279 ** The current search settings are displayed after any changes are applied.
2280 ** Run this command with no arguments to simply see the settings.
2281 */
2282 void fts_config_cmd(void){
2283
--- src/search.c
+++ src/search.c
@@ -93,10 +93,174 @@
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 };
96 #define ISALNUM(x) (!isBoundary[(x)&0xff])
97
98 /*
99 ** Classes of non-ASCII characters returned by search_char_class()
100 */
101 #define SCHAR_WORD 0 /* Part of a word, or not a character start */
102 #define SCHAR_UNSPACED 1 /* Letter of a script written without spaces */
103 #define SCHAR_SEPARATOR 2 /* Punctuation or space that separates words */
104
105 /*
106 ** Return the number of bytes in the UTF-8 character that starts at z.
107 */
108 static int search_utf8_len(const char *z){
109 int n = 1;
110 if( (z[0]&0xc0)==0xc0 ){
111 while( n<4 && (z[n]&0xc0)==0x80 ) n++;
112 }
113 return n;
114 }
115
116 /*
117 ** Classify the character that starts at z.
118 **
119 ** Chinese, Japanese, Thai, Lao, Khmer and Burmese text has no spaces
120 ** between words, and finding word boundaries in it needs a dictionary
121 ** (Unicode UAX #29). The full-scan search instead lets a match begin
122 ** and end at any character of those scripts. The spaces and
123 ** punctuation listed below separate words: Latin-1 punctuation, the
124 ** General Punctuation, CJK and fullwidth punctuation blocks, the
125 ** Vertical Forms and CJK Compatibility Forms blocks (punctuation for
126 ** vertical text), and Arabic and Devanagari punctuation. Other
127 ** characters outside ASCII, including punctuation of other scripts such
128 ** as Armenian and Ethiopic, are treated as part of a word.
129 */
130 static int search_char_class(const char *z){
131 const unsigned char *u = (const unsigned char*)z;
132 u32 c;
133 if( u[0]<0xc0 ) return SCHAR_WORD;
134 c = fossil_utf8_read(&u);
135 if( c>=0xa0 && c<=0xbf ){
136 /* Latin-1 punctuation and symbols, but not ª ² ³ µ ¹ º ¼ ½ ¾ */
137 if( c==0xaa || c==0xb2 || c==0xb3 || c==0xb5 || c==0xb9
138 || c==0xba || (c>=0xbc && c<=0xbe) ){
139 return SCHAR_WORD;
140 }
141 return SCHAR_SEPARATOR;
142 }
143 if( c==0xd7 || c==0xf7 ) return SCHAR_SEPARATOR; /* × ÷ */
144 if( c==0x60c || c==0x61b || c==0x61f || c==0x6d4 ){
145 return SCHAR_SEPARATOR; /* Arabic */
146 }
147 if( c==0x964 || c==0x965 ) return SCHAR_SEPARATOR; /* Danda */
148 if( c>=0x2000 && c<=0x206f ) return SCHAR_SEPARATOR;
149 if( c>=0x3000 && c<=0x303f ) return SCHAR_SEPARATOR;
150 if( (c>=0xfe10 && c<=0xfe19) || (c>=0xfe30 && c<=0xfe4f) ){
151 return SCHAR_SEPARATOR; /* Vertical forms */
152 }
153 if( (c>=0xff00 && c<=0xff0f) || (c>=0xff1a && c<=0xff20)
154 || (c>=0xff3b && c<=0xff40) || (c>=0xff5b && c<=0xff65) ){
155 return SCHAR_SEPARATOR; /* Fullwidth */
156 }
157 if( (c>=0x0e00 && c<=0x0eff) /* Thai, Lao */
158 || (c>=0x1000 && c<=0x109f) /* Myanmar */
159 || (c>=0x1780 && c<=0x17ff) /* Khmer */
160 || (c>=0x3040 && c<=0x30ff) /* Hiragana, Katakana */
161 || (c>=0x3400 && c<=0x4dbf) /* CJK Extension A */
162 || (c>=0x4e00 && c<=0x9fff) /* CJK Unified Ideographs */
163 || (c>=0xf900 && c<=0xfaff) /* CJK Compatibility Ideographs */
164 || (c>=0x20000 && c<=0x3ffff) /* CJK Extensions B and later */
165 ){
166 return SCHAR_UNSPACED;
167 }
168 return SCHAR_WORD;
169 }
170
171 /*
172 ** True if the byte at z can begin or continue a word
173 */
174 #define ISWORD(z) (ISALNUM(*(z)) && search_char_class(z)!=SCHAR_SEPARATOR)
175
176 /*
177 ** True if a match that ends just before z ends on a word boundary
178 */
179 #define ISWORDEND(z) (!ISWORD(z) || search_char_class(z)==SCHAR_UNSPACED)
180
181 /*
182 ** Compare the nTerm-byte search term zTerm against the text at zDoc,
183 ** ignoring case. Return the number of bytes of zDoc that match, which
184 ** can differ from nTerm when case folding changes the length of a
185 ** character, or -1 if the text does not match. A match must end on a
186 ** word boundary, unless its last character belongs to a script written
187 ** without spaces or the term ends with "*".
188 */
189 static int search_term_match(const char *zTerm, int nTerm, const char *zDoc){
190 const unsigned char *t = (const unsigned char*)zTerm;
191 const unsigned char *tEnd = t + nTerm;
192 const unsigned char *d = (const unsigned char*)zDoc;
193 int bUnspaced = 0;
194 while( t<tEnd ){
195 if( t[0]<0x80 && d[0]<0x80 ){
196 if( fossil_tolower(t[0])!=fossil_tolower(d[0]) ) return -1;
197 t++;
198 d++;
199 bUnspaced = 0;
200 }else{
201 u32 ct, cd;
202 if( d[0]==0 ) return -1;
203 bUnspaced = search_char_class((const char*)t)==SCHAR_UNSPACED;
204 ct = fossil_utf8_read(&t);
205 cd = fossil_utf8_read(&d);
206 if( ct!=cd && unicode_fold((int)ct,0)!=unicode_fold((int)cd,0) ){
207 return -1;
208 }
209 }
210 }
211 if( !bUnspaced && !ISWORDEND((const char*)d) && *tEnd!='*' ){
212 return -1;
213 }
214 return (int)(d - (const unsigned char*)zDoc);
215 }
216
217
218 /*
219 ** Return true if zPat matches the start of zText, ignoring case with
220 ** Unicode case folding.
221 */
222 static int unicode_prefix_nocase(
223 const unsigned char *zText,
224 const unsigned char *zPat
225 ){
226 while( *zPat ){
227 u32 a, b;
228 if( *zText==0 ) return 0;
229 a = fossil_utf8_read(&zText);
230 b = fossil_utf8_read(&zPat);
231 if( a!=b && unicode_fold((int)a,0)!=unicode_fold((int)b,0) ) return 0;
232 }
233 return 1;
234 }
235
236 /*
237 ** Implementation of the SQL function unicode_contains(TEXT,PATTERN),
238 ** which is true if PATTERN occurs in TEXT, ignoring case with Unicode
239 ** case folding. Use it in place of LIKE '%PATTERN%', which folds only
240 ** the ASCII letters A to Z.
241 */
242 void unicode_contains_sqlfunc(
243 sqlite3_context *context,
244 int argc,
245 sqlite3_value **argv
246 ){
247 const unsigned char *zText = sqlite3_value_text(argv[0]);
248 const unsigned char *zPat = sqlite3_value_text(argv[1]);
249 int bFound = 0;
250 if( zText && zPat ){
251 while( 1 ){
252 if( unicode_prefix_nocase(zText, zPat) ){
253 bFound = 1;
254 break;
255 }
256 if( *zText==0 ) break;
257 zText += search_utf8_len((const char*)zText);
258 }
259 }
260 sqlite3_result_int(context, bFound);
261 }
262
263 /*
264 ** Destroy a full-scan search context.
265 */
266 void search_end(Search *p){
@@ -137,14 +301,14 @@
301 p->zMarkEnd = mprintf("%s",zMarkEnd);
302 p->zMarkGap = mprintf("%s",zMarkGap);
303 p->fSrchFlg = fSrchFlg;
304 blob_init(&p->snip, 0, 0);
305 while( *z && p->nTerm<SEARCH_MAX_TERM ){
306 while( *z && !ISWORD(z) ){ z += search_utf8_len(z); }
307 if( *z==0 ) break;
308 p->a[p->nTerm].z = z;
309 for(i=1; ISWORD(&z[i]); i++){}
310 p->a[p->nTerm].n = i;
311 z += i;
312 p->nTerm++;
313 }
314 return p;
@@ -211,17 +375,19 @@
375 for(iDoc=0; iDoc<nDoc; iDoc++){
376 zDoc = azDoc[iDoc];
377 if( zDoc==0 ) continue;
378 iWord++;
379 for(i=0; zDoc[i]; i++){
380 int bUnspaced;
381 if( !ISWORD(&zDoc[i]) ){
382 i += search_utf8_len(&zDoc[i]) - 1;
383 continue;
384 }
385 bUnspaced = search_char_class(&zDoc[i])==SCHAR_UNSPACED;
386 iWord++;
387 for(j=0; j<p->nTerm; j++){
388 if( search_term_match(p->a[j].z, p->a[j].n, &zDoc[i])>=0 ){
 
 
 
389 aiWordIdx[j] = iWord;
390 aiLastDoc[j] = iDoc;
391 aiLastOfst[j] = i;
392 for(k=1; j-k>=0 && anMatch[j-k] && aiWordIdx[j-k]==iWord-k; k++){}
393 for(ii=0; ii<k; ii++){
@@ -232,12 +398,20 @@
398 }
399 }
400 break;
401 }
402 }
403 if( bUnspaced ){
404 /* The next character can begin a match */
405 i += search_utf8_len(&zDoc[i]) - 1;
406 continue;
407 }
408 while( ISWORD(&zDoc[i]) && search_char_class(&zDoc[i])==SCHAR_WORD ){
409 i++;
410 }
411 if( zDoc[i]==0 ) break;
412 if( ISALNUM(zDoc[i]) ) i--; /* Revisit a character outside ASCII */
413 }
414 }
415
416 /* Finished search all documents.
417 ** Every term must be seen or else the score is zero
@@ -283,33 +457,45 @@
457 }
458 }
459 zDoc = azDoc[iDoc];
460 iOfst -= CTX;
461 if( iOfst<0 ) iOfst = 0;
462 while( iOfst>0 && ISALNUM(zDoc[iOfst-1])
463 && search_char_class(&zDoc[iOfst])==SCHAR_WORD ){
464 iOfst--;
465 }
466 while( zDoc[iOfst] && !ISWORD(&zDoc[iOfst]) ){
467 iOfst += search_utf8_len(&zDoc[iOfst]);
468 }
469 for(ii=0; ii<CTX && zDoc[iTail]; ii++, iTail++){}
470 while( ISALNUM(zDoc[iTail])
471 && search_char_class(&zDoc[iTail])==SCHAR_WORD ){
472 iTail++;
473 }
474 if( iOfst>0 || wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
475 wantGap = zDoc[iTail]!=0;
476 zDoc += iOfst;
477 iTail -= iOfst;
478
479 /* Add a snippet segment using characters iOfst..iOfst+iTail from zDoc */
480 for(i=0; i<iTail; i++){
481 if( !ISWORD(&zDoc[i]) ){
482 i += search_utf8_len(&zDoc[i]) - 1;
483 continue;
484 }
485 for(j=0; j<p->nTerm; j++){
486 int n = search_term_match(p->a[j].z, p->a[j].n, &zDoc[i]);
487 if( n>=0 ){
 
 
488 snippet_text_append(p, &p->snip, zDoc, i);
489 zDoc += i;
490 iTail -= i;
491 blob_append(&p->snip, p->zMarkBegin, -1);
492 if( p->a[j].z[p->a[j].n]=='*' ){
493 while( ISALNUM(zDoc[n])
494 && search_char_class(&zDoc[n])==SCHAR_WORD ){
495 n++;
496 }
497 }
498 snippet_text_append(p, &p->snip, zDoc, n);
499 zDoc += n;
500 iTail -= n;
501 blob_append(&p->snip, p->zMarkEnd, -1);
@@ -316,11 +502,17 @@
502 i = -1;
503 break;
504 } /* end-if */
505 } /* end for(j) */
506 if( j<p->nTerm ){
507 while( i<iTail && ISALNUM(zDoc[i])
508 && (i<0 || search_char_class(&zDoc[i])==SCHAR_WORD) ){
509 i++;
510 }
511 if( i>=0 && i<iTail && ISALNUM(zDoc[i]) ) i--;
512 }else if( search_char_class(&zDoc[i])==SCHAR_UNSPACED ){
513 i += search_utf8_len(&zDoc[i]) - 1;
514 }
515 } /* end for(i) */
516 snippet_text_append(p, &p->snip, zDoc, iTail);
517 }
518 if( wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
@@ -2273,10 +2465,17 @@
2465 **
2466 ** tokenizer VALUE Select a tokenizer for indexed search. VALUE
2467 ** may be one of (porter, on, off, trigram, unicode61),
2468 ** and "on" is equivalent to "porter". Unindexed
2469 ** search never uses tokenization or stemming.
2470 ** The porter and unicode61 tokenizers treat an
2471 ** unbroken run of Chinese, Japanese, Thai, Lao,
2472 ** Khmer or Burmese text as a single word. The
2473 ** trigram tokenizer finds words in such text but
2474 ** does not match a query shorter than three
2475 ** characters. Unindexed search matches at any
2476 ** character of those scripts.
2477 **
2478 ** The current search settings are displayed after any changes are applied.
2479 ** Run this command with no arguments to simply see the settings.
2480 */
2481 void fts_config_cmd(void){
2482
--- src/setup.c
+++ src/setup.c
@@ -2305,10 +2305,15 @@
23052305
"unicode61", "Unicode without stemming",
23062306
"trigram", "Trigram",
23072307
};
23082308
multiple_choice_attribute("FTS Tokenizer", "search-tokenizer",
23092309
"ftstok", "off", 4, aTokenizer);
2310
+ @ <p>The Porter and Unicode tokenizers treat an unbroken run of Chinese,
2311
+ @ Japanese, Thai, Lao, Khmer or Burmese text as a single word. The
2312
+ @ Trigram tokenizer finds words in such text, but does not match a
2313
+ @ query shorter than three characters. Search without an index
2314
+ @ matches at any character of those scripts.</p>
23102315
}
23112316
23122317
/*
23132318
** WEBPAGE: srchsetup
23142319
**
23152320
--- src/setup.c
+++ src/setup.c
@@ -2305,10 +2305,15 @@
2305 "unicode61", "Unicode without stemming",
2306 "trigram", "Trigram",
2307 };
2308 multiple_choice_attribute("FTS Tokenizer", "search-tokenizer",
2309 "ftstok", "off", 4, aTokenizer);
 
 
 
 
 
2310 }
2311
2312 /*
2313 ** WEBPAGE: srchsetup
2314 **
2315
--- src/setup.c
+++ src/setup.c
@@ -2305,10 +2305,15 @@
2305 "unicode61", "Unicode without stemming",
2306 "trigram", "Trigram",
2307 };
2308 multiple_choice_attribute("FTS Tokenizer", "search-tokenizer",
2309 "ftstok", "off", 4, aTokenizer);
2310 @ <p>The Porter and Unicode tokenizers treat an unbroken run of Chinese,
2311 @ Japanese, Thai, Lao, Khmer or Burmese text as a single word. The
2312 @ Trigram tokenizer finds words in such text, but does not match a
2313 @ query shorter than three characters. Search without an index
2314 @ matches at any character of those scripts.</p>
2315 }
2316
2317 /*
2318 ** WEBPAGE: srchsetup
2319 **
2320
+5 -4
--- src/timeline.c
+++ src/timeline.c
@@ -3202,18 +3202,19 @@
32023202
if( zSearch ){
32033203
if( tmFlags & TIMELINE_FORUMTXT ){
32043204
sqlite3_create_function(g.db, "forum_post_content", 1, SQLITE_UTF8,
32053205
0, forum_post_content_function, 0, 0);
32063206
blob_append_sql(&cond,
3207
- " AND (event.comment LIKE '%%%q%%'"
3208
- " OR event.brief LIKE '%%%q%%'"
3207
+ " AND (unicode_contains(event.comment,%Q)"
3208
+ " OR unicode_contains(event.brief,%Q)"
32093209
" OR (event.type=='f' AND"
3210
- " forum_post_content(event.objid) LIKE '%%%q%%'))",
3210
+ " unicode_contains(forum_post_content(event.objid),%Q)))",
32113211
zSearch, zSearch, zSearch);
32123212
}else{
32133213
blob_append_sql(&cond,
3214
- " AND (event.comment LIKE '%%%q%%' OR event.brief LIKE '%%%q%%')",
3214
+ " AND (unicode_contains(event.comment,%Q)"
3215
+ " OR unicode_contains(event.brief,%Q))",
32153216
zSearch, zSearch);
32163217
}
32173218
}
32183219
rBefore = symbolic_name_to_mtime(zBefore, &zBefore, 1);
32193220
rAfter = symbolic_name_to_mtime(zAfter, &zAfter, 0);
32203221
--- src/timeline.c
+++ src/timeline.c
@@ -3202,18 +3202,19 @@
3202 if( zSearch ){
3203 if( tmFlags & TIMELINE_FORUMTXT ){
3204 sqlite3_create_function(g.db, "forum_post_content", 1, SQLITE_UTF8,
3205 0, forum_post_content_function, 0, 0);
3206 blob_append_sql(&cond,
3207 " AND (event.comment LIKE '%%%q%%'"
3208 " OR event.brief LIKE '%%%q%%'"
3209 " OR (event.type=='f' AND"
3210 " forum_post_content(event.objid) LIKE '%%%q%%'))",
3211 zSearch, zSearch, zSearch);
3212 }else{
3213 blob_append_sql(&cond,
3214 " AND (event.comment LIKE '%%%q%%' OR event.brief LIKE '%%%q%%')",
 
3215 zSearch, zSearch);
3216 }
3217 }
3218 rBefore = symbolic_name_to_mtime(zBefore, &zBefore, 1);
3219 rAfter = symbolic_name_to_mtime(zAfter, &zAfter, 0);
3220
--- src/timeline.c
+++ src/timeline.c
@@ -3202,18 +3202,19 @@
3202 if( zSearch ){
3203 if( tmFlags & TIMELINE_FORUMTXT ){
3204 sqlite3_create_function(g.db, "forum_post_content", 1, SQLITE_UTF8,
3205 0, forum_post_content_function, 0, 0);
3206 blob_append_sql(&cond,
3207 " AND (unicode_contains(event.comment,%Q)"
3208 " OR unicode_contains(event.brief,%Q)"
3209 " OR (event.type=='f' AND"
3210 " unicode_contains(forum_post_content(event.objid),%Q)))",
3211 zSearch, zSearch, zSearch);
3212 }else{
3213 blob_append_sql(&cond,
3214 " AND (unicode_contains(event.comment,%Q)"
3215 " OR unicode_contains(event.brief,%Q))",
3216 zSearch, zSearch);
3217 }
3218 }
3219 rBefore = symbolic_name_to_mtime(zBefore, &zBefore, 1);
3220 rAfter = symbolic_name_to_mtime(zAfter, &zAfter, 0);
3221

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button