-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_agg.c
More file actions
453 lines (436 loc) · 16.3 KB
/
Copy pathreplace_agg.c
File metadata and controls
453 lines (436 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
** replace_agg.c - 2013 - jakethaw
**
*************************************************************************
**
** MIT License
**
** Copyright (c) 2020 jakethaw
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in all
** copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/
/*
*************************************************************************
** SQLite3 *************************************************************
*************************************************************************
**
** replace_agg(A, B, C)
**
** The aggregate replace_agg(A, B, C) function. Three arguments are all strings:
** call them A (constant), B, and C. The result is also a string which is derived
** from A by replacing every occurrence of each B with its corresponding C.
** The match must be exact. Collating sequences are not used.
** replace_agg() output is identical to that of the scalar replace() function if
** aggregating over 1 row.
** To compile replace_agg with gcc as a run-time loadable extension:
**
** UNIX-like : gcc -g -O3 -fPIC -shared replace_agg.c -o replace_agg.so
** Mac : gcc -g -O3 -fPIC -dynamiclib replace_agg.c -o replace_agg.dylib
** Windows : gcc -g -O3 -shared replace_agg.c -o replace_agg.dll
**
*************************************************************************
*/
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <string.h>
typedef struct replace_Replacement *replace_Replacement;
struct replace_Replacement {
char *z; /* The replacement string C */
int n1; /* Length of C */
int n2; /* Length of pattern string B */
int i; /* Counter of usage */
};
typedef struct replace_PatternLocation *replace_PatternLocation;
struct replace_PatternLocation {
int i; /* Location of B in A */
struct replace_Replacement *r; /* Link to B's replacement string C */
struct replace_PatternLocation *p; /* Link to next PatternLocation */
};
typedef struct replaceCtx replaceCtx;
struct replaceCtx {
char *z; /* The input string A */
int n; /* Length of A */
struct replace_PatternLocation *p; /* Link to head PatternLocation */
int firstStep; /* False if A needs to be stored */
};
static void replaceStep(sqlite3_context *context, int argc, sqlite3_value **argv){
const char *z; /* The pattern string B */
int n; /* Length of B */
const char *z2; /* Temporary pointer to an input string */
int i, j; /* Loop counters */
int bFirstMatch = 1; /* True if pattern string needs to be stored */
replaceCtx *ctx; /* SQLite aggregate context */
replace_Replacement r, r2; /* Active replacement, and neighbouring replacement */
replace_PatternLocation p, p2, p3, p4; /* Active PatternLocation, and neighbouring PatternLocations */
ctx = (replaceCtx*)sqlite3_aggregate_context(context, sizeof(*ctx));
if( ctx ){
if( !ctx->firstStep ){
/* Store the input string A */
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
z2 = (char *)sqlite3_value_text(argv[0]);
ctx->n = sqlite3_value_bytes(argv[0]);
if( ctx->n ){
ctx->z = sqlite3_malloc(ctx->n);
memcpy(ctx->z, z2, ctx->n);
}
ctx->p = 0;
ctx->firstStep = 1;
}
/* if A is empty, then don't bother continuing */
if( !ctx->n ) return;
/* get pattern string B */
if( sqlite3_value_type(argv[1])==SQLITE_NULL ) return;
if( sqlite3_value_type(argv[2])==SQLITE_NULL ) return;
z = (char *)sqlite3_value_text(argv[1]);
n = sqlite3_value_bytes(argv[1]);
if( !n ) return;
for(i=0; i<=ctx->n-n; i++){
/* check first character match */
if( ctx->z[i]==z[0] ){
j=1;
/* check rest of characters */
if( n>1 ){
while (ctx->z[i+j]==z[j]){
j++;
if( j==n ) break;
}
}
/* if match is found */
if( j==n ){
if( bFirstMatch ){
/* Store the replacement string if the first occurrence
** of the pattern string is found
*/
bFirstMatch=0;
r = (replace_Replacement) sqlite3_malloc(sizeof(struct replace_Replacement));
r->n2 = n;
z2 = (char *)sqlite3_value_text(argv[2]);
r->n1 = sqlite3_value_bytes(argv[2]);
if( r->n1 ){
r->z = sqlite3_malloc(r->n1);
memcpy(r->z, z2, r->n1);
}
r->i = 0;
}
/* Store the location of the pattern string in the input string */
p = (replace_PatternLocation) sqlite3_malloc(sizeof(struct replace_PatternLocation));
p->i = i;
p->r = r;
r->i++;
/* Insert pattern location into linked list */
if( ctx->p == 0 ){
/* if this is the first pattern location, just make
** it the head node
*/
p->p = 0;
ctx->p = p;
}else{
/* else go through linked list, starting at head node.
** node order: p4->p3->p2.
**
** Potential outcomes for p (the new pattern location):
**
** Case 1: p lies before p2 where p2 is the head node, and
** no overlapping occurs.
** => p becomes the head node.
**
** Case 2: p lies between p3 and p2 and no overlapping occurs.
** => p will be inserted between p3 and p2.
**
** Case 3: p overlaps either p3 or p2 and has a shorter
** pattern string length.
** => p will be ignored.
**
** Case 4: p3 overlaps p on the left, and p3 has a shorter
** pattern string length.
** => p will take the position of p3.
**
** Case 5: p2 overlaps p on the right, and p2 has a
** shorter pattern string length.
** => p will take the position of p2.
**
** Case 6: p3 overlaps p on the left, and p2 overlaps
** p on the right, and both p3 and p2 have
** shorter pattern string length.
** => p will take position of p2 and p3.
**
** Case 7: p lies after the current tail node,
** and does not overlap.
** => p becomes the tail node.
**
** Case 8: the tail node overlaps p on the left and
** the tail node has a shorter pattern string length.
** => p will take the position of the tail node.
**
** Case 9: the tail node overlaps p on the left and
** p has a shorter pattern string length.
** => p will be ignored.
*/
p2 = ctx->p;
p3 = p4 = 0;
while (1){
/* if arrived at insertion point */
if( p->i < p2->i ) {
/* check if any overlapping will occur at this point */
if( p3 ){
/* if p3 currently points to a node,
** then check for left overlap
*/
r2 = p3->r;
if( p3->i+r2->n2 > p->i ){
/* overlap does occur, so remove shorter pattern */
if( r2->n2 < r->n2 ){
/* p3 is shorter
** if p4 currently points to a node,
** then link it to p2, otherwise p2 will
** be the head node (p is yet to be inserted)
*/
if( p4 ){
p4->p = p2;
}else{
ctx->p = p2;
}
/* remove replacement string if all matching patterns
** have been overlapped
*/
r2->i--;
if( !r2->i ){
if( r2->n1 ) sqlite3_free(r2->z);
sqlite3_free(r2);
}
/* remove current p3 and make p3 = p4 */
sqlite3_free(p3);
p3 = p4;
}else{
/* p is shorter, no changes to list */
r->i--;
sqlite3_free(p);
p = 0;
}
}
}
if( p ){
/* if p still exists, then check for right overlap */
if( p->i+r->n2 > p2->i ){
/* overlap does occur, so remove shorter pattern */
r2 = p2->r;
if( r2->n2 < r->n2 ){
/* p2 is shorter */
if( p3 ){
p3->p = p2->p;
}else{
ctx->p = p2->p;
}
/* remove replacement string if all matching patterns
** have been overlapped
*/
r2->i--;
if( !r2->i ){
if( r2->n1 ) sqlite3_free(r2->z);
sqlite3_free(r2);
}
/* remove current p2 and make p2 it's link */
p4 = p2;
p2 = p2->p;
sqlite3_free(p4);
}else{
/* p is shorter, no changes to list */
r->i--;
sqlite3_free(p);
p = 0;
}
}
}
if( p ){
/* if p still exists at this point
** insert new pattern location between p3 and p2
*/
p->p = p2;
if( !p3 ){ /* (!p3 => p2 is the head node) */
/* if p2 was the head node, just make
** p the new head node
*/
ctx->p = p;
}else{
p3->p = p;
}
}
break;
}else{
/* if final node */
if ( !p2->p ){
/* check for right overlap */
r2 = p2->r;
if( p2->i+r2->n2 > p->i ){
/* overlap does occur, so remove shorter pattern */
if( r2->n2 < r->n2 ){
/* p2 is shorter
** if p3 currently points to a node,
** then link it to p, otherwise p will
** be the head node
*/
p->p = 0;
if( p3 ){
p3->p = p;
}else{
ctx->p = p;
}
/* remove p2 */
r2->i--;
if( !r2->i ){
if( r2->n1 ) sqlite3_free(r2->z);
sqlite3_free(r2);
}
sqlite3_free(p2);
}else{
/* p is shorter, no changes to list */
r->i--;
sqlite3_free(p);
p = 0;
}
}else{
/* else, no overlap */
p2->p = p;
p->p = 0;
}
break;
}
/* otherwise move to next node */
if( p3 ) p4 = p3;
p3 = p2;
p2 = p2->p;
}
}
}
/* add n-1 to continue searching the input
** string from beyond the matched pattern
*/
i += n-1;
}
}
}
/* remove replacement string of current step
** if all matches have been removed due to overlap
** (this is fairly unlikely, but still possible)
*/
if( !bFirstMatch ){
if( !r->i ){
if( r->n1 ) sqlite3_free(r->z);
sqlite3_free(r);
}
}
}
}
static void replaceFinalize(sqlite3_context *context){
char *z;
int n = 0;
int i, j;
replaceCtx *ctx;
replace_PatternLocation p, p2;
replace_Replacement r;
ctx = sqlite3_aggregate_context(context, 0);
if( ctx ){
/* if the input string is empty,
** then return an empty string
*/
if( !ctx->n ){
sqlite3_result_text(context, "", 0, SQLITE_STATIC);
return;
}
/* if there are no values to replace, then
** just return the input string unchanged
*/
if( !ctx->p ){
sqlite3_result_text(context, ctx->z, ctx->n, sqlite3_free);
return;
}
/* calculate memory required */
p = ctx->p;
while (1){
r = p->r;
n += r->n1 - r->n2;
if( !p->p ) break;
p = p->p;
}
n += ctx->n;
/* if the output string has length 0,
** then return an empty string
*/
if( !n ){
sqlite3_result_text(context, "", 0, SQLITE_STATIC);
return;
}
z = sqlite3_malloc(n);
/* construct new string */
p = ctx->p;
i = p->i;
/* copy first part of input string */
memcpy(&z[0], ctx->z, i);
while (1){
r = p->r;
/* copy replacement string */
if( r->n1 ) memcpy(&z[i], r->z, r->n1);
i += r->n1;
r->i--;
/* quit loop if on last pattern location */
if( !p->p ) {
if( r->n1 ) sqlite3_free(r->z);
sqlite3_free(r);
sqlite3_free(p);
break;
}
/* copy next part of input string */
j = p->i + r->n2;
p2 = p;
p = p->p;
sqlite3_free(p2);
memcpy(&z[i], &(ctx->z[j]), p->i-j);
i += p->i-j;
if( !r->i ){
if( r->n1 ) sqlite3_free(r->z);
sqlite3_free(r);
}
}
/* tail end of input string */
memcpy(&z[i], &(ctx->z[ctx->n-n+i]), n-i);
sqlite3_free(ctx->z);
sqlite3_result_text(context, z, n, sqlite3_free);
}
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_replaceagg_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
){
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
rc = sqlite3_create_function(db,
"replace_agg",
3,
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
0,
0,
replaceStep,
replaceFinalize);
return rc;
}