Fossil SCM

fossil-scm / src / casefold.c
Blame History Raw 219 lines
1
/*
2
** Copyright (c) 2026 Warren Young
3
**
4
** This program is free software; you can redistribute it and/or
5
** modify it under the terms of the Simplified BSD License (also
6
** known as the "2-Clause License" or "FreeBSD License".)
7
**
8
** This program is distributed in the hope that it will be useful,
9
** but without any warranty; without even the implied warranty of
10
** merchantability or fitness for a particular purpose.
11
**
12
** Author contact information:
13
** https://fossil-scm.org/forum/ (user wyoung)
14
**
15
*******************************************************************************
16
**
17
** Canonical caseless filename comparison for case-sensitive off.
18
** Tables in casefold.h are generated by tools/casefold.tcl from the
19
** Unicode character database.
20
*/
21
#include "config.h"
22
#include "casefold.h"
23
#include <string.h>
24
25
/*
26
** Tables for filename_nocase_cmp(). Canonical decompositions and case
27
** folds come from the Unicode character database. Fullwidth Latin
28
** letters fold to fullwidth letters, not to ASCII.
29
*/
30
typedef struct FnMap FnMap;
31
struct FnMap {
32
unsigned int cp;
33
unsigned short off;
34
unsigned char n;
35
};
36
typedef struct FnCcc FnCcc;
37
struct FnCcc {
38
unsigned int cp;
39
unsigned char ccc;
40
};
41
#include "../src/casefold.h"
42
43
static int fn_find_map(const FnMap *a, int n, unsigned int cp){
44
int lo = 0, hi = n-1;
45
while( lo<=hi ){
46
int mid = (lo+hi)/2;
47
if( a[mid].cp<cp ) lo = mid+1;
48
else if( a[mid].cp>cp ) hi = mid-1;
49
else return mid;
50
}
51
return -1;
52
}
53
54
static int fn_find_ccc(unsigned int cp){
55
int lo = 0, hi = count(aFnCcc)-1;
56
while( lo<=hi ){
57
int mid = (lo+hi)/2;
58
if( aFnCcc[mid].cp<cp ) lo = mid+1;
59
else if( aFnCcc[mid].cp>cp ) hi = mid-1;
60
else return aFnCcc[mid].ccc;
61
}
62
return 0;
63
}
64
65
static int fn_hangul(unsigned int cp, unsigned int *out){
66
unsigned int si, t;
67
if( cp<0xAC00 || cp>0xD7A3 ) return 0;
68
si = cp - 0xAC00;
69
out[0] = 0x1100 + si/588;
70
out[1] = 0x1161 + (si%588)/28;
71
t = 0x11A7 + si%28;
72
if( t==0x11A7 ) return 2;
73
out[2] = t;
74
return 3;
75
}
76
77
static int *fn_push(int *a, int *pn, int *pnAlloc, int cp){
78
if( *pn>=*pnAlloc ){
79
*pnAlloc = *pnAlloc ? (*pnAlloc)*2 : 32;
80
a = fossil_realloc(a, (size_t)(*pnAlloc)*sizeof(int));
81
}
82
a[(*pn)++] = cp;
83
return a;
84
}
85
86
static int *fn_emit_decomp(int *a, int *pn, int *pnAlloc, unsigned int cp){
87
unsigned int h[3];
88
int i, k, n;
89
n = fn_hangul(cp, h);
90
if( n ){
91
for(k=0; k<n; k++) a = fn_push(a, pn, pnAlloc, (int)h[k]);
92
return a;
93
}
94
i = fn_find_map(aFnNfdMap, count(aFnNfdMap), cp);
95
if( i>=0 ){
96
for(k=0; k<aFnNfdMap[i].n; k++){
97
a = fn_push(a, pn, pnAlloc, (int)aFnNfdSeq[aFnNfdMap[i].off+k]);
98
}
99
return a;
100
}
101
return fn_push(a, pn, pnAlloc, (int)cp);
102
}
103
104
static void fn_reorder(int *a, int n){
105
int done;
106
do{
107
int i;
108
done = 1;
109
for(i=1; i<n; i++){
110
int cb = fn_find_ccc((unsigned int)a[i]);
111
int ca = fn_find_ccc((unsigned int)a[i-1]);
112
if( cb && ca>cb ){
113
int t = a[i];
114
a[i] = a[i-1];
115
a[i-1] = t;
116
done = 0;
117
}
118
}
119
}while( !done );
120
}
121
122
static int fn_utf8_next(const unsigned char *z, int n, int *pi){
123
int i0, i;
124
unsigned int c, u;
125
i0 = *pi;
126
i = i0;
127
if( i>=n ) return -1;
128
c = z[i++];
129
if( c<0x80 ){
130
*pi = i;
131
return (int)c;
132
}
133
if( (c&0xE0)==0xC0 && i<n && (z[i]&0xC0)==0x80 ){
134
u = ((c&0x1F)<<6) | (z[i]&0x3F);
135
if( u>=0x80 ){
136
*pi = i+1;
137
return (int)u;
138
}
139
}else if( (c&0xF0)==0xE0 && i+1<n && (z[i]&0xC0)==0x80
140
&& (z[i+1]&0xC0)==0x80 ){
141
u = ((c&0x0F)<<12) | ((z[i]&0x3F)<<6) | (z[i+1]&0x3F);
142
if( u>=0x800 && (u<0xD800 || u>0xDFFF) ){
143
*pi = i+2;
144
return (int)u;
145
}
146
}else if( (c&0xF8)==0xF0 && i+2<n && (z[i]&0xC0)==0x80
147
&& (z[i+1]&0xC0)==0x80 && (z[i+2]&0xC0)==0x80 ){
148
u = ((c&0x07)<<18) | ((z[i]&0x3F)<<12) | ((z[i+1]&0x3F)<<6)
149
| (z[i+2]&0x3F);
150
if( u>=0x10000 && u<=0x10FFFF ){
151
*pi = i+3;
152
return (int)u;
153
}
154
}
155
*pi = i0+1;
156
return (int)z[i0];
157
}
158
159
static int *fn_key(const char *z, int n, int *pn){
160
const unsigned char *uz = (const unsigned char *)(z ? z : "");
161
int *dec = 0, *out = 0;
162
int nDec = 0, nDecAlloc = 0, nOut = 0, nOutAlloc = 0;
163
int i, k;
164
if( n<0 ) n = (int)strlen((const char *)uz);
165
i = 0;
166
while( i<n ){
167
int cp = fn_utf8_next(uz, n, &i);
168
if( cp<0 ) break;
169
dec = fn_emit_decomp(dec, &nDec, &nDecAlloc, (unsigned int)cp);
170
}
171
fn_reorder(dec, nDec);
172
for(k=0; k<nDec; k++){
173
unsigned int cp = (unsigned int)dec[k];
174
int m = fn_find_map(aFnFoldMap, count(aFnFoldMap), cp);
175
int j;
176
if( m<0 ){
177
out = fn_push(out, &nOut, &nOutAlloc, (int)cp);
178
}else{
179
for(j=0; j<aFnFoldMap[m].n; j++){
180
out = fn_push(out, &nOut, &nOutAlloc,
181
(int)aFnFoldSeq[aFnFoldMap[m].off+j]);
182
}
183
}
184
}
185
fn_reorder(out, nOut);
186
fossil_free(dec);
187
*pn = nOut;
188
return out;
189
}
190
191
static int fn_cmp_cps(const int *a, int na, const int *b, int nb){
192
int i, n;
193
n = na<nb ? na : nb;
194
for(i=0; i<n; i++){
195
if( a[i]!=b[i] ) return a[i]<b[i] ? -1 : 1;
196
}
197
if( na==nb ) return 0;
198
return na<nb ? -1 : 1;
199
}
200
201
/*
202
** Compare two filenames for case-sensitive off. Zero means the same
203
** file: equal after canonical decomposition and case folding. nA or
204
** nB below zero selects a NUL-terminated string. Fullwidth Latin
205
** letters fold inside the fullwidth block, so they stay distinct from
206
** ASCII.
207
*/
208
int filename_nocase_cmp(const char *zA, int nA, const char *zB, int nB){
209
int *a, *b, na = 0, nb = 0, rc;
210
if( zA==0 ){ zA = ""; nA = 0; }
211
if( zB==0 ){ zB = ""; nB = 0; }
212
a = fn_key(zA, nA, &na);
213
b = fn_key(zB, nB, &nb);
214
rc = fn_cmp_cps(a, na, b, nb);
215
fossil_free(a);
216
fossil_free(b);
217
return rc;
218
}
219

Keyboard Shortcuts

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