|
@@ -16,12 +16,19 @@
|
|
|
* Data structures.
|
|
|
*/
|
|
|
|
|
|
+/// Sub-match coordinates in IRI parsing results.
|
|
|
+typedef struct match_coord_t {
|
|
|
+ size_t offset; ///< Offset of match from start of string.
|
|
|
+ size_t size; ///< Length of match.
|
|
|
+} MatchCoord;
|
|
|
+
|
|
|
+
|
|
|
/// Matching sub-patterns for IRI parts.
|
|
|
struct iri_info_t {
|
|
|
LSUP_NSMap * nsm; ///< NSM handle for prefixed IRI.
|
|
|
- regmatch_t prefix; ///< Matching group #1.
|
|
|
- regmatch_t path; ///< Matching group #5.
|
|
|
- regmatch_t frag; ///< Matching group #10.
|
|
|
+ MatchCoord prefix; ///< URI prefix (scheme + authority).
|
|
|
+ MatchCoord path; ///< URI path (including fragment).
|
|
|
+ MatchCoord frag; ///< URI fragment.
|
|
|
};
|
|
|
|
|
|
|
|
@@ -72,8 +79,8 @@ typedef struct link_map {
|
|
|
*/
|
|
|
|
|
|
uint32_t LSUP_default_dtype_key = 0;
|
|
|
-regex_t *LSUP_uri_ptn;
|
|
|
LSUP_Term *LSUP_default_datatype = NULL;
|
|
|
+LSUP_TermSet *LSUP_term_cache = NULL;
|
|
|
|
|
|
|
|
|
/*
|
|
@@ -83,6 +90,10 @@ LSUP_Term *LSUP_default_datatype = NULL;
|
|
|
// Characters not allowed in a URI string.
|
|
|
static const char *invalid_uri_chars = "<>\" {}|\\^`";
|
|
|
|
|
|
+/// Minimum valid type code.
|
|
|
+static const LSUP_TermType MIN_VALID_TYPE = LSUP_TERM_IRIREF;
|
|
|
+/// Maximum valid type code. Change this if adding to enum LSUP_TermType.
|
|
|
+static const LSUP_TermType MAX_VALID_TYPE = LSUP_TERM_BNODE;
|
|
|
|
|
|
/*
|
|
|
* Static prototypes.
|
|
@@ -146,6 +157,9 @@ link_map_free_fn (void *item)
|
|
|
}
|
|
|
|
|
|
|
|
|
+static LSUP_rc parse_iri (char *iri, MatchCoord coords[]);
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
* Term API.
|
|
|
*/
|
|
@@ -235,9 +249,11 @@ LSUP_iriref_absolute (const LSUP_Term *root, const LSUP_Term *iri)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- char *data, *pfx = LSUP_iriref_prefix (iri);
|
|
|
+ char
|
|
|
+ *data,
|
|
|
+ *pfx = LSUP_iriref_prefix (iri);
|
|
|
|
|
|
- if (pfx) data = iri->data;
|
|
|
+ if (strlen (pfx) > 0) data = iri->data;
|
|
|
|
|
|
else if (iri->data[0] == '/') {
|
|
|
free (pfx);
|
|
@@ -392,12 +408,11 @@ LSUP_iriref_prefix (const LSUP_Term *iri)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- if (iri->iri_info->prefix.rm_so == -1) return NULL;
|
|
|
-
|
|
|
- size_t len = iri->iri_info->prefix.rm_eo - iri->iri_info->prefix.rm_so;
|
|
|
- if (len == 0) return NULL;
|
|
|
+ // if (iri->iri_info->prefix.size == 0) return NULL;
|
|
|
|
|
|
- return strndup (iri->data + iri->iri_info->prefix.rm_so, len);
|
|
|
+ return strndup (
|
|
|
+ iri->data + iri->iri_info->prefix.offset,
|
|
|
+ iri->iri_info->prefix.size);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -409,12 +424,11 @@ LSUP_iriref_path (const LSUP_Term *iri)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- if (iri->iri_info->path.rm_so == -1) return NULL;
|
|
|
-
|
|
|
- size_t len = iri->iri_info->path.rm_eo - iri->iri_info->path.rm_so;
|
|
|
- if (len == 0) return NULL;
|
|
|
+ // if (iri->iri_info->path.size == 0) return NULL;
|
|
|
|
|
|
- return strndup (iri->data + iri->iri_info->path.rm_so, len);
|
|
|
+ return strndup (
|
|
|
+ iri->data + iri->iri_info->path.offset,
|
|
|
+ iri->iri_info->path.size);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -426,11 +440,11 @@ LSUP_iriref_frag (const LSUP_Term *iri)
|
|
|
return NULL;
|
|
|
}
|
|
|
|
|
|
- if (iri->iri_info->frag.rm_so == -1) return NULL;
|
|
|
+ // if (iri->iri_info->frag.size == 0) return NULL;
|
|
|
|
|
|
- size_t len = iri->iri_info->frag.rm_eo - iri->iri_info->frag.rm_so;
|
|
|
-
|
|
|
- return strndup (iri->data + iri->iri_info->frag.rm_so, len);
|
|
|
+ return strndup (
|
|
|
+ iri->data + iri->iri_info->frag.offset,
|
|
|
+ iri->iri_info->frag.size);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -654,7 +668,7 @@ LSUP_link_map_add (
|
|
|
KeyedTerm *ins;
|
|
|
MALLOC_GUARD (ins, LSUP_MEM_ERR);
|
|
|
memcpy (ins, &entry_s, sizeof (entry_s));
|
|
|
- Link link = {.term=ins, tset=tset};
|
|
|
+ Link link = {.term=ins, .tset=tset};
|
|
|
hashmap_set (cmap->links, &link);
|
|
|
}
|
|
|
|
|
@@ -740,12 +754,8 @@ term_init (
|
|
|
LSUP_Term *term, LSUP_TermType type,
|
|
|
const char *data, void *metadata)
|
|
|
{
|
|
|
- if (UNLIKELY (!LSUP_uri_ptn)) {
|
|
|
- log_error ("Environment not initialized. Did you call LSUP_init()?");
|
|
|
- return LSUP_ERROR;
|
|
|
- }
|
|
|
// This can never be LSUP_TERM_UNDEFINED.
|
|
|
- if (type == LSUP_TERM_UNDEFINED) {
|
|
|
+ if (type < MIN_VALID_TYPE || type > MAX_VALID_TYPE) {
|
|
|
log_error ("%d is not a valid term type.", type);
|
|
|
return LSUP_VALUE_ERR;
|
|
|
}
|
|
@@ -778,9 +788,9 @@ term_init (
|
|
|
}
|
|
|
|
|
|
// Capture interesting IRI parts.
|
|
|
- regmatch_t matches[11];
|
|
|
- if (UNLIKELY (regexec (LSUP_uri_ptn, fquri, 11, matches, 0) != 0)) {
|
|
|
- fprintf (stderr, "Error matching URI pattern.\n");
|
|
|
+ MatchCoord matches[7] = {}; // Initialize all to 0.
|
|
|
+ if (UNLIKELY (parse_iri (fquri, matches) != LSUP_OK)) {
|
|
|
+ log_error ("Error matching URI pattern.");
|
|
|
|
|
|
return LSUP_VALUE_ERR;
|
|
|
}
|
|
@@ -789,8 +799,8 @@ term_init (
|
|
|
MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
|
|
|
|
|
|
term->iri_info->prefix = matches[1];
|
|
|
- term->iri_info->path = matches[5];
|
|
|
- term->iri_info->frag = matches[10];
|
|
|
+ term->iri_info->path = matches[4];
|
|
|
+ term->iri_info->frag = matches[6];
|
|
|
term->iri_info->nsm = metadata;
|
|
|
}
|
|
|
|
|
@@ -813,12 +823,12 @@ term_init (
|
|
|
MALLOC_GUARD (term->iri_info, LSUP_MEM_ERR);
|
|
|
|
|
|
// Allocate IRI match patterns manually.
|
|
|
- term->iri_info->prefix.rm_so = 0;
|
|
|
- term->iri_info->prefix.rm_eo = 4;
|
|
|
- term->iri_info->path.rm_so = 4;
|
|
|
- term->iri_info->path.rm_eo = UUIDSTR_SIZE + 6;
|
|
|
- term->iri_info->frag.rm_so = -1;
|
|
|
- term->iri_info->frag.rm_eo = -1;
|
|
|
+ term->iri_info->prefix.offset = 0;
|
|
|
+ term->iri_info->prefix.size = 4;
|
|
|
+ term->iri_info->path.offset = 4;
|
|
|
+ term->iri_info->path.size = UUIDSTR_SIZE + 6;
|
|
|
+ term->iri_info->frag.offset = 0;
|
|
|
+ term->iri_info->frag.size = 0;
|
|
|
term->iri_info->nsm = NULL;
|
|
|
|
|
|
} else term->data = strdup (uuid_str);
|
|
@@ -881,6 +891,134 @@ term_init (
|
|
|
}
|
|
|
|
|
|
|
|
|
+/**
|
|
|
+ * @brief scan an IRI string and parse IRI parts.
|
|
|
+ *
|
|
|
+ * Experimental replacement of a regex engine for better performance.
|
|
|
+ *
|
|
|
+ * Slightly adapted from regex on
|
|
|
+ * https://datatracker.ietf.org/doc/html/rfc3986#appendix-B to capture relevant
|
|
|
+ * parts of the IRI.
|
|
|
+ *
|
|
|
+ * Reference regex and group numbering:
|
|
|
+ * ^((?([^:/?#]+):)?(?//([^/?#]*))?)((?[^?#]*)(?\?([^#]*))?(?#(.*))?)
|
|
|
+ * 1 2 3 4 5 6
|
|
|
+ *
|
|
|
+ * Capturing groups:
|
|
|
+ *
|
|
|
+ * #0: Full parsed URI (http://example.org/123/456/?query=blah#frag)
|
|
|
+ * #1: Prefix (http://example.org)
|
|
|
+ * #2: Scheme (http)
|
|
|
+ * #3: Authority (example.org)
|
|
|
+ * #4: Path, including query and fragment (/123/456/?query=blah#frag)
|
|
|
+ * #5: Query (query=blah)
|
|
|
+ * #6: Fragment (frag)
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param iri_str[in] IRI string to parse.
|
|
|
+ *
|
|
|
+ * @param match_coord_t[out] coord Coordinates to be stored. This must be a
|
|
|
+ * pre-allocated array of at least 7 elements.
|
|
|
+ *
|
|
|
+ * The first size_t of each element stores the relative position of a match,
|
|
|
+ * and the second one stores the length of the match. A length of 0 indicates
|
|
|
+ * no match.
|
|
|
+ */
|
|
|
+static LSUP_rc
|
|
|
+parse_iri (char *iri_str, MatchCoord coord[]) {
|
|
|
+ char *cur = iri_str;
|
|
|
+ size_t iri_len = strlen (iri_str);
|
|
|
+ MatchCoord tmp = {}; // Temporary storage for capture groups
|
|
|
+
|
|
|
+ // Redundant if only called by term_init.
|
|
|
+ // memset (coord, 0, sizeof(*coord));
|
|
|
+
|
|
|
+ //log_debug ("Parsing IRI: %s", iri_str);
|
|
|
+ // #2: ([^:/?#]+)
|
|
|
+ while (
|
|
|
+ *cur != ':' && *cur != '/' && *cur != '?'
|
|
|
+ && *cur != '#' && *cur != '\0') {
|
|
|
+ tmp.size++;
|
|
|
+ cur++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Non-capturing: (?([^:/?#]+):)?
|
|
|
+ if (tmp.size > 0 && *cur == ':') {
|
|
|
+ // Got capture groups #2 and #3. Store them.
|
|
|
+ coord[2].offset = 0;
|
|
|
+ coord[2].size = tmp.size;
|
|
|
+ cur++;
|
|
|
+ //log_debug ("Group #2: %lu, %lu", coord[2].offset, coord[2].size);
|
|
|
+ } else cur = iri_str; // Backtrack if no match.
|
|
|
+
|
|
|
+ // Non-capturing: (?//([^/?#]*))?
|
|
|
+ if (*cur == '/' && *(cur + 1) == '/') {
|
|
|
+ cur += 2;
|
|
|
+ tmp.offset = cur - iri_str;
|
|
|
+ tmp.size = 0;
|
|
|
+
|
|
|
+ // #3: ([^/?#]*)
|
|
|
+ while (*cur != '/' && *cur != '?' && *cur != '#' && *cur != '\0') {
|
|
|
+ tmp.size++;
|
|
|
+ cur++;
|
|
|
+ }
|
|
|
+ coord[3].offset = tmp.offset;
|
|
|
+ coord[3].size = tmp.size;
|
|
|
+ //log_debug ("Group #3: %lu, %lu", coord[3].offset, coord[3].size);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Capture group 1.
|
|
|
+ coord[1].offset = 0;
|
|
|
+ coord[1].size = cur - iri_str;
|
|
|
+ //log_debug ("Group #1: %lu, %lu", coord[1].offset, coord[1].size);
|
|
|
+
|
|
|
+ tmp.offset = cur - iri_str;
|
|
|
+ tmp.size = 0;
|
|
|
+
|
|
|
+ coord[4].offset = tmp.offset;
|
|
|
+ coord[4].size = iri_len - tmp.offset;
|
|
|
+ //log_debug ("Group #4: %lu, %lu", coord[4].offset, coord[4].size);
|
|
|
+
|
|
|
+ // Non-capturing: (?[^?#]*)
|
|
|
+ while (*cur != '?' && *cur != '#' && *cur != '\0') {
|
|
|
+ tmp.size++;
|
|
|
+ cur++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Non-capturing: (?\?([^#]*))
|
|
|
+ if (*cur == '?') {
|
|
|
+ // 5: ([^#]*)
|
|
|
+ tmp.offset = ++cur - iri_str;
|
|
|
+ tmp.size = 0;
|
|
|
+ while (*cur != '#' && *cur != '\0') {
|
|
|
+ tmp.size++;
|
|
|
+ cur++;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tmp.size > 0) {
|
|
|
+ // Got capture group #5.
|
|
|
+ coord[5].offset = tmp.offset;
|
|
|
+ coord[5].size = tmp.size;
|
|
|
+ //log_debug ("Group #5: %lu, %lu", coord[5].offset, coord[5].size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Non-capturing: (?#(.*))?
|
|
|
+ if (*cur == '#') {
|
|
|
+ // #6: (.*)
|
|
|
+ coord[6].offset = ++cur - iri_str;
|
|
|
+ coord[6].size = iri_str + iri_len - cur;
|
|
|
+ //log_debug ("Group #6: %lu, %lu", coord[6].offset, coord[6].size);
|
|
|
+ }
|
|
|
+
|
|
|
+ coord[0].offset = 0;
|
|
|
+ coord[0].size = iri_len;
|
|
|
+ //log_debug ("Full match: %lu, %lu", coord[0].offset, coord[0].size);
|
|
|
+
|
|
|
+ return LSUP_OK;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
* Extern inline functions.
|
|
|
*/
|