sqlite3_mod_regexp.c 962 B

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