cmark

My personal build of CMark ✏️

Commit
75b48c5938f5984dbcf79a579d15c9cbd6447d12
Parent
b237924585e61532ada774bf9e70eadff00666dc
Author
Nick Wellnhofer <wellnhofer@aevum.de>
Date

Use C string instead of chunk for custom block contents

Reduces size of struct cmark_node by 8 bytes.

Diffstat

4 files changed, 24 insertions, 26 deletions

Status File Name N° Changes Insertions Deletions
Modified src/html.c 26 12 14
Modified src/node.c 12 6 6
Modified src/node.h 4 2 2
Modified src/xml.c 8 4 4
diff --git a/src/html.c b/src/html.c
@@ -179,17 +179,16 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
     cr(html);
     break;
 
-  case CMARK_NODE_CUSTOM_BLOCK:
+  case CMARK_NODE_CUSTOM_BLOCK: {
+    unsigned char *block = entering ? node->as.custom.on_enter :
+                                      node->as.custom.on_exit;
     cr(html);
-    if (entering) {
-      cmark_strbuf_put(html, node->as.custom.on_enter.data,
-                       node->as.custom.on_enter.len);
-    } else {
-      cmark_strbuf_put(html, node->as.custom.on_exit.data,
-                       node->as.custom.on_exit.len);
+    if (block) {
+      cmark_strbuf_puts(html, (char *)block);
     }
     cr(html);
     break;
+  }
 
   case CMARK_NODE_THEMATIC_BREAK:
     cr(html);
@@ -250,15 +249,14 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
     }
     break;
 
-  case CMARK_NODE_CUSTOM_INLINE:
-    if (entering) {
-      cmark_strbuf_put(html, node->as.custom.on_enter.data,
-                       node->as.custom.on_enter.len);
-    } else {
-      cmark_strbuf_put(html, node->as.custom.on_exit.data,
-                       node->as.custom.on_exit.len);
+  case CMARK_NODE_CUSTOM_INLINE: {
+    unsigned char *block = entering ? node->as.custom.on_enter :
+                                      node->as.custom.on_exit;
+    if (block) {
+      cmark_strbuf_puts(html, (char *)block);
     }
     break;
+  }
 
   case CMARK_NODE_STRONG:
     if (entering) {
diff --git a/src/node.c b/src/node.c
@@ -125,8 +125,8 @@ static void S_free_nodes(cmark_node *e) {
       break;
     case CMARK_NODE_CUSTOM_BLOCK:
     case CMARK_NODE_CUSTOM_INLINE:
-      cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_enter);
-      cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_exit);
+      NODE_MEM(e)->free(e->as.custom.on_enter);
+      NODE_MEM(e)->free(e->as.custom.on_exit);
       break;
     default:
       break;
@@ -571,7 +571,7 @@ const char *cmark_node_get_on_enter(cmark_node *node) {
   switch (node->type) {
   case CMARK_NODE_CUSTOM_INLINE:
   case CMARK_NODE_CUSTOM_BLOCK:
-    return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter);
+    return node->as.custom.on_enter ? (char *)node->as.custom.on_enter : "";
   default:
     break;
   }
@@ -587,7 +587,7 @@ int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) {
   switch (node->type) {
   case CMARK_NODE_CUSTOM_INLINE:
   case CMARK_NODE_CUSTOM_BLOCK:
-    cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
+    cmark_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter);
     return 1;
   default:
     break;
@@ -604,7 +604,7 @@ const char *cmark_node_get_on_exit(cmark_node *node) {
   switch (node->type) {
   case CMARK_NODE_CUSTOM_INLINE:
   case CMARK_NODE_CUSTOM_BLOCK:
-    return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit);
+    return node->as.custom.on_exit ? (char *)node->as.custom.on_exit : "";
   default:
     break;
   }
@@ -620,7 +620,7 @@ int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) {
   switch (node->type) {
   case CMARK_NODE_CUSTOM_INLINE:
   case CMARK_NODE_CUSTOM_BLOCK:
-    cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
+    cmark_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit);
     return 1;
   default:
     break;
diff --git a/src/node.h b/src/node.h
@@ -42,8 +42,8 @@ typedef struct {
 } cmark_link;
 
 typedef struct {
-  cmark_chunk on_enter;
-  cmark_chunk on_exit;
+  unsigned char *on_enter;
+  unsigned char *on_exit;
 } cmark_custom;
 
 enum cmark_node__internal_flags {
diff --git a/src/xml.c b/src/xml.c
@@ -110,12 +110,12 @@ static int S_render_node(cmark_node *node, cmark_event_type ev_type,
     case CMARK_NODE_CUSTOM_BLOCK:
     case CMARK_NODE_CUSTOM_INLINE:
       cmark_strbuf_puts(xml, " on_enter=\"");
-      escape_xml(xml, node->as.custom.on_enter.data,
-                 node->as.custom.on_enter.len);
+      escape_xml(xml, node->as.custom.on_enter,
+                 strlen((char *)node->as.custom.on_enter));
       cmark_strbuf_putc(xml, '"');
       cmark_strbuf_puts(xml, " on_exit=\"");
-      escape_xml(xml, node->as.custom.on_exit.data,
-                 node->as.custom.on_exit.len);
+      escape_xml(xml, node->as.custom.on_exit,
+                 strlen((char *)node->as.custom.on_exit));
       cmark_strbuf_putc(xml, '"');
       break;
     case CMARK_NODE_LINK: