aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/serd/serd.h21
-rw-r--r--src/node.c94
-rw-r--r--test/test_node.c66
-rw-r--r--test/test_reader_writer.c5
-rw-r--r--test/test_writer.c4
5 files changed, 122 insertions, 68 deletions
diff --git a/include/serd/serd.h b/include/serd/serd.h
index 766844be..1d1ae70a 100644
--- a/include/serd/serd.h
+++ b/include/serd/serd.h
@@ -547,16 +547,25 @@ SerdNode* SERD_ALLOCATED
serd_new_string(SerdStringView string);
/**
- Create a new literal node from `str`.
+ Create a new plain literal node from `str` with `lang`.
- Either `datatype_uri` or `lang` can be given, but not both, unless
- `datatype_uri` is rdf:langString in which case it is ignored.
+ A plain literal has no datatype, but may have a language tag. The `lang`
+ may be empty, in which case this is equivalent to `serd_new_string()`.
*/
SERD_API
SerdNode* SERD_ALLOCATED
-serd_new_literal(SerdStringView string,
- SerdStringView datatype_uri,
- SerdStringView lang);
+serd_new_plain_literal(SerdStringView str, SerdStringView lang);
+
+/**
+ Create a new typed literal node from `str`.
+
+ A typed literal has no language tag, but may have a datatype. The
+ `datatype` may be NULL, in which case this is equivalent to
+ `serd_new_string()`.
+*/
+SERD_API
+SerdNode* SERD_ALLOCATED
+serd_new_typed_literal(SerdStringView str, SerdStringView datatype_uri);
/// Create a new blank node
SERD_API
diff --git a/src/node.c b/src/node.c
index f8c4b47f..cf5f7e46 100644
--- a/src/node.c
+++ b/src/node.c
@@ -17,6 +17,7 @@
#include "node.h"
#include "base64.h"
+#include "serd_internal.h"
#include "static_nodes.h"
#include "string_utils.h"
#include "system.h"
@@ -224,48 +225,77 @@ serd_new_string(const SerdStringView str)
return node;
}
-SerdNode*
-serd_new_literal(const SerdStringView str,
- const SerdStringView datatype_uri,
- const SerdStringView lang)
+/// Internal pre-measured implementation of serd_new_plain_literal
+static SerdNode*
+serd_new_plain_literal_i(const SerdStringView str,
+ SerdNodeFlags flags,
+ const SerdStringView lang)
{
- SerdNodeFlags flags = 0;
- const size_t length = serd_substrlen(str.buf, str.len, &flags);
- const size_t len = serd_node_pad_size(length);
+ assert(str.len);
+ assert(lang.len);
- SerdNode* node = NULL;
- if (lang.len) {
- const size_t total_len = len + sizeof(SerdNode) + lang.len;
+ flags |= SERD_HAS_LANGUAGE;
- node = serd_node_malloc(total_len, flags | SERD_HAS_LANGUAGE, SERD_LITERAL);
- node->length = length;
- memcpy(serd_node_buffer(node), str.buf, length);
+ const size_t len = serd_node_pad_size(str.len);
+ const size_t total_len = len + sizeof(SerdNode) + lang.len;
- SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode));
- lang_node->type = SERD_LITERAL;
- lang_node->length = lang.len;
- memcpy(serd_node_buffer(lang_node), lang.buf, lang.len);
- serd_node_check_padding(lang_node);
+ SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL);
+ memcpy(serd_node_buffer(node), str.buf, str.len);
+ node->length = str.len;
- } else if (datatype_uri.len) {
- const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len;
+ SerdNode* lang_node = node + 1 + (len / sizeof(SerdNode));
+ lang_node->type = SERD_LITERAL;
+ lang_node->length = lang.len;
+ memcpy(serd_node_buffer(lang_node), lang.buf, lang.len);
+ serd_node_check_padding(lang_node);
- node = serd_node_malloc(total_len, flags | SERD_HAS_DATATYPE, SERD_LITERAL);
- node->length = length;
- memcpy(serd_node_buffer(node), str.buf, length);
+ serd_node_check_padding(node);
+ return node;
+}
- SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode));
- datatype_node->type = SERD_URI;
- datatype_node->length = datatype_uri.len;
- memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len);
- serd_node_check_padding(datatype_node);
+SerdNode*
+serd_new_plain_literal(const SerdStringView str, const SerdStringView lang)
+{
+ if (!lang.len) {
+ return serd_new_string(str);
+ }
- } else {
- node = serd_node_malloc(length, flags, SERD_LITERAL);
- memcpy(serd_node_buffer(node), str.buf, length);
- node->length = length;
+ SerdNodeFlags flags = 0;
+ serd_strlen(str.buf, &flags);
+
+ return serd_new_plain_literal_i(str, flags, lang);
+}
+
+SerdNode*
+serd_new_typed_literal(const SerdStringView str,
+ const SerdStringView datatype_uri)
+{
+ if (!datatype_uri.len) {
+ return serd_new_string(str);
+ }
+
+ if (!strcmp(datatype_uri.buf, NS_RDF "langString")) {
+ return NULL;
}
+ SerdNodeFlags flags = 0u;
+ serd_strlen(str.buf, &flags);
+
+ 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.buf, str.len);
+ node->length = str.len;
+
+ SerdNode* datatype_node = node + 1 + (len / sizeof(SerdNode));
+ datatype_node->length = datatype_uri.len;
+ datatype_node->type = SERD_URI;
+ memcpy(serd_node_buffer(datatype_node), datatype_uri.buf, datatype_uri.len);
+ serd_node_check_padding(datatype_node);
+
serd_node_check_padding(node);
return node;
}
diff --git a/test/test_node.c b/test/test_node.c
index bd3a5506..095d8755 100644
--- a/test/test_node.c
+++ b/test/test_node.c
@@ -35,6 +35,7 @@
#endif
#define NS_XSD "http://www.w3.org/2001/XMLSchema#"
+#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
static void
test_strtod(double dbl, double max_delta)
@@ -174,12 +175,13 @@ test_node_equals(void)
{
static const uint8_t replacement_char_str[] = {0xEF, 0xBF, 0xBD, 0};
- SerdNode* const lhs =
- serd_new_string(SERD_SUBSTRING((const char*)replacement_char_str, 3));
+ static const SerdStringView replacement_char = {
+ (const char*)replacement_char_str, 3};
- assert(serd_node_equals(lhs, lhs));
+ SerdNode* lhs = serd_new_string(replacement_char);
+ SerdNode* rhs = serd_new_string(SERD_STRING("123"));
- SerdNode* const rhs = serd_new_string(SERD_STRING("123"));
+ assert(serd_node_equals(lhs, lhs));
assert(!serd_node_equals(lhs, rhs));
SerdNode* const qnode = serd_new_curie(SERD_STRING("foo:bar"));
@@ -232,42 +234,58 @@ check_copy_equals(const SerdNode* const node)
static void
test_literal(void)
{
- SerdNode* hello2 = serd_new_literal(
- SERD_STRING("hello\""), SERD_EMPTY_STRING(), SERD_EMPTY_STRING());
+ SerdNode* hello2 = serd_new_string(SERD_STRING("hello\""));
assert(serd_node_length(hello2) == 6 &&
serd_node_flags(hello2) == SERD_HAS_QUOTE &&
!strcmp(serd_node_string(hello2), "hello\""));
+
check_copy_equals(hello2);
+
+ SerdNode* hello3 =
+ serd_new_plain_literal(SERD_STRING("hello\""), SERD_EMPTY_STRING());
+
+ assert(serd_node_equals(hello2, hello3));
+
+ SerdNode* hello4 =
+ serd_new_typed_literal(SERD_STRING("hello\""), SERD_EMPTY_STRING());
+
+ assert(!serd_new_typed_literal(SERD_STRING("plain"),
+ SERD_STRING(NS_RDF "langString")));
+
+ assert(serd_node_equals(hello4, hello2));
+
+ serd_node_free(hello4);
+ serd_node_free(hello3);
serd_node_free(hello2);
- SerdNode* hello_l = serd_new_literal(
- SERD_STRING("hello_l\""), SERD_EMPTY_STRING(), SERD_STRING("en"));
+ const char* lang_lit_str = "\"Hello\"@en";
+ SerdNode* sliced_lang_lit = serd_new_plain_literal(
+ SERD_SUBSTRING(lang_lit_str + 1, 5), SERD_SUBSTRING(lang_lit_str + 8, 2));
- assert(serd_node_length(hello_l) == 8);
- assert(!strcmp(serd_node_string(hello_l), "hello_l\""));
- assert(serd_node_flags(hello_l) == (SERD_HAS_QUOTE | SERD_HAS_LANGUAGE));
+ assert(!strcmp(serd_node_string(sliced_lang_lit), "Hello"));
- const SerdNode* const lang = serd_node_language(hello_l);
+ const SerdNode* const lang = serd_node_language(sliced_lang_lit);
assert(lang);
assert(!strcmp(serd_node_string(lang), "en"));
- check_copy_equals(hello_l);
- serd_node_free(hello_l);
+ check_copy_equals(sliced_lang_lit);
+ serd_node_free(sliced_lang_lit);
- SerdNode* hello_dt = serd_new_literal(SERD_STRING("hello_dt\""),
- SERD_STRING("http://example.org/Thing"),
- SERD_EMPTY_STRING());
+ const char* type_lit_str = "\"Hallo\"^^<http://example.org/Greeting>";
+ SerdNode* sliced_type_lit = serd_new_typed_literal(
+ SERD_SUBSTRING(type_lit_str + 1, 5), SERD_SUBSTRING(type_lit_str + 10, 27));
- assert(serd_node_length(hello_dt) == 9);
- assert(!strcmp(serd_node_string(hello_dt), "hello_dt\""));
- assert(serd_node_flags(hello_dt) == (SERD_HAS_QUOTE | SERD_HAS_DATATYPE));
+ assert(!strcmp(serd_node_string(sliced_type_lit), "Hallo"));
- const SerdNode* const datatype = serd_node_datatype(hello_dt);
+ const SerdNode* const datatype = serd_node_datatype(sliced_type_lit);
assert(datatype);
- assert(!strcmp(serd_node_string(datatype), "http://example.org/Thing"));
+ assert(!strcmp(serd_node_string(datatype), "http://example.org/Greeting"));
+ serd_node_free(sliced_type_lit);
- check_copy_equals(hello_dt);
- serd_node_free(hello_dt);
+ SerdNode* const plain_lit =
+ serd_new_plain_literal(SERD_STRING("Plain"), SERD_EMPTY_STRING());
+ assert(!strcmp(serd_node_string(plain_lit), "Plain"));
+ serd_node_free(plain_lit);
}
static void
diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c
index 62b77f1b..730e5bf0 100644
--- a/test/test_reader_writer.c
+++ b/test/test_reader_writer.c
@@ -216,12 +216,11 @@ test_writer(const char* const path)
assert(serd_sink_write(iface, 0, junk[i][0], junk[i][1], junk[i][2], NULL));
}
- static const SerdStringView empty = SERD_EMPTY_STRING();
static const SerdStringView urn_Type = SERD_STRING("urn:Type");
static const SerdStringView en = SERD_STRING("en");
- SerdNode* const t = serd_new_literal(buf_view, urn_Type, empty);
- SerdNode* const l = serd_new_literal(buf_view, empty, en);
+ SerdNode* const t = serd_new_typed_literal(buf_view, urn_Type);
+ SerdNode* const l = serd_new_plain_literal(buf_view, en);
const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}};
for (size_t i = 0; i < sizeof(good) / (sizeof(SerdNode*) * 3); ++i) {
diff --git a/test/test_writer.c b/test/test_writer.c
index 19aaf4b3..be90b09e 100644
--- a/test/test_writer.c
+++ b/test/test_writer.c
@@ -61,9 +61,7 @@ test_write_long_literal(void)
SerdNode* s = serd_new_uri(SERD_STRING("http://example.org/s"));
SerdNode* p = serd_new_uri(SERD_STRING("http://example.org/p"));
- SerdNode* o = serd_new_literal(SERD_STRING("hello \"\"\"world\"\"\"!"),
- SERD_EMPTY_STRING(),
- SERD_EMPTY_STRING());
+ SerdNode* o = serd_new_string(SERD_STRING("hello \"\"\"world\"\"\"!"));
assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL));