diff options
author | David Robillard <d@drobilla.net> | 2021-08-12 13:42:25 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2023-12-02 18:49:08 -0500 |
commit | 8346ac7f529f5aeb8d8b0e48837e680ea14e8893 (patch) | |
tree | 0e1aa8f67135af24cbbfe5a5911b1a74e5699df6 /src | |
parent | 3ea3143632e3577ac5794faed1141e460a11a9fb (diff) | |
download | serd-8346ac7f529f5aeb8d8b0e48837e680ea14e8893.tar.gz serd-8346ac7f529f5aeb8d8b0e48837e680ea14e8893.tar.bz2 serd-8346ac7f529f5aeb8d8b0e48837e680ea14e8893.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/read_ntriples.c | 6 | ||||
-rw-r--r-- | src/reader.c | 39 | ||||
-rw-r--r-- | src/reader.h | 2 | ||||
-rw-r--r-- | src/world.h | 3 | ||||
-rw-r--r-- | src/writer.c | 35 |
5 files changed, 23 insertions, 62 deletions
diff --git a/src/read_ntriples.c b/src/read_ntriples.c index 57b1e7be..dd5c28fc 100644 --- a/src/read_ntriples.c +++ b/src/read_ntriples.c @@ -273,10 +273,8 @@ read_BLANK_NODE_LABEL(SerdReader* const reader, return r_err(reader, SERD_BAD_SYNTAX, "expected blank node label"); } - 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_BAD_STACK; } diff --git a/src/reader.c b/src/reader.c index 39e85b47..73509c6f 100644 --- a/src/reader.c +++ b/src/reader.c @@ -68,11 +68,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 +200,13 @@ serd_reader_read_document(SerdReader* const reader) return SERD_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) { @@ -271,6 +277,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; } @@ -286,28 +298,9 @@ serd_reader_free(SerdReader* const reader) } serd_aaligned_free(reader->world->allocator, reader->stack.buf); - serd_wfree(reader->world, reader->bprefix); serd_wfree(reader->world, reader); } -void -serd_reader_add_blank_prefix(SerdReader* const reader, const char* const prefix) -{ - assert(reader); - - serd_wfree(reader->world, 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*)serd_wmalloc(reader->world, 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 a98d5ef8..0bb3454b 100644 --- a/src/reader.h +++ b/src/reader.h @@ -46,7 +46,7 @@ struct SerdReaderImpl { SerdSyntax syntax; SerdReaderFlags flags; unsigned next_id; - 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 2499b761..f615868a 100644 --- a/src/world.h +++ b/src/world.h @@ -14,10 +14,11 @@ #include <stdint.h> struct SerdWorldImpl { - SerdLimits limits; SerdAllocator* allocator; SerdLog log; + SerdLimits limits; uint32_t next_blank_id; + uint32_t next_document_id; SerdNode* blank_node; bool stderr_color; diff --git a/src/writer.c b/src/writer.c index 1de5e055..7afeb1c4 100644 --- a/src/writer.c +++ b/src/writer.c @@ -140,8 +140,6 @@ struct SerdWriterImpl { size_t anon_stack_size; SerdBlockDumper output; WriteContext context; - char* bprefix; - size_t bprefix_len; Sep last_sep; int indent; }; @@ -901,17 +899,7 @@ write_blank(SerdWriter* const writer, } TRY(st, esink("_:", 2, writer)); - if (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)); - } - - return st; + return esink(node_str, node->length, writer); } SERD_NODISCARD static SerdStatus @@ -1382,25 +1370,7 @@ serd_writer_new(SerdWorld* world, return writer; } -void -serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix) -{ - assert(writer); - - serd_wfree(writer->world, 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*)serd_wmalloc(writer->world, writer->bprefix_len + 1); - memcpy(writer->bprefix, prefix, writer->bprefix_len + 1); - } -} - -SERD_NODISCARD static SerdStatus +static SerdStatus serd_writer_set_base_uri(SerdWriter* writer, const SerdNode* uri) { assert(writer); @@ -1482,7 +1452,6 @@ serd_writer_free(SerdWriter* writer) free_anon_stack(writer); serd_block_dumper_close(&writer->output); serd_wfree(writer->world, writer->anon_stack); - serd_wfree(writer->world, writer->bprefix); serd_node_free(writer->world->allocator, writer->root_node); serd_wfree(writer->world, writer); } |