diff options
author | John MacFarlane <jgm@berkeley.edu> | 2015-07-11 21:37:19 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2015-07-11 21:37:19 -0700 |
commit | f1a1393881d1e96074e448c3935f55fb9aeb7359 (patch) | |
tree | 4bd0eadbc5c352ab1fb8c9e8a65197a7dad61531 /src | |
parent | 476f083fc0d4ac31da918f7e1110cabc10acf02f (diff) |
render: Simplified code, avoiding some allocations.
Diffstat (limited to 'src')
-rw-r--r-- | src/render.c | 51 |
1 files changed, 16 insertions, 35 deletions
diff --git a/src/render.c b/src/render.c index 0fec3c4..f4c56ac 100644 --- a/src/render.c +++ b/src/render.c @@ -5,36 +5,6 @@ #include "utf8.h" #include "render.h" -static inline -cmark_render_state -cmark_initialize_render_state(int options, int width, - void (*outc)(cmark_render_state*, - cmark_escaping, - int32_t, - unsigned char)) -{ - cmark_strbuf *pref = (cmark_strbuf*)malloc(sizeof(cmark_strbuf)); - cmark_strbuf *buf = (cmark_strbuf*)malloc(sizeof(cmark_strbuf)); - cmark_strbuf_init(pref, 16); - cmark_strbuf_init(buf, 1024); - cmark_render_state state = { options, buf, pref, 0, width, - 0, 0, 0, true, false, false, outc }; - return state; -} - -static inline -char * -cmark_finalize_render_state(cmark_render_state *state) -{ - char * result; - result = (char *)cmark_strbuf_detach(state->buffer); - cmark_strbuf_free(state->prefix); - cmark_strbuf_free(state->buffer); - free(state->prefix); - free(state->buffer); - return result; -} - void cr(cmark_render_state *state) { if (state->need_cr < 1) { @@ -162,14 +132,21 @@ cmark_render(cmark_node *root, cmark_event_type ev_type, cmark_render_state *state)) { - if (CMARK_OPT_HARDBREAKS & options) { - width = 0; - } - cmark_render_state state = cmark_initialize_render_state(options, width, outc); + cmark_strbuf pref = GH_BUF_INIT; + cmark_strbuf buf = GH_BUF_INIT; cmark_node *cur; cmark_event_type ev_type; + char *result; cmark_iter *iter = cmark_iter_new(root); + + if (CMARK_OPT_HARDBREAKS & options) { + width = 0; + } + + cmark_render_state state = { options, &buf, &pref, 0, width, + 0, 0, 0, true, false, false, outc }; + while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) { cur = cmark_iter_get_node(iter); if (!render_node(cur, ev_type, &state)) { @@ -180,7 +157,11 @@ cmark_render(cmark_node *root, } } + result = (char *)cmark_strbuf_detach(state.buffer); + cmark_iter_free(iter); + cmark_strbuf_free(state.prefix); + cmark_strbuf_free(state.buffer); - return cmark_finalize_render_state(&state); + return result; } |