Skip to content

Commit

Permalink
impr: regexp - print the actual error message if compilation fails (#104
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nalgeon committed Feb 2, 2024
1 parent 7a59981 commit 84671a0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/regexp/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ static void regexp_statement(sqlite3_context* context, int argc, sqlite3_value**
if (re == NULL) {
re = regexp.compile(pattern);
if (re == NULL) {
sqlite3_result_error_nomem(context);
char* msg = regexp.get_error(pattern);
sqlite3_result_error(context, msg, -1);
free(msg);
return;
}
is_new_re = true;
Expand Down Expand Up @@ -121,7 +123,9 @@ static void regexp_like(sqlite3_context* context, int argc, sqlite3_value** argv
if (re == NULL) {
re = regexp.compile(pattern);
if (re == NULL) {
sqlite3_result_error_nomem(context);
char* msg = regexp.get_error(pattern);
sqlite3_result_error(context, msg, -1);
free(msg);
return;
}
is_new_re = true;
Expand Down Expand Up @@ -171,7 +175,9 @@ static void regexp_substr(sqlite3_context* context, int argc, sqlite3_value** ar
if (re == NULL) {
re = regexp.compile(pattern);
if (re == NULL) {
sqlite3_result_error_nomem(context);
char* msg = regexp.get_error(pattern);
sqlite3_result_error(context, msg, -1);
free(msg);
return;
}
is_new_re = true;
Expand Down Expand Up @@ -243,7 +249,9 @@ static void regexp_capture(sqlite3_context* context, int argc, sqlite3_value** a
if (re == NULL) {
re = regexp.compile(pattern);
if (re == NULL) {
sqlite3_result_error_nomem(context);
char* msg = regexp.get_error(pattern);
sqlite3_result_error(context, msg, -1);
free(msg);
return;
}
is_new_re = true;
Expand Down Expand Up @@ -309,7 +317,9 @@ static void regexp_replace(sqlite3_context* context, int argc, sqlite3_value** a
if (re == NULL) {
re = regexp.compile(pattern);
if (re == NULL) {
sqlite3_result_error_nomem(context);
char* msg = regexp.get_error(pattern);
sqlite3_result_error(context, msg, -1);
free(msg);
return;
}
is_new_re = true;
Expand Down
29 changes: 29 additions & 0 deletions src/regexp/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ static void re_free(pcre2_code* re) {
pcre2_code_free(re);
}

/*
* re_get_error returns the error message for a given pattern.
*/
static char* re_get_error(const char* pattern) {
size_t erroffset;
int errcode;
uint32_t options = PCRE2_UCP | PCRE2_UTF;
pcre2_code* re = pcre2_compile((PCRE2_SPTR8)pattern, PCRE2_ZERO_TERMINATED, options, &errcode,
&erroffset, NULL);

if (re != NULL) {
// free the compiled pattern if successful
pcre2_code_free(re);
return NULL;
}

PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errcode, buffer, sizeof(buffer));

// Allocate memory for the error message
// (additional space for formatting)
char* msg = (char*)malloc(256 + 32);
if (msg != NULL) {
snprintf(msg, 256 + 32, "%s (offset %d)", buffer, (int)erroffset);
}
return msg;
}

/*
* re_like checks if source string matches pattern.
* returns:
Expand Down Expand Up @@ -143,6 +171,7 @@ static int re_replace(pcre2_code* re, const char* source, const char* repl, char
struct regexp_ns regexp = {
.compile = re_compile,
.free = re_free,
.get_error = re_get_error,
.like = re_like,
.extract = re_extract,
.replace = re_replace,
Expand Down
1 change: 1 addition & 0 deletions src/regexp/regexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
struct regexp_ns {
pcre2_code* (*compile)(const char* pattern);
void (*free)(pcre2_code* re);
char* (*get_error)(const char* pattern);
int (*like)(pcre2_code* re, const char* source);
int (*extract)(pcre2_code* re, const char* source, size_t group_idx, char** substr);
int (*replace)(pcre2_code* re, const char* source, const char* repl, char** dest);
Expand Down

0 comments on commit 84671a0

Please sign in to comment.