Created
October 24, 2019 11:50
-
-
Save malcommac/49ec95570915115fec39913a16f54228 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqlite3_create_function(dbSqlitez, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL); | |
... | |
... | |
static void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values) { | |
if ( argc < 2 ) { | |
sqlite3_result_error(context, "SQL function regexp() called with missing arguments.", -1); | |
return; | |
} | |
char* reg = (char*)sqlite3_value_text(values[0]); | |
char* text = (char*)sqlite3_value_text(values[1]); | |
if ( argc != 2 || reg == 0 || text == 0) { | |
sqlite3_result_error(context, "SQL function regexp() called with invalid arguments.", -1); | |
return; | |
} | |
int ret; | |
regex_t regex; | |
ret = regcomp(®ex, reg, REG_EXTENDED | REG_NOSUB); | |
if ( ret != 0 ) { | |
sqlite3_result_error(context, "error compiling regular expression", -1); | |
return; | |
} | |
ret = regexec(®ex, text , 0, NULL, 0); | |
regfree(®ex); | |
sqlite3_result_int(context, (ret != REG_NOMATCH)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment