From b0a4cfa36e99c27dd2b20be8f8888fa7721bad58 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sun, 19 Jan 2020 00:51:02 +0100 Subject: Use C string instead of chunk for literal text Use zero-terminated C strings and a separate length field instead of cmark_chunks. Literal inline text will now be copied from the parent block's content buffer, slowing the benchmark down by 10-15%. The node struct never references memory of other nodes now, fixing #309. Node accessors don't have to check for delayed creation of C strings, so parsing and iterating all literals using the public API should actually be faster than before. --- src/node.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/node.c') diff --git a/src/node.c b/src/node.c index 1e1f0e0..931bd46 100644 --- a/src/node.c +++ b/src/node.c @@ -116,7 +116,7 @@ static void S_free_nodes(cmark_node *e) { case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: case CMARK_NODE_HTML_BLOCK: - cmark_chunk_free(NODE_MEM(e), &e->as.literal); + NODE_MEM(e)->free(e->as.literal.data); break; case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: @@ -295,7 +295,7 @@ const char *cmark_node_get_literal(cmark_node *node) { case CMARK_NODE_TEXT: case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: - return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal); + return node->as.literal.data ? (char *)node->as.literal.data : ""; case CMARK_NODE_CODE_BLOCK: return (char *)node->as.code.literal; @@ -317,7 +317,8 @@ int cmark_node_set_literal(cmark_node *node, const char *content) { case CMARK_NODE_TEXT: case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: - cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content); + node->as.literal.len = cmark_set_cstr(NODE_MEM(node), + &node->as.literal.data, content); return 1; case CMARK_NODE_CODE_BLOCK: -- cgit v1.2.3