diff options
-rw-r--r-- | serd/serd.h | 83 | ||||
-rw-r--r-- | src/env.c | 14 | ||||
-rw-r--r-- | src/node.c | 209 | ||||
-rw-r--r-- | src/node.h | 2 | ||||
-rw-r--r-- | src/serdi.c | 6 | ||||
-rw-r--r-- | src/world.c | 2 | ||||
-rw-r--r-- | tests/serd_test.c | 161 |
7 files changed, 298 insertions, 179 deletions
diff --git a/serd/serd.h b/serd/serd.h index 281a812f..4e74bd64 100644 --- a/serd/serd.h +++ b/serd/serd.h @@ -344,7 +344,7 @@ serd_strtod(const char* str, size_t* end); /** Decode a base64 string. This function can be used to deserialise a blob node created with - serd_node_new_blob(). + serd_new_blob(). @param str Base64 string to decode. @param len The length of `str`. @@ -523,47 +523,83 @@ serd_uri_serialise_relative(const SerdURI* uri, */ SERD_API SerdNode* -serd_node_new_simple_node(SerdType type, const char* str, size_t len); +serd_new_simple_node(SerdType type, const char* str, size_t len); /** Create a new plain literal string node from `str`. */ SERD_API SerdNode* -serd_node_new_string(const char* str); +serd_new_string(const char* str); /** Create a new plain literal string node from a prefix of `str`. */ SERD_API SerdNode* -serd_node_new_substring(const char* str, size_t len); +serd_new_substring(const char* str, size_t len); + +/** + Create a new literal node from substrings. + + This is a low-level constructor which can be used for constructing a literal + from slices of a buffer (for example, directly from a Turtle literal) + without copying. In most cases, applications should use the simpler + serd_new_plain_literal() or serd_new_typed_literal(). + + Either `datatype_uri` or `lang` can be given, but not both, unless + `datatype_uri` is rdf:langString in which case it is ignored. + + @param str Literal body string. + @param str_len Length of `str` in bytes. + @param datatype_uri Full datatype URI, or NULL. + @param datatype_uri_len Length of `datatype_uri` in bytes. + @param lang Language string. + @param lang_len Length of `lang` in bytes. +*/ +SERD_API +SerdNode* +serd_new_literal(const char* str, + size_t str_len, + const char* datatype_uri, + size_t datatype_uri_len, + const char* lang, + size_t lang_len); /** Create a new literal node from `str`. - Either `datatype` or `lang` can be given, but not both, unless `datatype` 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 NULL, in which case this is equivalent to `serd_new_string()`. */ SERD_API SerdNode* -serd_node_new_literal(const char* str, - const SerdNode* datatype, - const char* lang); +serd_new_plain_literal(const char* str, const char* 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_new_typed_literal(const char* str, const SerdNode* datatype); /** Create a new blank node. */ SERD_API SerdNode* -serd_node_new_blank(const char* str); +serd_new_blank(const char* str); /** Create a new CURIE node. */ SERD_API SerdNode* -serd_node_new_curie(const char* str); +serd_new_curie(const char* str); /** Return a deep copy of `node`. @@ -584,14 +620,14 @@ serd_node_equals(const SerdNode* a, const SerdNode* b); */ SERD_API SerdNode* -serd_node_new_uri(const char* str); +serd_new_uri(const char* str); /** Create a new URI from a string, resolved against a base URI. */ SERD_API SerdNode* -serd_node_new_resolved_uri(const char* str, const SerdNode* base); +serd_new_resolved_uri(const char* str, const SerdNode* base); /** Resolve `node` against `base`. @@ -614,7 +650,7 @@ serd_node_resolve(const SerdNode* node, const SerdNode* base); */ SERD_API 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); /** Create a new URI from a string, relative to a base URI. @@ -631,9 +667,9 @@ serd_node_new_file_uri(const char* path, const char* hostname, bool escape); */ SERD_API 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); /** Create a new node by serialising `d` into an xsd:decimal string. @@ -656,7 +692,7 @@ serd_node_new_relative_uri(const char* str, */ SERD_API SerdNode* -serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype); +serd_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype); /** Create a new node by serialising `i` into an xsd:integer string. @@ -668,7 +704,7 @@ serd_node_new_decimal(double d, unsigned frac_digits, const SerdNode* datatype); */ SERD_API SerdNode* -serd_node_new_integer(int64_t i, const SerdNode* datatype); +serd_new_integer(int64_t i, const SerdNode* datatype); /** Create a node by serialising `buf` into an xsd:base64Binary string. @@ -685,10 +721,11 @@ serd_node_new_integer(int64_t i, const SerdNode* datatype); type xsd:base64Binary. */ SERD_API -SerdNode* serd_node_new_blob(const void* buf, - size_t size, - bool wrap_lines, - const SerdNode* datatype); +SerdNode* +serd_new_blob(const void* buf, + size_t size, + bool wrap_lines, + const SerdNode* datatype); /** Return the type of a node (SERD_URI, SERD_BLANK, or SERD_LITERAL). @@ -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; @@ -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; @@ -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 70929781..578d1855 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 9908e4d4..27feeac6 100644 --- a/src/world.c +++ b/src/world.c @@ -76,7 +76,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; } diff --git a/tests/serd_test.c b/tests/serd_test.c index bfb0af21..e58380c4 100644 --- a/tests/serd_test.c +++ b/tests/serd_test.c @@ -90,7 +90,7 @@ test_file_uri(const char* hostname, expected_path = path; } - SerdNode* node = serd_node_new_file_uri(path, hostname, escape); + SerdNode* node = serd_new_file_uri(path, hostname, escape); const char* node_str = serd_node_get_string(node); char* out_hostname = NULL; char* out_path = serd_file_uri_parse(node_str, &out_hostname); @@ -234,7 +234,7 @@ test_double_to_node(void) }; for (unsigned i = 0; i < sizeof(dbl_test_nums) / sizeof(double); ++i) { - SerdNode* node = serd_node_new_decimal(dbl_test_nums[i], 8, NULL); + SerdNode* node = serd_new_decimal(dbl_test_nums[i], 8, NULL); const char* node_str = serd_node_get_string(node); const bool pass = (node_str && dbl_test_strs[i]) ? !strcmp(node_str, dbl_test_strs[i]) @@ -261,7 +261,7 @@ test_integer_to_node(void) }; for (unsigned i = 0; i < sizeof(int_test_nums) / sizeof(double); ++i) { - SerdNode* node = serd_node_new_integer(int_test_nums[i], NULL); + SerdNode* node = serd_new_integer(int_test_nums[i], NULL); const char* node_str = serd_node_get_string(node); assert(!strcmp(node_str, int_test_strs[i])); const size_t len = strlen(node_str); @@ -275,8 +275,8 @@ test_integer_to_node(void) static void test_blob_to_node(void) { - assert(!serd_node_new_blob(NULL, 0, true, NULL)); - assert(!serd_node_new_blob("data", 0, true, NULL)); + assert(!serd_new_blob(NULL, 0, true, NULL)); + assert(!serd_new_blob("data", 0, true, NULL)); for (size_t size = 1; size < 256; ++size) { uint8_t* data = (uint8_t*)malloc(size); @@ -285,7 +285,7 @@ test_blob_to_node(void) } size_t out_size; - SerdNode* blob = serd_node_new_blob(data, size, size % 5, NULL); + SerdNode* blob = serd_new_blob(data, size, size % 5, NULL); const char* blob_str = serd_node_get_string(blob); uint8_t* out = (uint8_t*)serd_base64_decode( blob_str, serd_node_get_length(blob), &out_size); @@ -356,11 +356,11 @@ static void test_node_equals(void) { const uint8_t replacement_char_str[] = { 0xEF, 0xBF, 0xBD, 0 }; - SerdNode* lhs = serd_node_new_string((const char*)replacement_char_str); - SerdNode* rhs = serd_node_new_string("123"); + SerdNode* lhs = serd_new_string((const char*)replacement_char_str); + SerdNode* rhs = serd_new_string("123"); assert(!serd_node_equals(lhs, rhs)); - SerdNode* qnode = serd_node_new_curie("foo:bar"); + SerdNode* qnode = serd_new_curie("foo:bar"); assert(!serd_node_equals(lhs, qnode)); serd_node_free(qnode); @@ -373,9 +373,9 @@ test_node_equals(void) static void test_node_from_string(void) { - assert(!serd_node_new_string(NULL)); + assert(!serd_new_string(NULL)); - SerdNode* hello = serd_node_new_string("hello\""); + SerdNode* hello = serd_new_string("hello\""); assert(serd_node_get_length(hello) == 6); assert(serd_node_get_flags(hello) == SERD_HAS_QUOTE); assert(!strncmp(serd_node_get_string(hello), "hello\"", 6)); @@ -385,16 +385,16 @@ test_node_from_string(void) static void test_node_from_substring(void) { - assert(!serd_node_new_substring(NULL, 32)); + assert(!serd_new_substring(NULL, 32)); - SerdNode* a_b = serd_node_new_substring("a\"bc", 3); + SerdNode* a_b = serd_new_substring("a\"bc", 3); assert(serd_node_get_length(a_b) == 3); assert(serd_node_get_flags(a_b) == SERD_HAS_QUOTE); assert(strlen(serd_node_get_string(a_b)) == 3); assert(!strncmp(serd_node_get_string(a_b), "a\"b", 3)); serd_node_free(a_b); - a_b = serd_node_new_substring("a\"bc", 10); + a_b = serd_new_substring("a\"bc", 10); assert(serd_node_get_length(a_b) == 4); assert(serd_node_get_flags(a_b) == SERD_HAS_QUOTE); assert(strlen(serd_node_get_string(a_b)) == 4); @@ -405,53 +405,62 @@ test_node_from_substring(void) static void test_simple_node(void) { - assert(!serd_node_new_simple_node(SERD_LITERAL, "Literal", 7)); - assert(!serd_node_new_simple_node(SERD_URI, NULL, 0)); + assert(!serd_new_simple_node(SERD_LITERAL, "Literal", 7)); + assert(!serd_new_simple_node(SERD_URI, NULL, 0)); } static void test_literal(void) { - assert(!serd_node_new_literal(NULL, NULL, NULL)); + assert(!serd_new_literal(NULL, 0, NULL, 0, NULL, 0)); + assert(!serd_new_plain_literal(NULL, NULL)); + assert(!serd_new_typed_literal(NULL, NULL)); - SerdNode* hello2 = serd_node_new_literal("hello\"", NULL, NULL); + SerdNode* hello2 = serd_new_string("hello\""); assert(serd_node_get_length(hello2) == 6 && serd_node_get_flags(hello2) == SERD_HAS_QUOTE && !strcmp(serd_node_get_string(hello2), "hello\"")); + + SerdNode* hello3 = serd_new_plain_literal("hello\"", NULL); + assert(serd_node_equals(hello2, hello3)); + + serd_node_free(hello3); serd_node_free(hello2); - SerdNode* hello_l = serd_node_new_literal("hello_l\"", NULL, "en"); - assert(serd_node_get_length(hello_l) == 8); - assert(!strcmp(serd_node_get_string(hello_l), "hello_l\"")); - assert(serd_node_get_flags(hello_l) == - (SERD_HAS_QUOTE | SERD_HAS_LANGUAGE)); - assert(!strcmp(serd_node_get_string(serd_node_get_language(hello_l)), - "en")); - serd_node_free(hello_l); - - SerdNode* eg_Thing = serd_node_new_uri("http://example.org/Thing"); - SerdNode* hello_dt = serd_node_new_literal("hello_dt\"", eg_Thing, NULL); - assert(serd_node_get_length(hello_dt) == 9); - assert(!strcmp(serd_node_get_string(hello_dt), "hello_dt\"")); - assert(serd_node_get_flags(hello_dt) == - (SERD_HAS_QUOTE | SERD_HAS_DATATYPE)); - assert(!strcmp(serd_node_get_string(serd_node_get_datatype(hello_dt)), - "http://example.org/Thing")); - serd_node_free(hello_dt); - serd_node_free(eg_Thing); + const char* lang_lit_str = "\"Hello\"@en"; + SerdNode* sliced_lang_lit = + serd_new_literal(lang_lit_str + 1, 5, NULL, 0, lang_lit_str + 8, 2); + assert(!strcmp(serd_node_get_string(sliced_lang_lit), "Hello")); + assert(!strcmp( + serd_node_get_string(serd_node_get_language(sliced_lang_lit)), + "en")); + serd_node_free(sliced_lang_lit); + + const char* type_lit_str = "\"Hallo\"^^<http://example.org/Greeting>"; + SerdNode* sliced_type_lit = + serd_new_literal(type_lit_str + 1, 5, type_lit_str + 10, 27, NULL, 0); + assert(!strcmp(serd_node_get_string(sliced_type_lit), "Hallo")); + assert(!strcmp( + serd_node_get_string(serd_node_get_datatype(sliced_type_lit)), + "http://example.org/Greeting")); + serd_node_free(sliced_type_lit); + + SerdNode* plain_lit = serd_new_literal("Plain", 5, NULL, 0, NULL, 0); + assert(!strcmp(serd_node_get_string(plain_lit), "Plain")); + serd_node_free(plain_lit); } static void test_uri_from_string(void) { - assert(!serd_node_new_uri(NULL)); - - SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/"); - SerdNode* not_a_uri = serd_node_new_string("hello"); - SerdNode* nil = serd_node_new_resolved_uri(NULL, base); - SerdNode* nil2 = serd_node_new_resolved_uri("", base); - assert(!serd_node_new_resolved_uri("", NULL)); - assert(!serd_node_new_resolved_uri("", not_a_uri)); + assert(!serd_new_uri(NULL)); + + SerdNode* base = serd_new_uri("http://example.org/a/b/c/"); + SerdNode* not_a_uri = serd_new_string("hello"); + SerdNode* nil = serd_new_resolved_uri(NULL, base); + SerdNode* nil2 = serd_new_resolved_uri("", base); + assert(!serd_new_resolved_uri("", NULL)); + assert(!serd_new_resolved_uri("", not_a_uri)); assert(serd_node_get_type(nil) == SERD_URI); assert(!strcmp(serd_node_get_string(nil), serd_node_get_string(base))); assert(serd_node_get_type(nil2) == SERD_URI); @@ -468,7 +477,7 @@ check_rel_uri(const char* uri, const SerdNode* root, const char* expected) { - SerdNode* rel = serd_node_new_relative_uri(uri, base, root); + SerdNode* rel = serd_new_relative_uri(uri, base, root); const int ret = strcmp(serd_node_get_string(rel), expected); serd_node_free(rel); assert(!ret); @@ -477,8 +486,8 @@ check_rel_uri(const char* uri, static void test_relative_uri(void) { - SerdNode* root = serd_node_new_uri("http://example.org/a/b/"); - SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/"); + SerdNode* root = serd_new_uri("http://example.org/a/b/"); + SerdNode* base = serd_new_uri("http://example.org/a/b/c/"); check_rel_uri("http://example.org/a/b/c/foo", base, NULL, "foo"); check_rel_uri("http://example.org/a/", base, NULL, "../../"); @@ -493,18 +502,18 @@ test_relative_uri(void) static void test_uri_resolution(void) { - SerdNode* base = serd_node_new_uri("http://example.org/a/b/c/"); - SerdNode* nil = serd_node_new_resolved_uri(NULL, base); - SerdNode* nil2 = serd_node_new_resolved_uri("", base); - SerdNode* not_a_uri = serd_node_new_string("hello"); - SerdNode* root = serd_node_new_uri("http://example.org/a/b/"); + SerdNode* base = serd_new_uri("http://example.org/a/b/c/"); + SerdNode* nil = serd_new_resolved_uri(NULL, base); + SerdNode* nil2 = serd_new_resolved_uri("", base); + SerdNode* not_a_uri = serd_new_string("hello"); + SerdNode* root = serd_new_uri("http://example.org/a/b/"); assert(!serd_node_resolve(NULL, base)); assert(!serd_node_resolve(nil, NULL)); assert(!serd_node_resolve(not_a_uri, base)); assert(!serd_node_resolve(nil, not_a_uri)); - SerdNode* rel = serd_node_new_relative_uri( + SerdNode* rel = serd_new_relative_uri( "http://example.org/a/b/c/foo", base, NULL); SerdNode* resolved = serd_node_resolve(rel, base); assert(!strcmp(serd_node_get_string(resolved), @@ -522,9 +531,9 @@ test_uri_resolution(void) static void test_blank(void) { - assert(!serd_node_new_blank(NULL)); + assert(!serd_new_blank(NULL)); - SerdNode* blank = serd_node_new_blank("b0"); + SerdNode* blank = serd_new_blank("b0"); assert(serd_node_get_length(blank) == 2); assert(serd_node_get_flags(blank) == 0); assert(!strcmp(serd_node_get_string(blank), "b0")); @@ -536,11 +545,11 @@ test_env(void) { SerdWorld* world = serd_world_new(); - SerdNode* hello = serd_node_new_string("hello\""); - SerdNode* u = serd_node_new_uri("http://example.org/foo"); - SerdNode* b = serd_node_new_curie("invalid"); - SerdNode* c = serd_node_new_curie("eg.2:b"); - SerdNode* s = serd_node_new_string("hello"); + SerdNode* hello = serd_new_string("hello\""); + SerdNode* u = serd_new_uri("http://example.org/foo"); + SerdNode* b = serd_new_curie("invalid"); + SerdNode* c = serd_new_curie("eg.2:b"); + SerdNode* s = serd_new_string("hello"); SerdEnv* env = serd_env_new(NULL); serd_env_set_prefix_from_strings(env, "eg.2", "http://example.org/"); @@ -565,7 +574,7 @@ test_env(void) assert(!strcmp(serd_node_get_string(xu), "http://example.org/foo")); serd_node_free(xu); - SerdNode* badpre = serd_node_new_curie("hm:what"); + SerdNode* badpre = serd_new_curie("hm:what"); SerdNode* xbadpre = serd_env_expand_node(env, badpre); assert(!xbadpre); @@ -575,7 +584,7 @@ test_env(void) assert(serd_env_set_prefix(env, NULL, NULL)); - SerdNode* lit = serd_node_new_string("hello"); + SerdNode* lit = serd_new_string("hello"); assert(serd_env_set_prefix(env, b, lit)); int n_prefixes = 0; @@ -583,7 +592,7 @@ test_env(void) serd_env_foreach(env, count_prefixes, &n_prefixes); assert(n_prefixes == 1); - SerdNode* shorter_uri = serd_node_new_uri("urn:foo"); + SerdNode* shorter_uri = serd_new_uri("urn:foo"); const SerdNode* prefix_name; assert(!serd_env_qualify(env, shorter_uri, &prefix_name, &suffix)); serd_node_free(shorter_uri); @@ -618,7 +627,7 @@ test_writer(const char* const path) serd_writer_chop_blank_prefix(writer, "tmp"); serd_writer_chop_blank_prefix(writer, NULL); - SerdNode* lit = serd_node_new_string("hello"); + SerdNode* lit = serd_new_string("hello"); const SerdSink* iface = serd_writer_get_sink(writer); assert(iface->base(iface->handle, lit)); @@ -627,9 +636,9 @@ test_writer(const char* const path) assert(serd_writer_get_env(writer) == env); uint8_t buf[] = { 0xEF, 0xBF, 0xBD, 0 }; - SerdNode* s = serd_node_new_uri(""); - SerdNode* p = serd_node_new_uri("http://example.org/pred"); - SerdNode* o = serd_node_new_string((char*)buf); + SerdNode* s = serd_new_uri(""); + SerdNode* p = serd_new_uri("http://example.org/pred"); + SerdNode* o = serd_new_string((char*)buf); // Write 3 invalid statements (should write nothing) const SerdNode* junk[][5] = { { s, p, NULL }, @@ -647,10 +656,10 @@ test_writer(const char* const path) iface, 0, junk[i][0], junk[i][1], junk[i][2], 0)); } - SerdNode* urn_Type = serd_node_new_uri("urn:Type"); + SerdNode* urn_Type = serd_new_uri("urn:Type"); - SerdNode* t = serd_node_new_literal((char*)buf, urn_Type, NULL); - SerdNode* l = serd_node_new_literal((char*)buf, NULL, "en"); + SerdNode* t = serd_new_typed_literal((char*)buf, urn_Type); + SerdNode* l = serd_new_plain_literal((char*)buf, "en"); const SerdNode* good[][5] = { { s, p, o }, { s, p, o }, { s, p, t }, @@ -668,8 +677,8 @@ test_writer(const char* const path) // Write statements with bad UTF-8 (should be replaced) const char bad_str[] = { (char)0xFF, (char)0x90, 'h', 'i', 0 }; - SerdNode* bad_lit = serd_node_new_string(bad_str); - SerdNode* bad_uri = serd_node_new_uri(bad_str); + SerdNode* bad_lit = serd_new_string(bad_str); + SerdNode* bad_uri = serd_new_uri(bad_str); assert(!serd_sink_write(iface, 0, s, p, bad_lit, 0)); assert(!serd_sink_write(iface, 0, s, p, bad_uri, 0)); serd_node_free(bad_uri); @@ -677,7 +686,7 @@ test_writer(const char* const path) // Write 1 valid statement serd_node_free(o); - o = serd_node_new_string("hello"); + o = serd_new_string("hello"); assert(!serd_sink_write(iface, 0, s, p, o, 0)); serd_writer_free(writer); @@ -694,7 +703,7 @@ test_writer(const char* const path) writer = serd_writer_new( world, SERD_TURTLE, (SerdStyle)0, env, serd_buffer_sink, &buffer); - o = serd_node_new_uri("http://example.org/base"); + o = serd_new_uri("http://example.org/base"); assert(!serd_writer_set_base_uri(writer, o)); serd_node_free(o); @@ -717,7 +726,7 @@ test_reader(const char* path) SerdReader* reader = serd_reader_new(world, SERD_TURTLE, &sink, 4096); assert(reader); - SerdNode* g = serd_node_new_uri("http://example.org/"); + SerdNode* g = serd_new_uri("http://example.org/"); serd_reader_set_default_graph(reader, g); serd_reader_add_blank_prefix(reader, "tmp"); serd_reader_add_blank_prefix(reader, NULL); |