|
@@ -1,36 +1,38 @@
|
|
-//#define PY_SSIZE_T_CLEAN
|
|
|
|
-//#include <Python.h>
|
|
|
|
-
|
|
|
|
#include "term.h"
|
|
#include "term.h"
|
|
|
|
|
|
#define CHR sizeof(char)
|
|
#define CHR sizeof(char)
|
|
#define NLEN(str) (str) == NULL ? 0 : strlen((str))
|
|
#define NLEN(str) (str) == NULL ? 0 : strlen((str))
|
|
|
|
|
|
|
|
+static regex_t ptn;
|
|
|
|
+static bool ptn_init = false;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Free global regex struct. Register with atexit().
|
|
|
|
+ */
|
|
|
|
+void term_cleanup() { if (ptn_init) regfree(&ptn); }
|
|
|
|
+
|
|
|
|
+
|
|
int
|
|
int
|
|
LSUP_term_init(
|
|
LSUP_term_init(
|
|
LSUP_Term *term, LSUP_term_type type,
|
|
LSUP_Term *term, LSUP_term_type type,
|
|
- char *data, char *datatype, char *lang) {
|
|
|
|
-
|
|
|
|
|
|
+ char *data, char *datatype, char *lang)
|
|
|
|
+{
|
|
term->type = type;
|
|
term->type = type;
|
|
if (data == NULL) return -1;
|
|
if (data == NULL) return -1;
|
|
|
|
|
|
if (term->type == LSUP_TERM_URI) {
|
|
if (term->type == LSUP_TERM_URI) {
|
|
- TRACE(STR, "Checking URI term.");
|
|
|
|
- // TODO Move this to a code block that is only executed once.
|
|
|
|
- regex_t ptn;
|
|
|
|
- int status = regcomp(&ptn, URI_REGEX_STR, REG_EXTENDED|REG_NOSUB);
|
|
|
|
- assert(status == 0);
|
|
|
|
- //TRACE(STR, "Regex compiled.");
|
|
|
|
-
|
|
|
|
- //TRACE("Checking data: %s", data);
|
|
|
|
- status = regexec(&ptn, data, 0, NULL, 0);
|
|
|
|
- regfree(&ptn);
|
|
|
|
- if (status != 0) {
|
|
|
|
|
|
+ if (UNLIKELY(!ptn_init)) {
|
|
|
|
+ assert (regcomp(&ptn, URI_REGEX_STR, REG_EXTENDED) == 0);
|
|
|
|
+ ptn_init = true;
|
|
|
|
+ atexit(term_cleanup);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (regexec(&ptn, data, 0, NULL, 0) != 0) {
|
|
printf("Error matching URI pattern.\n");
|
|
printf("Error matching URI pattern.\n");
|
|
|
|
|
|
- return(-1);
|
|
|
|
|
|
+ return -1;
|
|
}
|
|
}
|
|
- TRACE(STR, "URI checked.");
|
|
|
|
}
|
|
}
|
|
|
|
|
|
term->data = malloc(strlen(data) + 1);
|
|
term->data = malloc(strlen(data) + 1);
|
|
@@ -105,7 +107,7 @@ LSUP_term_serialize(const LSUP_Term *term, LSUP_Buffer *sterm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- TRACE("Serialized term size: %lu", size);
|
|
|
|
|
|
+ //TRACE("Serialized term size: %lu", size);
|
|
LSUP_buffer_init(sterm, size);
|
|
LSUP_buffer_init(sterm, size);
|
|
|
|
|
|
// Copy type.
|
|
// Copy type.
|