aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/env.c14
-rw-r--r--src/node.c209
-rw-r--r--src/node.h2
-rw-r--r--src/serdi.c6
-rw-r--r--src/world.c2
5 files changed, 153 insertions, 80 deletions
diff --git a/src/env.c b/src/env.c
index 2cb180d0..0a1a5ffa 100644
--- a/src/env.c
+++ b/src/env.c
@@ -80,7 +80,7 @@ serd_env_set_base_uri(SerdEnv* env,
}
// Resolve base URI and create a new node and URI for it
- SerdNode* base_uri_node = serd_node_new_resolved_uri_i(
+ SerdNode* base_uri_node = serd_new_resolved_uri_i(
serd_node_get_string(uri), &env->base_uri);
SerdURI base_uri;
@@ -145,7 +145,7 @@ serd_env_set_prefix(SerdEnv* env,
serd_env_add(env, name, uri);
} else {
// Resolve relative URI and create a new node and URI for it
- SerdNode* abs_uri = serd_node_new_resolved_uri_i(
+ SerdNode* abs_uri = serd_new_resolved_uri_i(
serd_node_get_string(uri), &env->base_uri);
// Set prefix to resolved (absolute) URI
@@ -160,8 +160,8 @@ serd_env_set_prefix_from_strings(SerdEnv* env,
const char* name,
const char* uri)
{
- SerdNode* name_node = serd_node_new_string(name);
- SerdNode* uri_node = serd_node_new_uri(uri);
+ SerdNode* name_node = serd_new_string(name);
+ SerdNode* uri_node = serd_new_uri(uri);
const SerdStatus st = serd_env_set_prefix(env, name_node, uri_node);
@@ -240,15 +240,15 @@ serd_env_expand_node(const SerdEnv* env,
SerdNode* datatype = serd_env_expand_node(
env, serd_node_get_datatype(node));
if (datatype) {
- SerdNode* ret = serd_node_new_literal(
- serd_node_get_string(node), datatype, NULL);
+ SerdNode* ret = serd_new_typed_literal(
+ serd_node_get_string(node), datatype);
serd_node_free(datatype);
return ret;
}
}
return NULL;
case SERD_URI:
- return serd_node_new_resolved_uri_i(
+ return serd_new_resolved_uri_i(
serd_node_get_string(node), &env->base_uri);
default:
return NULL;
diff --git a/src/node.c b/src/node.c
index afc1ed87..a5baa1ad 100644
--- a/src/node.c
+++ b/src/node.c
@@ -42,7 +42,7 @@
static const size_t serd_node_align = sizeof(SerdNode);
static SerdNode*
-serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base);
+serd_new_from_uri(const SerdURI* uri, const SerdURI* base);
static size_t
serd_node_pad_size(const size_t n_bytes)
@@ -126,7 +126,7 @@ serd_node_set(SerdNode** dst, const SerdNode* src)
}
SerdNode*
-serd_node_new_simple_node(SerdType type, const char* str, const size_t len)
+serd_new_simple_node(SerdType type, const char* str, const size_t len)
{
if (!str) {
return NULL;
@@ -142,7 +142,7 @@ serd_node_new_simple_node(SerdType type, const char* str, const size_t len)
}
SerdNode*
-serd_node_new_string(const char* str)
+serd_new_string(const char* str)
{
if (!str) {
return NULL;
@@ -158,7 +158,7 @@ serd_node_new_string(const char* str)
}
SerdNode*
-serd_node_new_substring(const char* str, const size_t len)
+serd_new_substring(const char* str, const size_t len)
{
if (!str) {
return NULL;
@@ -172,74 +172,147 @@ serd_node_new_substring(const char* str, const size_t len)
return node;
}
-SerdNode*
-serd_node_new_literal(const char* str,
- const SerdNode* datatype,
- const char* lang)
+/// Internal pre-measured implementation of serd_new_plain_literal
+static SerdNode*
+serd_new_plain_literal_i(const char* str,
+ const size_t str_len,
+ SerdNodeFlags flags,
+ const char* lang,
+ const size_t lang_len)
{
- serd_node_check_padding(datatype);
+ assert(str);
+ assert(lang);
+
+ flags |= SERD_HAS_LANGUAGE;
+
+ const size_t len = serd_node_pad_size(str_len);
+ const size_t total_len = len + sizeof(SerdNode) + lang_len;
+
+ SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str, str_len);
+ node->n_bytes = str_len;
+
+ SerdNode* lang_node = node + 1 + (len / serd_node_align);
+ lang_node->type = SERD_LITERAL;
+ lang_node->n_bytes = lang_len;
+ memcpy(serd_node_buffer(lang_node), lang, lang_len);
+ serd_node_check_padding(lang_node);
- if (!str || (lang && datatype &&
- strcmp(serd_node_buffer_c(datatype), NS_RDF "langString")) ||
- (datatype && serd_node_get_type(datatype) != SERD_URI)) {
+ serd_node_check_padding(node);
+ return node;
+}
+
+/// Internal pre-measured implementation of serd_new_typed_literal
+static SerdNode*
+serd_new_typed_literal_i(const char* str,
+ const size_t str_len,
+ SerdNodeFlags flags,
+ const char* datatype_uri,
+ const size_t datatype_uri_len)
+{
+ assert(str);
+ assert(datatype_uri);
+ assert(strcmp(datatype_uri, NS_RDF "langString"));
+
+ flags |= SERD_HAS_DATATYPE;
+
+ const size_t len = serd_node_pad_size(str_len);
+ const size_t total_len = len + sizeof(SerdNode) + datatype_uri_len;
+
+ SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str, str_len);
+ node->n_bytes = str_len;
+
+ SerdNode* datatype_node = node + 1 + (len / serd_node_align);
+ datatype_node->n_bytes = datatype_uri_len;
+ datatype_node->type = SERD_URI;
+ memcpy(serd_node_buffer(datatype_node), datatype_uri, datatype_uri_len);
+ serd_node_check_padding(datatype_node);
+
+ serd_node_check_padding(node);
+ return node;
+}
+
+SerdNode*
+serd_new_literal(const char* str,
+ const size_t str_len,
+ const char* datatype_uri,
+ const size_t datatype_uri_len,
+ const char* lang,
+ const size_t lang_len)
+{
+ if (!str ||
+ (lang && datatype_uri && strcmp(datatype_uri, NS_RDF "langString"))) {
return NULL;
}
- SerdNodeFlags flags = 0;
- const size_t n_bytes = serd_strlen(str, &flags);
- const size_t len = serd_node_pad_size(n_bytes);
+ SerdNodeFlags flags = 0;
+ serd_substrlen(str, str_len, &flags);
- SerdNode* node = NULL;
if (lang) {
- flags |= SERD_HAS_LANGUAGE;
- const size_t lang_len = strlen(lang);
- const size_t total_len = len + sizeof(SerdNode) + lang_len;
- node = serd_node_malloc(total_len, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
-
- SerdNode* lang_node = node + 1 + (len / serd_node_align);
- lang_node->type = SERD_LITERAL;
- lang_node->n_bytes = lang_len;
- memcpy(serd_node_buffer(lang_node), lang, lang_len);
- serd_node_check_padding(lang_node);
- } else if (datatype) {
- flags |= SERD_HAS_DATATYPE;
- const size_t datatype_len = strlen(serd_node_buffer_c(datatype));
- const size_t total_len = len + sizeof(SerdNode) + datatype_len;
- node = serd_node_malloc(total_len, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
-
- SerdNode* datatype_node = node + 1 + (len / serd_node_align);
- memcpy(datatype_node, datatype, sizeof(SerdNode) + datatype_len);
- serd_node_check_padding(datatype_node);
+ return serd_new_plain_literal_i(str, str_len, flags, lang, lang_len);
+ } else if (datatype_uri) {
+ return serd_new_typed_literal_i(
+ str, str_len, flags, datatype_uri, datatype_uri_len);
} else {
- node = serd_node_malloc(n_bytes, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str, n_bytes);
- node->n_bytes = n_bytes;
+ return serd_new_substring(str, str_len);
}
+}
- serd_node_check_padding(node);
- return node;
+SerdNode*
+serd_new_plain_literal(const char* str, const char* lang)
+{
+ if (!str) {
+ return NULL;
+ } else if (!lang) {
+ return serd_new_string(str);
+ }
+
+ SerdNodeFlags flags = 0;
+ const size_t str_len = serd_strlen(str, &flags);
+ const size_t lang_len = strlen(lang);
+
+ return serd_new_plain_literal_i(str, str_len, flags, lang, lang_len);
+}
+
+SerdNode*
+serd_new_typed_literal(const char* str, const SerdNode* datatype)
+{
+ if (!str) {
+ return NULL;
+ } else if (!datatype) {
+ return serd_new_string(str);
+ } else if (!strcmp(serd_node_buffer_c(datatype), NS_RDF "langString") ||
+ serd_node_get_type(datatype) != SERD_URI) {
+ return NULL;
+ }
+
+ SerdNodeFlags flags = 0;
+ const size_t str_len = serd_strlen(str, &flags);
+
+ return serd_new_typed_literal_i(str,
+ str_len,
+ flags,
+ serd_node_get_string(datatype),
+ serd_node_get_length(datatype));
}
SerdNode*
-serd_node_new_blank(const char* str)
+serd_new_blank(const char* str)
{
- return str ? serd_node_new_simple_node(SERD_BLANK, str, strlen(str)) : NULL;
+ return str ? serd_new_simple_node(SERD_BLANK, str, strlen(str)) : NULL;
}
SerdNode*
-serd_node_new_curie(const char* str)
+serd_new_curie(const char* str)
{
- return str ? serd_node_new_simple_node(SERD_CURIE, str, strlen(str)) : NULL;
+ return str ? serd_new_simple_node(SERD_CURIE, str, strlen(str)) : NULL;
}
SerdNode*
-serd_node_new_uri(const char* str)
+serd_new_uri(const char* str)
{
- return str ? serd_node_new_simple_node(SERD_URI, str, strlen(str)) : NULL;
+ return str ? serd_new_simple_node(SERD_URI, str, strlen(str)) : NULL;
}
/**
@@ -321,7 +394,7 @@ string_sink(const void* buf, size_t size, size_t nmemb, void* stream)
}
SerdNode*
-serd_node_new_resolved_uri(const char* str, const SerdNode* base)
+serd_new_resolved_uri(const char* str, const SerdNode* base)
{
if (!base || base->type != SERD_URI) {
return NULL;
@@ -329,7 +402,7 @@ serd_node_new_resolved_uri(const char* str, const SerdNode* base)
SerdURI base_uri;
serd_uri_parse(serd_node_get_string(base), &base_uri);
- return serd_node_new_resolved_uri_i(str, &base_uri);
+ return serd_new_resolved_uri_i(str, &base_uri);
}
SerdNode*
@@ -344,20 +417,20 @@ serd_node_resolve(const SerdNode* node, const SerdNode* base)
serd_uri_parse(serd_node_get_string(node), &uri);
serd_uri_parse(serd_node_get_string(base), &base_uri);
- return serd_node_new_from_uri(&uri, &base_uri);
+ return serd_new_from_uri(&uri, &base_uri);
}
SerdNode*
-serd_node_new_resolved_uri_i(const char* str, const SerdURI* base)
+serd_new_resolved_uri_i(const char* str, const SerdURI* base)
{
if (!str || str[0] == '\0') {
// Empty URI => Base URI, or nothing if no base is given
- return base ? serd_node_new_from_uri(base, NULL) : NULL;
+ return base ? serd_new_from_uri(base, NULL) : NULL;
}
SerdURI uri;
serd_uri_parse(str, &uri);
- return serd_node_new_from_uri(&uri, base);
+ return serd_new_from_uri(&uri, base);
}
static inline bool
@@ -380,7 +453,7 @@ is_uri_path_char(const char c)
}
SerdNode*
-serd_node_new_file_uri(const char* path, const char* hostname, bool escape)
+serd_new_file_uri(const char* path, const char* hostname, bool escape)
{
const size_t path_len = strlen(path);
const size_t hostname_len = hostname ? strlen(hostname) : 0;
@@ -411,14 +484,14 @@ serd_node_new_file_uri(const char* path, const char* hostname, bool escape)
}
serd_buffer_sink_finish(&buffer);
- SerdNode* node = serd_node_new_uri((const char*)buffer.buf);
+ SerdNode* node = serd_new_uri((const char*)buffer.buf);
free(buffer.buf);
serd_node_check_padding(node);
return node;
}
static SerdNode*
-serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base)
+serd_new_from_uri(const SerdURI* uri, const SerdURI* base)
{
SerdURI abs_uri = *uri;
if (base) {
@@ -438,9 +511,9 @@ serd_node_new_from_uri(const SerdURI* uri, const SerdURI* base)
}
SerdNode*
-serd_node_new_relative_uri(const char* str,
- const SerdNode* base,
- const SerdNode* root)
+serd_new_relative_uri(const char* str,
+ const SerdNode* base,
+ const SerdNode* root)
{
SerdURI uri = SERD_URI_NULL;
SerdURI base_uri = SERD_URI_NULL;
@@ -476,7 +549,7 @@ serd_digits(double abs)
}
SerdNode*
-serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
+serd_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
{
if (isnan(d) || isinf(d)) {
return NULL;
@@ -539,7 +612,7 @@ serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype)
}
SerdNode*
-serd_node_new_integer(int64_t i, const SerdNode* datatype)
+serd_new_integer(int64_t i, const SerdNode* datatype)
{
const SerdNode* type = datatype ? datatype : &serd_xsd_integer.node;
int64_t abs_i = (i < 0) ? -i : i;
@@ -571,10 +644,10 @@ serd_node_new_integer(int64_t i, const SerdNode* datatype)
}
SerdNode*
-serd_node_new_blob(const void* buf,
- size_t size,
- bool wrap_lines,
- const SerdNode* datatype)
+serd_new_blob(const void* buf,
+ size_t size,
+ bool wrap_lines,
+ const SerdNode* datatype)
{
if (!buf || !size) {
return NULL;
diff --git a/src/node.h b/src/node.h
index 2a37217c..8b662a3c 100644
--- a/src/node.h
+++ b/src/node.h
@@ -42,6 +42,6 @@ serd_node_buffer_c(const SerdNode* node)
SerdNode* serd_node_malloc(size_t n_bytes, SerdNodeFlags flags, SerdType type);
void serd_node_set(SerdNode** dst, const SerdNode* src);
void serd_node_zero_pad(SerdNode* node);
-SerdNode* serd_node_new_resolved_uri_i(const char* str, const SerdURI* base);
+SerdNode* serd_new_resolved_uri_i(const char* str, const SerdURI* base);
#endif // SERD_NODE_H
diff --git a/src/serdi.c b/src/serdi.c
index a7bce9b9..d8d8437f 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -197,9 +197,9 @@ main(int argc, char** argv)
SerdNode* base = NULL;
if (a < argc) { // Base URI given on command line
- base = serd_node_new_uri((const char*)argv[a]);
+ base = serd_new_uri((const char*)argv[a]);
} else if (!from_string && !from_stdin) { // Use input file URI
- base = serd_node_new_file_uri(input, NULL, true);
+ base = serd_new_file_uri(input, NULL, true);
}
FILE* out_fd = stdout;
@@ -241,7 +241,7 @@ main(int argc, char** argv)
serd_world_set_error_sink(world, quiet_error_sink, NULL);
}
- SerdNode* root = serd_node_new_uri(root_uri);
+ SerdNode* root = serd_new_uri(root_uri);
serd_writer_set_root_uri(writer, root);
serd_writer_chop_blank_prefix(writer, chop_prefix);
serd_reader_add_blank_prefix(reader, add_prefix);
diff --git a/src/world.c b/src/world.c
index d503a2ff..54990029 100644
--- a/src/world.c
+++ b/src/world.c
@@ -77,7 +77,7 @@ serd_world_new(void)
{
SerdWorld* world = (SerdWorld*)calloc(1, sizeof(SerdWorld));
- world->blank_node = serd_node_new_blank("b0000000000");
+ world->blank_node = serd_new_blank("b0000000000");
return world;
}