Fork github.com/mattn/go-sqlite3 with adjustment for go1.16.2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

  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. if (!re) {
  16. sqlite3_result_error(context, errstr, 0);
  17. return;
  18. }
  19. rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500);
  20. if (rc <= 0) {
  21. sqlite3_result_int(context, 0);
  22. return;
  23. }
  24. sqlite3_result_int(context, 1);
  25. }
  26. }
  27. #ifdef _WIN32
  28. __declspec(dllexport)
  29. #endif
  30. int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) {
  31. SQLITE_EXTENSION_INIT2(api);
  32. return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, (void*)db, regexp_func, NULL, NULL);
  33. }