From d4aec28ba8ad24d5aef3ee12beeb1b805148eab1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 12 Aug 2021 13:42:25 -0400 Subject: Make blank node prefixing automatic Though potentially useful, I don't think the complexity cost of the old interface (both to the implementation and to the user) is worth it. A special tool to transform blank node labels (for example with regular expressions) would be a better approach to this if it's ever needed in the future. --- src/read_ntriples.c | 6 ++---- src/reader.c | 39 +++++++++++++++++---------------------- src/reader.h | 2 +- src/world.h | 1 + src/writer.c | 30 +----------------------------- 5 files changed, 22 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/read_ntriples.c b/src/read_ntriples.c index 08c489fe..c107207d 100644 --- a/src/read_ntriples.c +++ b/src/read_ntriples.c @@ -287,10 +287,8 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, return st; } - if (!(*dest = push_node(reader, - SERD_BLANK, - reader->bprefix ? reader->bprefix : "", - reader->bprefix_len))) { + if (!(*dest = push_node( + reader, SERD_BLANK, reader->bprefix, reader->bprefix_len))) { return SERD_ERR_OVERFLOW; } diff --git a/src/reader.c b/src/reader.c index 7a640cc6..c8a66c42 100644 --- a/src/reader.c +++ b/src/reader.c @@ -24,6 +24,7 @@ #include "stack.h" #include "statement.h" #include "system.h" +#include "world.h" #include #include @@ -70,11 +71,10 @@ set_blank_id(SerdReader* const reader, SerdNode* const node, const size_t buf_size) { - char* buf = (char*)(node + 1); - const char* prefix = reader->bprefix ? (const char*)reader->bprefix : ""; + char* const buf = (char*)(node + 1); - node->length = - (size_t)snprintf(buf, buf_size, "%sb%u", prefix, reader->next_id++); + node->length = (size_t)snprintf( + buf, buf_size, "%sb%u", reader->bprefix, reader->next_id++); } size_t @@ -201,6 +201,13 @@ serd_reader_read_document(SerdReader* const reader) return SERD_ERR_BAD_CALL; } + if (!(reader->flags & SERD_READ_GLOBAL)) { + reader->bprefix_len = (size_t)snprintf(reader->bprefix, + sizeof(reader->bprefix), + "f%u", + ++reader->world->next_document_id); + } + if (reader->syntax != SERD_SYNTAX_EMPTY && !reader->source->prepared) { SerdStatus st = serd_reader_prepare(reader); if (st) { @@ -263,6 +270,12 @@ serd_reader_new(SerdWorld* const world, assert(me->rdf_rest); assert(me->rdf_nil); + if (!(flags & SERD_READ_GLOBAL)) { + me->bprefix[0] = 'f'; + me->bprefix[1] = '0'; + me->bprefix_len = 2; + } + return me; } @@ -276,27 +289,9 @@ serd_reader_free(SerdReader* const reader) serd_reader_finish(reader); serd_free_aligned(reader->stack.buf); - free(reader->bprefix); free(reader); } -void -serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix) -{ - assert(reader); - - free(reader->bprefix); - reader->bprefix_len = 0; - reader->bprefix = NULL; - - const size_t prefix_len = prefix ? strlen(prefix) : 0; - if (prefix_len) { - reader->bprefix_len = prefix_len; - reader->bprefix = (char*)malloc(reader->bprefix_len + 1); - memcpy(reader->bprefix, prefix, reader->bprefix_len + 1); - } -} - static SerdStatus skip_bom(SerdReader* const me) { diff --git a/src/reader.h b/src/reader.h index 23d38009..ecf420f4 100644 --- a/src/reader.h +++ b/src/reader.h @@ -51,7 +51,7 @@ struct SerdReaderImpl { SerdReaderFlags flags; unsigned next_id; uint8_t* buf; - char* bprefix; + char bprefix[24]; size_t bprefix_len; bool strict; ///< True iff strict parsing bool seen_genid; diff --git a/src/world.h b/src/world.h index 8cc99e61..f1124023 100644 --- a/src/world.h +++ b/src/world.h @@ -41,6 +41,7 @@ struct SerdWorldImpl { } blank; uint32_t next_blank_id; + uint32_t next_document_id; bool stderr_color; }; diff --git a/src/writer.c b/src/writer.c index 5f178a6f..5f9fdaab 100644 --- a/src/writer.c +++ b/src/writer.c @@ -130,8 +130,6 @@ struct SerdWriterImpl { size_t anon_stack_size; SerdByteSink* byte_sink; WriteContext context; - char* bprefix; - size_t bprefix_len; Sep last_sep; int indent; bool empty; @@ -893,15 +891,7 @@ write_blank(SerdWriter* const writer, SerdStatus st = SERD_SUCCESS; TRY(st, esink("_:", 2, writer)); - if (!st && writer->bprefix && - !strncmp(node_str, writer->bprefix, writer->bprefix_len)) { - TRY(st, - esink(node_str + writer->bprefix_len, - node->length - writer->bprefix_len, - writer)); - } else { - TRY(st, esink(node_str, node->length, writer)); - } + TRY(st, esink(node_str, node->length, writer)); writer->last_sep = SEP_NONE; return st; @@ -1344,23 +1334,6 @@ serd_writer_new(SerdWorld* world, return writer; } -void -serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) -{ - assert(writer); - - free(writer->bprefix); - writer->bprefix_len = 0; - writer->bprefix = NULL; - - const size_t prefix_len = prefix ? strlen(prefix) : 0; - if (prefix_len) { - writer->bprefix_len = prefix_len; - writer->bprefix = (char*)malloc(writer->bprefix_len + 1); - memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); - } -} - SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { @@ -1445,7 +1418,6 @@ serd_writer_free(SerdWriter* writer) serd_writer_finish(writer); free(writer->anon_stack); - free(writer->bprefix); serd_node_free(writer->root_node); free(writer); } -- cgit v1.2.1