diff options
-rw-r--r-- | NEWS | 3 | ||||
-rw-r--r-- | include/serd/serd.h | 95 | ||||
-rw-r--r-- | src/n3.c | 5 | ||||
-rw-r--r-- | src/node.c | 141 | ||||
-rw-r--r-- | src/nodes.c | 19 | ||||
-rw-r--r-- | src/read_ntriples.c | 22 | ||||
-rw-r--r-- | src/string.c | 51 | ||||
-rw-r--r-- | src/string_utils.h | 5 | ||||
-rw-r--r-- | src/writer.c | 3 | ||||
-rw-r--r-- | test/test_env.c | 6 | ||||
-rw-r--r-- | test/test_node.c | 109 | ||||
-rw-r--r-- | test/test_node_syntax.c | 5 | ||||
-rw-r--r-- | test/test_nodes.c | 4 | ||||
-rw-r--r-- | test/test_reader_writer.c | 4 | ||||
-rw-r--r-- | test/test_string.c | 13 | ||||
-rw-r--r-- | test/test_writer.c | 8 |
16 files changed, 209 insertions, 284 deletions
@@ -10,6 +10,7 @@ serd (1.0.1) unstable; * Bring read/write interface closer to C standard * Improve error handling * Make nodes opaque + * Preserve long or short quoting from input documents * Remove serd_uri_to_path() * Remove support for Turtle named inline nodes extension * Remove useless character counting from API @@ -19,7 +20,7 @@ serd (1.0.1) unstable; * Use a fixed-size reader stack * Use char* for strings in public API - -- David Robillard <d@drobilla.net> Wed, 13 Jan 2021 13:37:55 +0000 + -- David Robillard <d@drobilla.net> Sat, 17 Jul 2021 18:19:48 +0000 serd (0.30.11) unstable; diff --git a/include/serd/serd.h b/include/serd/serd.h index 9ffb16fe..ded613a9 100644 --- a/include/serd/serd.h +++ b/include/serd/serd.h @@ -87,12 +87,11 @@ extern "C" { @{ */ -/// Flags indicating certain string properties relevant to serialisation +/// Flags that describe the details of a node typedef enum { - SERD_HAS_NEWLINE = 1u << 0u, ///< Contains line breaks ('\\n' or '\\r') - SERD_HAS_QUOTE = 1u << 1u, ///< Contains quotes ('"') - SERD_HAS_DATATYPE = 1u << 2u, ///< Literal node has datatype - SERD_HAS_LANGUAGE = 1u << 3u ///< Literal node has language + SERD_IS_LONG = 1u << 0u, ///< Literal node should be triple-quoted + SERD_HAS_DATATYPE = 1u << 1u, ///< Literal node has datatype + SERD_HAS_LANGUAGE = 1u << 2u ///< Literal node has language } SerdNodeFlag; /// Bitwise OR of SerdNodeFlag values @@ -255,17 +254,6 @@ serd_strerror(SerdStatus status); */ /** - Measure a UTF-8 string. - - @return Length of `str` in bytes. - @param str A null-terminated UTF-8 string. - @param flags (Output) Set to the applicable flags. -*/ -SERD_API -size_t -serd_strlen(const char* SERD_NONNULL str, SerdNodeFlags* SERD_NULLABLE flags); - -/** Return `path` as a canonical absolute path. This expands all symbolic links, relative references, and removes extra @@ -614,10 +602,9 @@ typedef enum { /** Create a new "simple" node that is just a string. - This can be used to create blank, CURIE, or URI nodes from an already - measured string or slice of a buffer, which avoids a strlen compared to the - friendly constructors. This may not be used for literals since those must - be measured to set the SERD_HAS_NEWLINE and SERD_HAS_QUOTE flags. + A "simple" node has no associated language or datatype. This can be used to + efficiently create nodes from an already measured string or slice of a + buffer, which avoids a strlen compared to the friendly constructors. */ SERD_API SerdNode* SERD_ALLOCATED @@ -629,25 +616,31 @@ SerdNode* SERD_ALLOCATED serd_new_string(SerdStringView string); /** - Create a new plain literal node from `str` with `lang`. + Create a new literal node with optional datatype or language. - 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_plain_literal(SerdStringView str, SerdStringView lang); + This can create more complex literals than serd_new_string() with an + associated datatype URI or language tag, as well as control whether a + literal should be written as a short or long (triple-quoted) string. -/** - Create a new typed literal node from `str`. + @param string The string value of the literal. - 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()`. + @param flags Flags to describe the literal and its metadata. This must be a + valid combination of flags, in particular, at most one of #SERD_HAS_DATATYPE + and #SERD_HAS_LANGUAGE may be set. + + @param meta The string value of the literal's metadata. If + #SERD_HAS_DATATYPE is set, then this must be an absolute datatype URI. If + #SERD_HAS_LANGUAGE is set, then this must be a language tag like "en-ca". + Otherwise, it is ignored. + + @return A newly allocated literal node that must be freed with + serd_node_free(), or null if the arguments are invalid or allocation failed. */ SERD_API SerdNode* SERD_ALLOCATED -serd_new_typed_literal(SerdStringView str, SerdStringView datatype_uri); +serd_new_literal(SerdStringView string, + SerdNodeFlags flags, + SerdStringView meta); /// Create a new blank node SERD_API @@ -989,31 +982,33 @@ const SerdNode* SERD_ALLOCATED serd_nodes_string(SerdNodes* SERD_NONNULL nodes, SerdStringView string); /** - Return a plain literal node with an optional language. + Make a literal node with optional datatype or language. - If the language is empty, then this is equivalent to serd_nodes_string(). + This can create complex literals with an associated datatype URI or language + tag, and control whether a literal should be written as a short or long + (triple-quoted) string. - A new node will be added if an equivalent node is not already in the set. -*/ -SERD_API -const SerdNode* SERD_ALLOCATED -serd_nodes_plain_literal(SerdNodes* SERD_NONNULL nodes, - SerdStringView string, - SerdStringView language); + @param nodes The node set to get this literal from. -/** - Return a typed literal node with an datatype URI. + @param string The string value of the literal. - If the datatype URI is empty, then this is equivalent to - serd_nodes_string(). + @param flags Flags to describe the literal and its metadata. Note that at + most one of #SERD_HAS_DATATYPE and #SERD_HAS_LANGUAGE may be set. - A new node will be added if an equivalent node is not already in the set. + @param meta The string value of the literal's metadata. If + #SERD_HAS_DATATYPE is set, then this must be an absolute datatype URI. If + #SERD_HAS_LANGUAGE is set, then this must be an RFC 5646 language tag like + "en-ca". Otherwise, it is ignored. + + @return A newly allocated literal node that must be freed with + serd_node_free(), or null if the arguments are invalid or allocation failed. */ SERD_API const SerdNode* SERD_ALLOCATED -serd_nodes_typed_literal(SerdNodes* SERD_NONNULL nodes, - SerdStringView string, - SerdStringView datatype_uri); +serd_nodes_literal(SerdNodes* SERD_NONNULL nodes, + SerdStringView string, + SerdNodeFlags flags, + SerdStringView meta); /** Make a URI node. @@ -114,7 +114,7 @@ read_STRING_LITERAL_LONG(SerdReader* const reader, eat_byte_safe(reader, q3); break; } - ref->flags |= SERD_HAS_QUOTE; + if (!(st = push_byte(reader, ref, c))) { st = read_character(reader, ref, (uint8_t)q2); } @@ -151,7 +151,10 @@ read_String(SerdReader* const reader, SerdNode* const node) return SERD_SUCCESS; } + // Long string eat_byte_safe(reader, q3); + node->flags |= SERD_IS_LONG; + return read_STRING_LITERAL_LONG(reader, node, (uint8_t)q1); } @@ -173,8 +173,8 @@ serd_new_simple_node(const SerdNodeType type, const SerdStringView str) return NULL; } - SerdNodeFlags flags = 0; - const size_t length = str.buf ? serd_strlen(str.buf, &flags) : 0; + SerdNodeFlags flags = 0u; + const size_t length = str.buf ? str.len : 0u; SerdNode* node = serd_node_malloc(length, flags, type); if (node) { @@ -193,87 +193,93 @@ serd_new_simple_node(const SerdNodeType type, const SerdStringView str) SerdNode* serd_new_string(const SerdStringView str) { - SerdNodeFlags flags = 0; - const size_t length = serd_substrlen(str.buf, str.len, &flags); - SerdNode* node = serd_node_malloc(length, flags, SERD_LITERAL); - - memcpy(serd_node_buffer(node), str.buf, str.len); - node->length = length; - - serd_node_check_padding(node); - return node; -} - -/// Internal pre-measured implementation of serd_new_plain_literal -static SerdNode* -serd_new_plain_literal_i(const SerdStringView str, - SerdNodeFlags flags, - const SerdStringView lang) -{ - assert(str.len); - assert(lang.len); - - flags |= SERD_HAS_LANGUAGE; - - const size_t len = serd_node_pad_length(str.len); - const size_t total_len = len + sizeof(SerdNode) + lang.len; + SerdNodeFlags flags = 0u; + SerdNode* node = serd_node_malloc(str.len, flags, SERD_LITERAL); - SerdNode* node = serd_node_malloc(total_len, flags, SERD_LITERAL); - memcpy(serd_node_buffer(node), str.buf, str.len); - node->length = str.len; + if (node) { + if (str.buf && str.len) { + memcpy(serd_node_buffer(node), str.buf, str.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->length = str.len; + serd_node_check_padding(node); + } - serd_node_check_padding(node); return node; } -SerdNode* -serd_new_plain_literal(const SerdStringView str, const SerdStringView lang) +SERD_PURE_FUNC +static bool +is_langtag(const SerdStringView string) { - if (!lang.len) { - return serd_new_string(str); + // First character must be a letter + size_t i = 0; + if (!string.len || !is_alpha(string.buf[i])) { + return false; } - SerdNodeFlags flags = 0; - serd_strlen(str.buf, &flags); + // First component must be all letters + while (++i < string.len && string.buf[i] && string.buf[i] != '-') { + if (!is_alpha(string.buf[i])) { + return false; + } + } + + // Following components can have letters and digits + while (i < string.len && string.buf[i] == '-') { + while (++i < string.len && string.buf[i] && string.buf[i] != '-') { + const char c = string.buf[i]; + if (!is_alpha(c) && !is_digit(c)) { + return false; + } + } + } - return serd_new_plain_literal_i(str, flags, lang); + return true; } SerdNode* -serd_new_typed_literal(const SerdStringView str, - const SerdStringView datatype_uri) +serd_new_literal(const SerdStringView string, + const SerdNodeFlags flags, + const SerdStringView meta) { - if (!datatype_uri.len) { - return serd_new_string(str); + if (!(flags & (SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE))) { + SerdNode* node = serd_node_malloc(string.len, flags, SERD_LITERAL); + + memcpy(serd_node_buffer(node), string.buf, string.len); + node->length = string.len; + serd_node_check_padding(node); + return node; } - if (!strcmp(datatype_uri.buf, NS_RDF "langString")) { + if ((flags & SERD_HAS_DATATYPE) && (flags & SERD_HAS_LANGUAGE)) { return NULL; } - SerdNodeFlags flags = 0u; - serd_strlen(str.buf, &flags); + if (!meta.len) { + return NULL; + } - flags |= SERD_HAS_DATATYPE; + if (((flags & SERD_HAS_DATATYPE) && + (!serd_uri_string_has_scheme(meta.buf) || + !strcmp(meta.buf, NS_RDF "langString"))) || + ((flags & SERD_HAS_LANGUAGE) && !is_langtag(meta))) { + return NULL; + } - const size_t len = serd_node_pad_length(str.len); - const size_t total_len = len + sizeof(SerdNode) + datatype_uri.len; + const size_t len = serd_node_pad_length(string.len); + const size_t meta_len = serd_node_pad_length(meta.len); + const size_t meta_size = sizeof(SerdNode) + meta_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* node = serd_node_malloc(len + meta_size, flags, SERD_LITERAL); + memcpy(serd_node_buffer(node), string.buf, string.len); + node->length = string.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); + SerdNode* meta_node = node + 1u + (len / sizeof(SerdNode)); + meta_node->length = meta.len; + meta_node->type = (flags & SERD_HAS_DATATYPE) ? SERD_URI : SERD_LITERAL; + memcpy(serd_node_buffer(meta_node), meta.buf, meta.len); + serd_node_check_padding(meta_node); serd_node_check_padding(node); return node; @@ -564,8 +570,9 @@ serd_new_double(const double d) const ExessResult r = exess_write_double(d, sizeof(buf), buf); return r.status ? NULL - : serd_new_typed_literal(SERD_SUBSTRING(buf, r.count), - SERD_STRING(EXESS_XSD_URI "double")); + : serd_new_literal(SERD_SUBSTRING(buf, r.count), + SERD_HAS_DATATYPE, + SERD_STRING(EXESS_XSD_URI "double")); } SerdNode* @@ -576,15 +583,17 @@ serd_new_float(const float f) const ExessResult r = exess_write_float(f, sizeof(buf), buf); return r.status ? NULL - : serd_new_typed_literal(SERD_SUBSTRING(buf, r.count), - SERD_STRING(EXESS_XSD_URI "float")); + : serd_new_literal(SERD_SUBSTRING(buf, r.count), + SERD_HAS_DATATYPE, + SERD_STRING(EXESS_XSD_URI "float")); } SerdNode* serd_new_boolean(bool b) { - return serd_new_typed_literal(b ? SERD_STRING("true") : SERD_STRING("false"), - serd_node_string_view(&serd_xsd_boolean.node)); + return serd_new_literal(b ? SERD_STRING("true") : SERD_STRING("false"), + SERD_HAS_DATATYPE, + serd_node_string_view(&serd_xsd_boolean.node)); } SerdNode* diff --git a/src/nodes.c b/src/nodes.c index 412d0d24..51f354bb 100644 --- a/src/nodes.c +++ b/src/nodes.c @@ -156,20 +156,13 @@ serd_nodes_string(SerdNodes* const nodes, const SerdStringView string) return serd_nodes_manage(nodes, serd_new_string(string)); } -const SerdNode* -serd_nodes_plain_literal(SerdNodes* const nodes, - const SerdStringView string, - const SerdStringView language) -{ - return serd_nodes_manage(nodes, serd_new_plain_literal(string, language)); -} - -const SerdNode* -serd_nodes_typed_literal(SerdNodes* const nodes, - const SerdStringView string, - const SerdStringView datatype_uri) +const SerdNode* SERD_ALLOCATED +serd_nodes_literal(SerdNodes* const nodes, + const SerdStringView string, + const SerdNodeFlags flags, + const SerdStringView meta) { - return serd_nodes_manage(nodes, serd_new_typed_literal(string, datatype_uri)); + return serd_nodes_manage(nodes, serd_new_literal(string, flags, meta)); } const SerdNode* diff --git a/src/read_ntriples.c b/src/read_ntriples.c index 78e46634..017c4dcf 100644 --- a/src/read_ntriples.c +++ b/src/read_ntriples.c @@ -206,24 +206,8 @@ read_IRI(SerdReader* const reader, SerdNode** const dest) SerdStatus read_character(SerdReader* const reader, SerdNode* const dest, const uint8_t c) { - if (!(c & 0x80)) { - switch (c) { - case 0xA: - case 0xD: - dest->flags |= SERD_HAS_NEWLINE; - break; - case '"': - case '\'': - dest->flags |= SERD_HAS_QUOTE; - break; - default: - break; - } - - return push_byte(reader, dest, c); - } - - return read_utf8_continuation(reader, dest, c); + return !(c & 0x80) ? push_byte(reader, dest, c) + : read_utf8_continuation(reader, dest, c); } /// [9] STRING_LITERAL_QUOTE @@ -432,11 +416,9 @@ read_ECHAR(SerdReader* const reader, SerdNode* const dest) eat_byte_safe(reader, 'b'); return push_byte(reader, dest, '\b'); case 'n': - dest->flags |= SERD_HAS_NEWLINE; eat_byte_safe(reader, 'n'); return push_byte(reader, dest, '\n'); case 'r': - dest->flags |= SERD_HAS_NEWLINE; eat_byte_safe(reader, 'r'); return push_byte(reader, dest, '\r'); case 'f': diff --git a/src/string.c b/src/string.c index 97c1432b..2d25fb80 100644 --- a/src/string.c +++ b/src/string.c @@ -14,13 +14,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "string_utils.h" - #include "serd/serd.h" -#include <assert.h> #include <stdlib.h> -#include <string.h> void serd_free(void* const ptr) @@ -66,50 +62,3 @@ serd_strerror(const SerdStatus status) return "Unknown error"; } - -static void -serd_update_flags(const char c, SerdNodeFlags* const flags) -{ - switch (c) { - case '\r': - case '\n': - *flags |= SERD_HAS_NEWLINE; - break; - case '"': - *flags |= SERD_HAS_QUOTE; - break; - default: - break; - } -} - -size_t -serd_substrlen(const char* const str, - const size_t len, - SerdNodeFlags* const flags) -{ - assert(flags); - - size_t i = 0; - *flags = 0; - for (; i < len && str[i]; ++i) { - serd_update_flags(str[i], flags); - } - - return i; -} - -size_t -serd_strlen(const char* const str, SerdNodeFlags* const flags) -{ - if (flags) { - size_t i = 0; - *flags = 0; - for (; str[i]; ++i) { - serd_update_flags(str[i], flags); - } - return i; - } - - return strlen(str); -} diff --git a/src/string_utils.h b/src/string_utils.h index a411b90d..54f7877c 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -17,8 +17,6 @@ #ifndef SERD_STRING_UTILS_H #define SERD_STRING_UTILS_H -#include "serd/serd.h" - #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -90,9 +88,6 @@ is_windows_path(const char* path) (path[2] == '/' || path[2] == '\\'); } -size_t -serd_substrlen(const char* str, size_t len, SerdNodeFlags* flags); - static inline char serd_to_upper(const char c) { diff --git a/src/writer.c b/src/writer.c index c6a91d78..5416d144 100644 --- a/src/writer.c +++ b/src/writer.c @@ -737,8 +737,7 @@ write_literal(SerdWriter* const writer, } SerdStatus st = SERD_SUCCESS; - if (supports_abbrev(writer) && - (node->flags & (SERD_HAS_NEWLINE | SERD_HAS_QUOTE))) { + if (supports_abbrev(writer) && (node->flags & SERD_IS_LONG)) { TRY(st, esink("\"\"\"", 3, writer)); TRY(st, write_text(writer, WRITE_LONG_STRING, node_str, node->length)); TRY(st, esink("\"\"\"", 3, writer)); diff --git a/test/test_env.c b/test/test_env.c index 41e2060b..1b924ae9 100644 --- a/test/test_env.c +++ b/test/test_env.c @@ -164,8 +164,10 @@ test_expand_bad_uri_datatype(void) { static const SerdStringView type = SERD_STRING("Type"); - SerdNode* const typed = serd_new_typed_literal(SERD_STRING("data"), type); - SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); + SerdNode* const typed = + serd_new_literal(SERD_STRING("data"), SERD_HAS_DATATYPE, type); + + SerdEnv* const env = serd_env_new(SERD_EMPTY_STRING()); assert(!serd_env_expand_node(env, typed)); diff --git a/test/test_node.c b/test/test_node.c index 72bd44e1..ef17afe9 100644 --- a/test/test_node.c +++ b/test/test_node.c @@ -83,8 +83,8 @@ check_get_boolean(const char* string, const char* datatype_uri, const bool expected) { - SerdNode* const node = - serd_new_typed_literal(SERD_STRING(string), SERD_STRING(datatype_uri)); + SerdNode* const node = serd_new_literal( + SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); assert(serd_get_boolean(node) == expected); @@ -170,8 +170,8 @@ check_get_double(const char* string, const char* datatype_uri, const double expected) { - SerdNode* const node = - serd_new_typed_literal(SERD_STRING(string), SERD_STRING(datatype_uri)); + SerdNode* const node = serd_new_literal( + SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); @@ -199,8 +199,8 @@ test_get_double(void) assert(isnan(serd_get_double(nan))); serd_node_free(nan); - SerdNode* const invalid = - serd_new_typed_literal(SERD_STRING("!invalid"), SERD_STRING(NS_XSD "long")); + SerdNode* const invalid = serd_new_literal( + SERD_STRING("!invalid"), SERD_HAS_DATATYPE, SERD_STRING(NS_XSD "long")); assert(isnan(serd_get_double(invalid))); serd_node_free(invalid); @@ -243,8 +243,8 @@ check_get_float(const char* string, const char* datatype_uri, const float expected) { - SerdNode* const node = - serd_new_typed_literal(SERD_STRING(string), SERD_STRING(datatype_uri)); + SerdNode* const node = serd_new_literal( + SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); @@ -270,8 +270,8 @@ test_get_float(void) assert(isnan(serd_get_float(nan))); serd_node_free(nan); - SerdNode* const invalid = - serd_new_typed_literal(SERD_STRING("!invalid"), SERD_STRING(NS_XSD "long")); + SerdNode* const invalid = serd_new_literal( + SERD_STRING("!invalid"), SERD_HAS_DATATYPE, SERD_STRING(NS_XSD "long")); assert(isnan(serd_get_double(invalid))); @@ -308,8 +308,8 @@ check_get_integer(const char* string, const char* datatype_uri, const int64_t expected) { - SerdNode* const node = - serd_new_typed_literal(SERD_STRING(string), SERD_STRING(datatype_uri)); + SerdNode* const node = serd_new_literal( + SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); assert(serd_get_integer(node) == expected); @@ -372,8 +372,8 @@ check_get_base64(const char* string, const char* datatype_uri, const char* expected) { - SerdNode* const node = - serd_new_typed_literal(SERD_STRING(string), SERD_STRING(datatype_uri)); + SerdNode* const node = serd_new_literal( + SERD_STRING(string), SERD_HAS_DATATYPE, SERD_STRING(datatype_uri)); assert(node); @@ -398,8 +398,8 @@ test_get_base64(void) check_get_base64("Zm9vYg==", NS_XSD "base64Binary", "foob"); check_get_base64(" \f\n\r\t\vZm9v \f\n\r\t\v", NS_XSD "base64Binary", "foo"); - SerdNode* const node = serd_new_typed_literal( - SERD_STRING("Zm9v"), SERD_STRING(NS_XSD "base64Binary")); + SerdNode* const node = serd_new_literal( + SERD_STRING("Zm9v"), SERD_HAS_DATATYPE, SERD_STRING(NS_XSD "base64Binary")); char small[2] = {0}; const SerdWriteResult r = serd_get_base64(node, sizeof(small), small); @@ -433,7 +433,7 @@ test_node_from_syntax(void) { SerdNode* const hello = serd_new_string(SERD_STRING("hello\"")); assert(serd_node_length(hello) == 6); - assert(serd_node_flags(hello) == SERD_HAS_QUOTE); + assert(!serd_node_flags(hello)); assert(!strncmp(serd_node_string(hello), "hello\"", 6)); serd_node_free(hello); } @@ -443,7 +443,7 @@ test_node_from_substring(void) { SerdNode* const a_b = serd_new_string(SERD_SUBSTRING("a\"bc", 3)); assert(serd_node_length(a_b) == 3); - assert(serd_node_flags(a_b) == SERD_HAS_QUOTE); + assert(!serd_node_flags(a_b)); assert(strlen(serd_node_string(a_b)) == 3); assert(!strncmp(serd_node_string(a_b), "a\"b", 3)); serd_node_free(a_b); @@ -468,46 +468,54 @@ check_copy_equals(const SerdNode* const node) static void test_literal(void) { - SerdNode* hello2 = serd_new_string(SERD_STRING("hello\"")); + static const SerdStringView hello_str = SERD_STRING("hello"); + static const SerdStringView empty_str = SERD_EMPTY_STRING(); - assert(serd_node_length(hello2) == 6 && - serd_node_flags(hello2) == SERD_HAS_QUOTE && - !strcmp(serd_node_string(hello2), "hello\"")); + assert(!serd_new_literal( + hello_str, SERD_HAS_DATATYPE | SERD_HAS_LANGUAGE, SERD_STRING("whatever"))); - check_copy_equals(hello2); + assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, empty_str)); + assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, empty_str)); + + assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, SERD_STRING("Type"))); + assert(!serd_new_literal(hello_str, SERD_HAS_DATATYPE, SERD_STRING("de"))); - SerdNode* hello3 = - serd_new_plain_literal(SERD_STRING("hello\""), SERD_EMPTY_STRING()); + assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, SERD_STRING("3n"))); + assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, SERD_STRING("d3"))); + assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, SERD_STRING("d3"))); + assert(!serd_new_literal(hello_str, SERD_HAS_LANGUAGE, SERD_STRING("en-!"))); - assert(serd_node_equals(hello2, hello3)); + SerdNode* hello2 = serd_new_string(SERD_STRING("hello\"")); - SerdNode* hello4 = - serd_new_typed_literal(SERD_STRING("hello\""), SERD_EMPTY_STRING()); + assert(serd_node_length(hello2) == 6 && + !strcmp(serd_node_string(hello2), "hello\"")); - assert(!serd_new_typed_literal(SERD_STRING("plain"), - SERD_STRING(NS_RDF "langString"))); + check_copy_equals(hello2); - assert(serd_node_equals(hello4, hello2)); + assert(!serd_new_literal( + SERD_STRING("plain"), SERD_HAS_DATATYPE, SERD_STRING(NS_RDF "langString"))); - serd_node_free(hello4); - serd_node_free(hello3); serd_node_free(hello2); - 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)); + const char* lang_lit_str = "\"Hello\"@en-ca"; + SerdNode* sliced_lang_lit = + serd_new_literal(SERD_SUBSTRING(lang_lit_str + 1, 5), + SERD_HAS_LANGUAGE, + SERD_SUBSTRING(lang_lit_str + 8, 5)); assert(!strcmp(serd_node_string(sliced_lang_lit), "Hello")); const SerdNode* const lang = serd_node_language(sliced_lang_lit); assert(lang); - assert(!strcmp(serd_node_string(lang), "en")); + assert(!strcmp(serd_node_string(lang), "en-ca")); check_copy_equals(sliced_lang_lit); serd_node_free(sliced_lang_lit); - 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)); + const char* type_lit_str = "\"Hallo\"^^<http://example.org/Greeting>"; + SerdNode* sliced_type_lit = + serd_new_literal(SERD_SUBSTRING(type_lit_str + 1, 5), + SERD_HAS_DATATYPE, + SERD_SUBSTRING(type_lit_str + 10, 27)); assert(!strcmp(serd_node_string(sliced_type_lit), "Hallo")); @@ -515,11 +523,6 @@ test_literal(void) assert(datatype); assert(!strcmp(serd_node_string(datatype), "http://example.org/Greeting")); serd_node_free(sliced_type_lit); - - 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 @@ -538,17 +541,17 @@ test_compare(void) SerdNode* xsd_short = serd_new_uri(SERD_STRING("http://www.w3.org/2001/XMLSchema#short")); - SerdNode* angst = - serd_new_plain_literal(SERD_STRING("angst"), SERD_EMPTY_STRING()); + SerdNode* angst = serd_new_string(SERD_STRING("angst")); - SerdNode* angst_de = - serd_new_plain_literal(SERD_STRING("angst"), SERD_STRING("de")); + SerdNode* angst_de = serd_new_literal( + SERD_STRING("angst"), SERD_HAS_LANGUAGE, SERD_STRING("de")); - SerdNode* angst_en = - serd_new_plain_literal(SERD_STRING("angst"), SERD_STRING("en")); + assert(angst_de); + SerdNode* angst_en = serd_new_literal( + SERD_STRING("angst"), SERD_HAS_LANGUAGE, SERD_STRING("en")); - SerdNode* hallo = - serd_new_plain_literal(SERD_STRING("Hallo"), SERD_STRING("de")); + SerdNode* hallo = serd_new_literal( + SERD_STRING("Hallo"), SERD_HAS_LANGUAGE, SERD_STRING("de")); SerdNode* hello = serd_new_string(SERD_STRING("Hello")); SerdNode* universe = serd_new_string(SERD_STRING("Universe")); diff --git a/test/test_node_syntax.c b/test/test_node_syntax.c index bb394c43..26b57ff1 100644 --- a/test/test_node_syntax.c +++ b/test/test_node_syntax.c @@ -53,11 +53,12 @@ test_common(const SerdSyntax syntax) assert(test(syntax, serd_new_string(SERD_STRING("node")), "\"node\"")); assert(test(syntax, - serd_new_plain_literal(SERD_STRING("hallo"), SERD_STRING("de")), + serd_new_literal( + SERD_STRING("hallo"), SERD_HAS_LANGUAGE, SERD_STRING("de")), "\"hallo\"@de")); assert(test(syntax, - serd_new_typed_literal(SERD_STRING("X"), datatype), + serd_new_literal(SERD_STRING("X"), SERD_HAS_DATATYPE, datatype), "\"X\"^^<http://example.org/Datatype>")); assert(test(syntax, serd_new_blank(SERD_STRING("blank")), "_:blank")); diff --git a/test/test_nodes.c b/test/test_nodes.c index 7e67eb30..74c99c11 100644 --- a/test/test_nodes.c +++ b/test/test_nodes.c @@ -88,7 +88,7 @@ test_plain_literal(void) SerdNodes* const nodes = serd_nodes_new(); const SerdNode* const node = - serd_nodes_plain_literal(nodes, string, language); + serd_nodes_literal(nodes, string, SERD_HAS_LANGUAGE, language); assert(node); assert(serd_node_type(node) == SERD_LITERAL); @@ -113,7 +113,7 @@ test_typed_literal(void) SerdNodes* const nodes = serd_nodes_new(); const SerdNode* const node = - serd_nodes_typed_literal(nodes, string, datatype); + serd_nodes_literal(nodes, string, SERD_HAS_DATATYPE, datatype); assert(node); assert(serd_node_type(node) == SERD_LITERAL); diff --git a/test/test_reader_writer.c b/test/test_reader_writer.c index 0124784c..cab33ff5 100644 --- a/test/test_reader_writer.c +++ b/test/test_reader_writer.c @@ -81,10 +81,10 @@ test_writer(const char* const path) const SerdNode* const o = serd_nodes_string(nodes, SERD_STRING("o")); const SerdNode* const t = - serd_nodes_typed_literal(nodes, SERD_STRING("t"), urn_Type); + serd_nodes_literal(nodes, SERD_STRING("t"), SERD_HAS_DATATYPE, urn_Type); const SerdNode* const l = - serd_nodes_plain_literal(nodes, SERD_STRING("l"), en); + serd_nodes_literal(nodes, SERD_STRING("l"), SERD_HAS_LANGUAGE, en); const SerdNode* good[][3] = {{s, p, o}, {s, p, t}, {s, p, l}}; diff --git a/test/test_string.c b/test/test_string.c index 2f805015..1181de01 100644 --- a/test/test_string.c +++ b/test/test_string.c @@ -24,22 +24,10 @@ #endif #include <assert.h> -#include <stdint.h> #include <stdio.h> #include <string.h> static void -test_strlen(void) -{ - const uint8_t str[] = {'"', '5', 0xE2, 0x82, 0xAC, '"', '\n', 0}; - - SerdNodeFlags flags = 0; - size_t n_bytes = serd_strlen((const char*)str, &flags); - assert(n_bytes == 7 && flags == (SERD_HAS_QUOTE | SERD_HAS_NEWLINE)); - assert(serd_strlen((const char*)str, NULL) == 7); -} - -static void test_strerror(void) { const char* msg = serd_strerror(SERD_SUCCESS); @@ -78,7 +66,6 @@ main(int argc, char** argv) { (void)argc; - test_strlen(); test_strerror(); test_canonical_path(argv[0]); diff --git a/test/test_writer.c b/test/test_writer.c index c9a77354..fd650ecd 100644 --- a/test/test_writer.c +++ b/test/test_writer.c @@ -64,11 +64,17 @@ test_write_long_literal(void) const SerdNode* s = serd_nodes_uri(nodes, SERD_STRING("http://example.org/s")); + const SerdNode* p = serd_nodes_uri(nodes, SERD_STRING("http://example.org/p")); + const SerdNode* o = - serd_nodes_string(nodes, SERD_STRING("hello \"\"\"world\"\"\"!")); + serd_nodes_literal(nodes, + SERD_STRING("hello \"\"\"world\"\"\"!"), + SERD_IS_LONG, + SERD_EMPTY_STRING()); + assert(serd_node_flags(o) & SERD_IS_LONG); assert(!serd_sink_write(serd_writer_sink(writer), 0, s, p, o, NULL)); serd_writer_free(writer); |