forked from nalgeon/sqlean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite3-text.c
176 lines (156 loc) · 5.39 KB
/
sqlite3-text.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
// Originally by Liam Healy, Public Domain
// extension-functions.c at https://sqlite.org/contrib/
// Modified by Anton Zhiyanov, https://github.com/nalgeon/sqlean, MIT License
/*
* SQLite text functions.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlean.h"
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
/**
* From sqlite3 utf.c
*/
static const unsigned char sqlite3Utf8Trans1[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
};
#define READ_UTF8(zIn, zTerm, c) \
c = *(zIn++); \
if (c >= 0xc0) { \
c = sqlite3Utf8Trans1[c - 0xc0]; \
while (zIn != zTerm && (*zIn & 0xc0) == 0x80) { \
c = (c << 6) + (0x3f & *(zIn++)); \
} \
if (c < 0x80 || (c & 0xFFFFF800) == 0xD800 || (c & 0xFFFFFFFE) == 0xFFFE) { \
c = 0xFFFD; \
} \
}
/*
* reverse() and friends extracted from
* extension-functions.c (https://sqlite.org/contrib/)
* by Liam Healy
*/
#define advance_char(X) \
while ((0xc0 & *++(X)) == 0x80) { \
}
static int read_char(const unsigned char* str) {
int c;
READ_UTF8(str, 0, c);
return c;
}
/*
* Returns reversed string.
* reverse("abcde") == "edcba"
*/
static char* reverse(const char* source) {
int len = strlen(source);
char* result = sqlite3_malloc(len + 1);
char* rzt = result + len;
*(rzt--) = '\0';
const char* zt = source;
while (read_char((unsigned char*)zt) != 0) {
source = zt;
advance_char(zt);
for (int i = 1; zt - i >= source; ++i) {
*(rzt--) = *(zt - i);
}
}
return result;
}
static void sqlite3_reverse(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 1);
if (sqlite3_value_type(argv[0]) == SQLITE_NULL) {
sqlite3_result_null(context);
return;
}
const char* source = (char*)sqlite3_value_text(argv[0]);
char* result = reverse(source);
sqlite3_result_text(context, result, -1, sqlite3_free);
}
/*
* strsep() implementation, Windows doesn't have it
* copied from https://unixpapa.com/incnote/string.html
*/
static char* str_sep(char** sp, const char* sep) {
if (sp == NULL || *sp == NULL || **sp == '\0') {
return NULL;
}
char* s = *sp;
char* p = s + strcspn(s, sep);
if (*p != '\0')
*p++ = '\0';
*sp = p;
return s;
}
/*
* Splits `source` string on `sep` and returns the given `part` (counting from one)
* split_part("one;two;three", ";", 2) == "two"
*/
static char* split_part(char* source, const char* sep, int64_t part) {
char* token;
int64_t index = 1;
while ((token = str_sep(&source, sep)) != NULL) {
if (index == part) {
break;
}
index++;
}
return token;
}
static void sqlite3_split_part(sqlite3_context* context, int argc, sqlite3_value** argv) {
assert(argc == 3);
char* source = (char*)sqlite3_value_text(argv[0]);
if (source == NULL) {
sqlite3_result_null(context);
return;
}
if (strcmp(source, "") == 0) {
sqlite3_result_text(context, "", -1, SQLITE_TRANSIENT);
return;
}
const char* sep = (const char*)sqlite3_value_text(argv[1]);
if (!sep) {
sqlite3_result_null(context);
return;
}
if (sqlite3_value_type(argv[2]) != SQLITE_INTEGER) {
sqlite3_result_error(context, "part parameter should be integer", -1);
return;
}
int64_t part = sqlite3_value_int64(argv[2]);
if (part <= 0) {
sqlite3_result_error(context, "part parameter should be > 0", -1);
return;
}
char* token = split_part(source, sep, part);
if (token == NULL) {
sqlite3_result_text(context, "", -1, SQLITE_TRANSIENT);
return;
}
sqlite3_result_text(context, token, -1, SQLITE_TRANSIENT);
}
// Returns the current Sqlean version.
static void sqlean_version(sqlite3_context* context, int argc, sqlite3_value** argv) {
sqlite3_result_text(context, SQLEAN_VERSION, -1, SQLITE_STATIC);
}
/*
* Registers the extension.
*/
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_text_init(sqlite3* db, char** pzErrMsg, const sqlite3_api_routines* pApi) {
SQLITE_EXTENSION_INIT2(pApi);
static const int flags = SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC;
sqlite3_create_function(db, "reverse", 1, flags, 0, sqlite3_reverse, 0, 0);
sqlite3_create_function(db, "split_part", 3, flags, 0, sqlite3_split_part, 0, 0);
sqlite3_create_function(db, "sqlean_version", 0, flags, 0, sqlean_version, 0, 0);
return SQLITE_OK;
}