| 1 |
#include <pcre.h> |
| 2 |
#include <string.h> |
| 3 |
#include <stdio.h> |
| 4 |
#include <sqlite3ext.h> |
| 5 |
|
| 6 |
SQLITE_EXTENSION_INIT1 |
| 7 |
static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) { |
| 8 |
if (argc >= 2) { |
| 9 |
const char *target = (const char *)sqlite3_value_text(argv[1]); |
| 10 |
const char *pattern = (const char *)sqlite3_value_text(argv[0]); |
| 11 |
const char* errstr = NULL; |
| 12 |
int erroff = 0; |
| 13 |
int vec[500]; |
| 14 |
int n, rc; |
| 15 |
pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL); |
| 16 |
if (!re) { |
| 17 |
sqlite3_result_error(context, errstr, 0); |
| 18 |
return; |
| 19 |
} |
| 20 |
rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); |
| 21 |
if (rc <= 0) { |
| 22 |
sqlite3_result_int(context, 0); |
| 23 |
return; |
| 24 |
} |
| 25 |
sqlite3_result_int(context, 1); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
#ifdef _WIN32 |
| 30 |
__declspec(dllexport) |
| 31 |
#endif |
| 32 |
int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) { |
| 33 |
SQLITE_EXTENSION_INIT2(api); |
| 34 |
return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL); |
| 35 |
} |