-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlean.c
14746 lines (13670 loc) · 650 KB
/
sqlean.c
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------
// Following is an amalgamated version of sqlean v0.21.6
// License @ https://github.com/nalgeon/sqlean/blob/main/LICENSE
// Find more details @ https://github.com/nalgeon/sqlean
// All copyrights belong to original author(s)
// ---------------------------------
#include "sqlean.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef SQLEAN_ENABLE_TEXT
// ---------------------------------
// src/text/bstring.c
// ---------------------------------
// Copyright (c) 2023 Anton Zhiyanov, MIT License
// https://github.com/nalgeon/sqlean
// Byte string data structure.
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// bstring_new creates an empty string.
static ByteString bstring_new(void) {
char* bytes = "\0";
ByteString str = {.bytes = bytes, .length = 0, .owning = false};
return str;
}
// bstring_from_cstring creates a new string that wraps an existing C string.
static ByteString bstring_from_cstring(const char* const cstring, size_t length) {
ByteString str = {.bytes = cstring, .length = length, .owning = false};
return str;
}
// bstring_clone creates a new string by copying an existing C string.
static ByteString bstring_clone(const char* const cstring, size_t length) {
char* bytes = calloc(length + 1, sizeof(char));
if (bytes == NULL) {
ByteString str = {NULL, 0, true};
return str;
}
memcpy(bytes, cstring, length);
ByteString str = {bytes, length, true};
return str;
}
// bstring_to_cstring converts the string to a zero-terminated C string.
static const char* bstring_to_cstring(ByteString str) {
if (str.bytes == NULL) {
return NULL;
}
return str.bytes;
}
// bstring_free destroys the string, freeing resources if necessary.
static void bstring_free(ByteString str) {
if (str.owning && str.bytes != NULL) {
free((void*)str.bytes);
}
}
// bstring_at returns a character by its index in the string.
static char bstring_at(ByteString str, size_t idx) {
if (str.length == 0) {
return 0;
}
if (idx < 0 || idx >= str.length) {
return 0;
};
return str.bytes[idx];
}
// bstring_slice returns a slice of the string,
// from the `start` index (inclusive) to the `end` index (non-inclusive).
// Negative `start` and `end` values count from the end of the string.
static ByteString bstring_slice(ByteString str, int start, int end) {
if (str.length == 0) {
return bstring_new();
}
// adjusted start index
start = start < 0 ? str.length + start : start;
// python-compatible: treat negative start index larger than the length of the string as zero
start = start < 0 ? 0 : start;
// adjusted start index should be less the the length of the string
if (start >= (int)str.length) {
return bstring_new();
}
// adjusted end index
end = end < 0 ? str.length + end : end;
// python-compatible: treat end index larger than the length of the string
// as equal to the length
end = end > (int)str.length ? (int)str.length : end;
// adjusted end index should be >= 0
if (end < 0) {
return bstring_new();
}
// adjusted start index should be less than adjusted end index
if (start >= end) {
return bstring_new();
}
char* at = (char*)str.bytes + start;
size_t length = end - start;
ByteString slice = bstring_clone(at, length);
return slice;
}
// bstring_substring returns a substring of `length` characters,
// starting from the `start` index.
static ByteString bstring_substring(ByteString str, size_t start, size_t length) {
if (length > str.length - start) {
length = str.length - start;
}
return bstring_slice(str, start, start + length);
}
// bstring_contains_after checks if the other string is a substring of the original string,
// starting at the `start` index.
static bool bstring_contains_after(ByteString str, ByteString other, size_t start) {
if (start + other.length > str.length) {
return false;
}
for (size_t idx = 0; idx < other.length; idx++) {
if (str.bytes[start + idx] != other.bytes[idx]) {
return false;
}
}
return true;
}
// bstring_index_char returns the first index of the character in the string
// after the `start` index, inclusive.
static int bstring_index_char(ByteString str, char chr, size_t start) {
for (size_t idx = start; idx < str.length; idx++) {
if (str.bytes[idx] == chr) {
return idx;
}
}
return -1;
}
// bstring_last_index_char returns the last index of the character in the string
// before the `end` index, inclusive.
static int bstring_last_index_char(ByteString str, char chr, size_t end) {
if (end >= str.length) {
return -1;
}
for (int idx = end; idx >= 0; idx--) {
if (str.bytes[idx] == chr) {
return idx;
}
}
return -1;
}
// bstring_index_after returns the index of the substring in the original string
// after the `start` index, inclusive.
static int bstring_index_after(ByteString str, ByteString other, size_t start) {
if (other.length == 0) {
return start;
}
if (str.length == 0 || other.length > str.length) {
return -1;
}
size_t cur_idx = start;
while (cur_idx < str.length) {
int match_idx = bstring_index_char(str, other.bytes[0], cur_idx);
if (match_idx == -1) {
return match_idx;
}
if (bstring_contains_after(str, other, match_idx)) {
return match_idx;
}
cur_idx = match_idx + 1;
}
return -1;
}
// bstring_index returns the first index of the substring in the original string.
static int bstring_index(ByteString str, ByteString other) {
return bstring_index_after(str, other, 0);
}
// bstring_last_index returns the last index of the substring in the original string.
static int bstring_last_index(ByteString str, ByteString other) {
if (other.length == 0) {
return str.length - 1;
}
if (str.length == 0 || other.length > str.length) {
return -1;
}
int cur_idx = str.length - 1;
while (cur_idx >= 0) {
int match_idx = bstring_last_index_char(str, other.bytes[0], cur_idx);
if (match_idx == -1) {
return match_idx;
}
if (bstring_contains_after(str, other, match_idx)) {
return match_idx;
}
cur_idx = match_idx - 1;
}
return -1;
}
// bstring_contains checks if the string contains the substring.
static bool bstring_contains(ByteString str, ByteString other) {
return bstring_index(str, other) != -1;
}
// bstring_equals checks if two strings are equal character by character.
static bool bstring_equals(ByteString str, ByteString other) {
if (str.bytes == NULL && other.bytes == NULL) {
return true;
}
if (str.bytes == NULL || other.bytes == NULL) {
return false;
}
if (str.length != other.length) {
return false;
}
return bstring_contains_after(str, other, 0);
}
// bstring_has_prefix checks if the string starts with the `other` substring.
static bool bstring_has_prefix(ByteString str, ByteString other) {
return bstring_index(str, other) == 0;
}
// bstring_has_suffix checks if the string ends with the `other` substring.
static bool bstring_has_suffix(ByteString str, ByteString other) {
if (other.length == 0) {
return true;
}
int idx = bstring_last_index(str, other);
return idx < 0 ? false : (size_t)idx == (str.length - other.length);
}
// bstring_count counts how many times the `other` substring is contained in the original string.
static size_t bstring_count(ByteString str, ByteString other) {
if (str.length == 0 || other.length == 0 || other.length > str.length) {
return 0;
}
size_t count = 0;
size_t char_idx = 0;
while (char_idx < str.length) {
int match_idx = bstring_index_after(str, other, char_idx);
if (match_idx == -1) {
break;
}
count += 1;
char_idx = match_idx + other.length;
}
return count;
}
// bstring_split_part splits the string by the separator and returns the nth part (0-based).
static ByteString bstring_split_part(ByteString str, ByteString sep, size_t part) {
if (str.length == 0 || sep.length > str.length) {
return bstring_new();
}
if (sep.length == 0) {
if (part == 0) {
return bstring_slice(str, 0, str.length);
} else {
return bstring_new();
}
}
size_t found = 0;
size_t prev_idx = 0;
size_t char_idx = 0;
while (char_idx < str.length) {
int match_idx = bstring_index_after(str, sep, char_idx);
if (match_idx == -1) {
break;
}
if (found == part) {
return bstring_slice(str, prev_idx, match_idx);
}
found += 1;
prev_idx = match_idx + sep.length;
char_idx = match_idx + sep.length;
}
if (found == part) {
return bstring_slice(str, prev_idx, str.length);
}
return bstring_new();
}
// bstring_join joins strings using the separator and returns the resulting string.
static ByteString bstring_join(ByteString* strings, size_t count, ByteString sep) {
// calculate total string length
size_t total_length = 0;
for (size_t idx = 0; idx < count; idx++) {
ByteString str = strings[idx];
total_length += str.length;
// no separator after the last one
if (idx != count - 1) {
total_length += sep.length;
}
}
// allocate memory for the bytes
size_t total_size = total_length * sizeof(char);
char* bytes = malloc(total_size + 1);
if (bytes == NULL) {
ByteString str = {NULL, 0, false};
return str;
}
// copy bytes from each string with separator in between
char* at = bytes;
for (size_t idx = 0; idx < count; idx++) {
ByteString str = strings[idx];
memcpy(at, str.bytes, str.length);
at += str.length;
if (idx != count - 1 && sep.length != 0) {
memcpy(at, sep.bytes, sep.length);
at += sep.length;
}
}
bytes[total_length] = '\0';
ByteString str = {bytes, total_length, true};
return str;
}
// bstring_concat concatenates strings and returns the resulting string.
static ByteString bstring_concat(ByteString* strings, size_t count) {
ByteString sep = bstring_new();
return bstring_join(strings, count, sep);
}
// bstring_repeat concatenates the string to itself a given number of times
// and returns the resulting string.
static ByteString bstring_repeat(ByteString str, size_t count) {
// calculate total string length
size_t total_length = str.length * count;
// allocate memory for the bytes
size_t total_size = total_length * sizeof(char);
char* bytes = malloc(total_size + 1);
if (bytes == NULL) {
ByteString res = {NULL, 0, false};
return res;
}
// copy bytes
char* at = bytes;
for (size_t idx = 0; idx < count; idx++) {
memcpy(at, str.bytes, str.length);
at += str.length;
}
bytes[total_size] = '\0';
ByteString res = {bytes, total_length, true};
return res;
}
// bstring_replace replaces the `old` substring with the `new` substring in the original string,
// but not more than `max_count` times.
static ByteString bstring_replace(ByteString str,
ByteString old,
ByteString new,
size_t max_count) {
// count matches of the old string in the source string
size_t count = bstring_count(str, old);
if (count == 0) {
return bstring_slice(str, 0, str.length);
}
// limit the number of replacements
if (max_count >= 0 && count > max_count) {
count = max_count;
}
// k matches split string into (k+1) parts
// allocate an array for them
size_t parts_count = count + 1;
ByteString* strings = malloc(parts_count * sizeof(ByteString));
if (strings == NULL) {
ByteString res = {NULL, 0, false};
return res;
}
// split the source string where it matches the old string
// and fill the strings array with these parts
size_t part_idx = 0;
size_t char_idx = 0;
while (char_idx < str.length && part_idx < count) {
int match_idx = bstring_index_after(str, old, char_idx);
if (match_idx == -1) {
break;
}
// slice from the prevoius match to the current match
strings[part_idx] = bstring_slice(str, char_idx, match_idx);
part_idx += 1;
char_idx = match_idx + old.length;
}
// "tail" from the last match to the end of the source string
strings[part_idx] = bstring_slice(str, char_idx, str.length);
// join all the parts using new string as a separator
ByteString res = bstring_join(strings, parts_count, new);
// free string parts
for (size_t idx = 0; idx < parts_count; idx++) {
bstring_free(strings[idx]);
}
free(strings);
return res;
}
// bstring_replace_all replaces all `old` substrings with the `new` substrings
// in the original string.
static ByteString bstring_replace_all(ByteString str, ByteString old, ByteString new) {
return bstring_replace(str, old, new, -1);
}
// bstring_reverse returns the reversed string.
static ByteString bstring_reverse(ByteString str) {
ByteString res = bstring_clone(str.bytes, str.length);
char* bytes = (char*)res.bytes;
for (size_t i = 0; i < str.length / 2; i++) {
char r = bytes[i];
bytes[i] = bytes[str.length - 1 - i];
bytes[str.length - 1 - i] = r;
}
return res;
}
// bstring_trim_left trims whitespaces from the beginning of the string.
static ByteString bstring_trim_left(ByteString str) {
if (str.length == 0) {
return bstring_new();
}
size_t idx = 0;
for (; idx < str.length; idx++) {
if (!isspace(str.bytes[idx])) {
break;
}
}
return bstring_slice(str, idx, str.length);
}
// bstring_trim_right trims whitespaces from the end of the string.
static ByteString bstring_trim_right(ByteString str) {
if (str.length == 0) {
return bstring_new();
}
size_t idx = str.length - 1;
for (; idx >= 0; idx--) {
if (!isspace(str.bytes[idx])) {
break;
}
}
return bstring_slice(str, 0, idx + 1);
}
// bstring_trim trims whitespaces from the beginning and end of the string.
static ByteString bstring_trim(ByteString str) {
if (str.length == 0) {
return bstring_new();
}
size_t left = 0;
for (; left < str.length; left++) {
if (!isspace(str.bytes[left])) {
break;
}
}
size_t right = str.length - 1;
for (; right >= 0; right--) {
if (!isspace(str.bytes[right])) {
break;
}
}
return bstring_slice(str, left, right + 1);
}
// bstring_print prints the string to stdout.
static void bstring_print(ByteString str) {
if (str.bytes == NULL) {
printf("<null>\n");
return;
}
printf("'%s' (len=%zu)\n", str.bytes, str.length);
}
struct bstring_ns bstring = {
.new = bstring_new,
.to_cstring = bstring_to_cstring,
.from_cstring = bstring_from_cstring,
.free = bstring_free,
.at = bstring_at,
.slice = bstring_slice,
.substring = bstring_substring,
.index = bstring_index,
.last_index = bstring_last_index,
.contains = bstring_contains,
.equals = bstring_equals,
.has_prefix = bstring_has_prefix,
.has_suffix = bstring_has_suffix,
.count = bstring_count,
.split_part = bstring_split_part,
.join = bstring_join,
.concat = bstring_concat,
.repeat = bstring_repeat,
.replace = bstring_replace,
.replace_all = bstring_replace_all,
.reverse = bstring_reverse,
.trim_left = bstring_trim_left,
.trim_right = bstring_trim_right,
.trim = bstring_trim,
.print = bstring_print,
};
// ---------------------------------
// src/text/extension.c
// ---------------------------------
// Copyright (c) 2023 Anton Zhiyanov, MIT License
// https://github.com/nalgeon/sqlean
// SQLite extension for working with text.
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
SQLITE_EXTENSION_INIT3
#pragma region Substrings
// Extracts a substring starting at the `start` position (1-based).
// text_substring(str, start)
// [pg-compatible] substr(string, start)
static void text_substring2(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "start parameter should be integer", -1);
return;
}
int start = sqlite3_value_int(argv[1]);
// convert to 0-based index
// postgres-compatible: treat negative index as zero
start = start > 0 ? start - 1 : 0;
RuneString s_src = rstring.from_cstring(src);
RuneString s_res = rstring.slice(s_src, start, s_src.length);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
// Extracts a substring of `length` characters starting at the `start` position (1-based).
// text_substring(str, start, length)
// [pg-compatible] substr(string, start, count)
static void text_substring3(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 3);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "start parameter should be integer", -1);
return;
}
int start = sqlite3_value_int(argv[1]);
if (sqlite3_value_type(argv[2]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "length parameter should be integer", -1);
return;
}
int length = sqlite3_value_int(argv[2]);
if (length < 0) {
sqlite3_result_error(context, "length parameter should >= 0", -1);
return;
}
// convert to 0-based index
start -= 1;
// postgres-compatible: treat negative start as 0, but shorten the length accordingly
if (start < 0) {
length += start;
start = 0;
}
// zero-length substring
if (length <= 0) {
sqlite3_result_text(context, "", -1, SQLITE_TRANSIENT);
return;
}
RuneString s_src = rstring.from_cstring(src);
// postgres-compatible: the substring cannot be longer the the original string
if ((size_t)length > s_src.length) {
length = s_src.length;
}
RuneString s_res = rstring.substring(s_src, start, length);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
// Extracts a substring starting at the `start` position (1-based).
// text_slice(str, start)
static void text_slice2(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "start parameter should be integer", -1);
return;
}
int start = sqlite3_value_int(argv[1]);
// convert to 0-based index
start = start > 0 ? start - 1 : start;
RuneString s_src = rstring.from_cstring(src);
// python-compatible: treat negative index larger than the length of the string as zero
// and return the original string
if (start < -(int)s_src.length) {
sqlite3_result_text(context, src, -1, SQLITE_TRANSIENT);
rstring.free(s_src);
return;
}
RuneString s_res = rstring.slice(s_src, start, s_src.length);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
// Extracts a substring from `start` position inclusive to `end` position non-inclusive (1-based).
// text_slice(str, start, end)
static void text_slice3(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 3);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "start parameter should be integer", -1);
return;
}
int start = sqlite3_value_int(argv[1]);
// convert to 0-based index
start = start > 0 ? start - 1 : start;
if (sqlite3_value_type(argv[2]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "end parameter should be integer", -1);
return;
}
int end = sqlite3_value_int(argv[2]);
// convert to 0-based index
end = end > 0 ? end - 1 : end;
RuneString s_src = rstring.from_cstring(src);
RuneString s_res = rstring.slice(s_src, start, end);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
// Extracts a substring of `length` characters from the beginning of the string.
// For `length < 0`, extracts all but the last `|length|` characters.
// text_left(str, length)
// [pg-compatible] left(string, n)
static void text_left(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "length parameter should be integer", -1);
return;
}
int length = sqlite3_value_int(argv[1]);
RuneString s_src = rstring.from_cstring(src);
if (length < 0) {
length = s_src.length + length;
length = length >= 0 ? length : 0;
}
RuneString s_res = rstring.substring(s_src, 0, length);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
// Extracts a substring of `length` characters from the end of the string.
// For `length < 0`, extracts all but the first `|length|` characters.
// text_right(str, length)
// [pg-compatible] right(string, n)
static void text_right(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "length parameter should be integer", -1);
return;
}
int length = sqlite3_value_int(argv[1]);
RuneString s_src = rstring.from_cstring(src);
length = (length < 0) ? (int)s_src.length + length : length;
int start = (int)s_src.length - length;
start = start < 0 ? 0 : start;
RuneString s_res = rstring.substring(s_src, start, length);
char* res = rstring.to_cstring(s_res);
sqlite3_result_text(context, res, -1, free);
rstring.free(s_src);
rstring.free(s_res);
}
#pragma endregion
#pragma region Search and match
// Returns the first index of the substring in the original string.
// text_index(str, other)
// [pg-compatible] strpos(string, substring)
static void text_index(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
RuneString s_src = rstring.from_cstring(src);
RuneString s_other = rstring.from_cstring(other);
int idx = rstring.index(s_src, s_other);
sqlite3_result_int64(context, idx + 1);
rstring.free(s_src);
rstring.free(s_other);
}
// Returns the last index of the substring in the original string.
// text_last_index(str, other)
static void text_last_index(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
RuneString s_src = rstring.from_cstring(src);
RuneString s_other = rstring.from_cstring(other);
int idx = rstring.last_index(s_src, s_other);
sqlite3_result_int64(context, idx + 1);
rstring.free(s_src);
rstring.free(s_other);
}
// Checks if the string contains the substring.
// text_contains(str, other)
static void text_contains(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
ByteString s_src = bstring.from_cstring(src, sqlite3_value_bytes(argv[0]));
ByteString s_other = bstring.from_cstring(other, sqlite3_value_bytes(argv[1]));
bool found = bstring.contains(s_src, s_other);
sqlite3_result_int(context, found);
bstring.free(s_src);
bstring.free(s_other);
}
// Checks if the string starts with the substring.
// text_has_prefix(str, other)
// [pg-compatible] starts_with(string, prefix)
static void text_has_prefix(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
ByteString s_src = bstring.from_cstring(src, sqlite3_value_bytes(argv[0]));
ByteString s_other = bstring.from_cstring(other, sqlite3_value_bytes(argv[1]));
bool found = bstring.has_prefix(s_src, s_other);
sqlite3_result_int(context, found);
bstring.free(s_src);
bstring.free(s_other);
}
// Checks if the string ends with the substring.
// text_has_suffix(str, other)
static void text_has_suffix(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
ByteString s_src = bstring.from_cstring(src, sqlite3_value_bytes(argv[0]));
ByteString s_other = bstring.from_cstring(other, sqlite3_value_bytes(argv[1]));
bool found = bstring.has_suffix(s_src, s_other);
sqlite3_result_int(context, found);
bstring.free(s_src);
bstring.free(s_other);
}
// Counts how many times the substring is contained in the original string.
// text_count(str, other)
static void text_count(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 2);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* other = (char*)sqlite3_value_text(argv[1]);
if (other == NULL) {
sqlite3_result_null(context);
return;
}
ByteString s_src = bstring.from_cstring(src, sqlite3_value_bytes(argv[0]));
ByteString s_other = bstring.from_cstring(other, sqlite3_value_bytes(argv[1]));
size_t count = bstring.count(s_src, s_other);
sqlite3_result_int(context, count);
bstring.free(s_src);
bstring.free(s_other);
}
#pragma endregion
#pragma region Split and join
// Splits a string by a separator and returns the n-th part (counting from one).
// When n is negative, returns the |n|'th-from-last part.
// text_split(str, sep, n)
// [pg-compatible] split_part(string, delimiter, n)
static void text_split(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 3);
const char* src = (char*)sqlite3_value_text(argv[0]);
if (src == NULL) {
sqlite3_result_null(context);
return;
}
const char* sep = (const char*)sqlite3_value_text(argv[1]);
if (sep == NULL) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[2]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "part parameter should be integer", -1);
return;
}
int part = sqlite3_value_int(argv[2]);
// pg-compatible
if (part == 0) {
sqlite3_result_error(context, "part parameter should not be 0", -1);
return;
}
// convert to 0-based index
part = part > 0 ? part - 1 : part;
ByteString s_src = bstring.from_cstring(src, strlen(src));
ByteString s_sep = bstring.from_cstring(sep, strlen(sep));
// count from the last part backwards
if (part < 0) {
int n_parts = bstring.count(s_src, s_sep) + 1;
part = n_parts + part;
}
ByteString s_part = bstring.split_part(s_src, s_sep, part);
sqlite3_result_text(context, s_part.bytes, -1, SQLITE_TRANSIENT);
bstring.free(s_src);
bstring.free(s_sep);
bstring.free(s_part);
}
// Joins strings using the separator and returns the resulting string. Ignores nulls.
// text_join(sep, str, ...)
// [pg-compatible] concat_ws(sep, val1[, val2 [, ...]])
static void text_join(sqlite3_context* context, int argc, sqlite3_value** argv) {
if (argc < 2) {
sqlite3_result_error(context, "expected at least 2 parameters", -1);
return;
}
// separator
const char* sep = (char*)sqlite3_value_text(argv[0]);
if (sep == NULL) {
sqlite3_result_null(context);
return;
}
ByteString s_sep = bstring.from_cstring(sep, sqlite3_value_bytes(argv[0]));