Skip to content

Instantly share code, notes, and snippets.

@zombiezen
Created June 14, 2020 18:09
Show Gist options
  • Save zombiezen/99ae6fd2eeb70e7a20b94effdd68e4a2 to your computer and use it in GitHub Desktop.
Save zombiezen/99ae6fd2eeb70e7a20b94effdd68e4a2 to your computer and use it in GitHub Desktop.
SQLite-loadable RE2 function
re2.so: re2.c++
g++ \
-I../sqlite-amalgamation-3320200 \
$(shell pkg-config --libs --cflags re2) \
-g -fPIC -shared \
re2.c++ \
-o re2.so
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <unistd.h>
#include <assert.h>
#include <sqlite3ext.h>
#include <re2/re2.h>
SQLITE_EXTENSION_INIT1
static void regexpfunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
) {
assert(argc == 2);
if (sqlite3_value_type(argv[0]) == SQLITE_NULL || sqlite3_value_type(argv[1]) == SQLITE_NULL) {
return;
}
bool matches = RE2::PartialMatch(
(const char *)sqlite3_value_text(argv[1]),
(const char *)sqlite3_value_text(argv[0]));
sqlite3_result_int(context, matches ? 1 : 0);
}
#ifdef _WIN32
__declspec(dllexport)
#endif
extern "C"
int sqlite3_re_init(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pApi
) {
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
rc = sqlite3_create_function_v2(
db,
"regexp",
2 /* nArg */,
SQLITE_UTF8|SQLITE_DETERMINISTIC,
NULL /* pApp */,
regexpfunc,
NULL /* xStep */,
NULL /* xFinal */,
NULL /* xDestroy */);
return rc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment