From 4d535bbe0390ed4f03c611e433145c9e49cbf3ad Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 9 Jul 2017 14:59:05 +0200 Subject: Add serd_node_from_substring() This allows creating nodes in-place from substrings of other strings to allow zero-copy serialization from existing delimited buffers. --- src/node.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 42992917..10df10d9 100644 --- a/src/node.c +++ b/src/node.c @@ -46,6 +46,22 @@ serd_node_from_string(SerdType type, const uint8_t* str) return ret; } +SERD_API +SerdNode +serd_node_from_substring(SerdType type, const uint8_t* str, const size_t len) +{ + if (!str) { + return SERD_NODE_NULL; + } + + uint32_t flags = 0; + size_t buf_n_bytes = 0; + const size_t buf_n_chars = serd_substrlen(str, len, &buf_n_bytes, &flags); + assert(buf_n_bytes <= len); + SerdNode ret = { str, buf_n_bytes, buf_n_chars, flags, type }; + return ret; +} + SERD_API SerdNode serd_node_copy(const SerdNode* node) @@ -187,7 +203,7 @@ serd_node_new_file_uri(const uint8_t* path, serd_uri_parse(chunk.buf, out); } - return serd_node_from_string(SERD_URI, chunk.buf); + return serd_node_from_substring(SERD_URI, chunk.buf, chunk.len); } SERD_API -- cgit v1.2.1