From d6077ea037e23f6e2c0d83dda7b5d7106f48d1a3 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 15 Dec 2014 12:05:04 -0800 Subject: Re-added cmark_ prefix to strbuf and chunk. Reverts 225d720. --- man/man3/cmark.3 | 2 +- src/blocks.c | 86 +++++++++++++++--------------- src/buffer.c | 108 ++++++++++++++++++------------------- src/buffer.h | 82 ++++++++++++++--------------- src/chunk.h | 34 ++++++------ src/houdini.h | 26 ++++----- src/houdini_href_e.c | 12 ++--- src/houdini_html_e.c | 10 ++-- src/houdini_html_u.c | 16 +++--- src/html.c | 94 ++++++++++++++++----------------- src/inlines.c | 146 +++++++++++++++++++++++++-------------------------- src/inlines.h | 6 +-- src/man.c | 72 ++++++++++++------------- src/node.c | 16 +++--- src/node.h | 7 ++- src/parser.h | 4 +- src/print.c | 74 +++++++++++++------------- src/references.c | 16 +++--- src/references.h | 4 +- src/scanners.c | 2 +- src/scanners.h | 2 +- src/utf8.c | 18 +++---- src/utf8.h | 6 +-- 23 files changed, 421 insertions(+), 422 deletions(-) diff --git a/man/man3/cmark.3 b/man/man3/cmark.3 index f250ccb..6a16e35 100644 --- a/man/man3/cmark.3 +++ b/man/man3/cmark.3 @@ -1,4 +1,4 @@ -.TH cmark 3 "December 14, 2014" "LOCAL" "Library Functions Manual" +.TH cmark 3 "December 15, 2014" "LOCAL" "Library Functions Manual" .SH NAME .PP diff --git a/src/blocks.c b/src/blocks.c index 9dece20..fe43055 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -37,7 +37,7 @@ static cmark_node* make_block(cmark_node_type tag, int start_line, int start_col e->start_line = start_line; e->start_column = start_column; e->end_line = start_line; - strbuf_init(&e->string_content, 32); + cmark_strbuf_init(&e->string_content, 32); } return e; @@ -54,10 +54,10 @@ cmark_parser *cmark_parser_new() { cmark_parser *parser = (cmark_parser*)malloc(sizeof(cmark_parser)); cmark_node *document = make_document(); - strbuf *line = (strbuf*)malloc(sizeof(strbuf)); - strbuf *buf = (strbuf*)malloc(sizeof(strbuf)); - strbuf_init(line, 256); - strbuf_init(buf, 0); + cmark_strbuf *line = (cmark_strbuf*)malloc(sizeof(cmark_strbuf)); + cmark_strbuf *buf = (cmark_strbuf*)malloc(sizeof(cmark_strbuf)); + cmark_strbuf_init(line, 256); + cmark_strbuf_init(buf, 0); parser->refmap = cmark_reference_map_new(); parser->root = document; @@ -71,9 +71,9 @@ cmark_parser *cmark_parser_new() void cmark_parser_free(cmark_parser *parser) { - strbuf_free(parser->curline); + cmark_strbuf_free(parser->curline); free(parser->curline); - strbuf_free(parser->linebuf); + cmark_strbuf_free(parser->linebuf); free(parser->linebuf); cmark_reference_map_free(parser->refmap); free(parser); @@ -83,7 +83,7 @@ static cmark_node* finalize(cmark_parser *parser, cmark_node* b, int line_number); // Returns true if line has only space characters, else false. -static bool is_blank(strbuf *s, int offset) +static bool is_blank(cmark_strbuf *s, int offset) { while (offset < s->size) { switch (s->ptr[offset]) { @@ -115,13 +115,13 @@ static inline bool accepts_lines(cmark_node_type block_type) block_type == NODE_CODE_BLOCK); } -static void add_line(cmark_node* cmark_node, chunk *ch, int offset) +static void add_line(cmark_node* cmark_node, cmark_chunk *ch, int offset) { assert(cmark_node->open); - strbuf_put(&cmark_node->string_content, ch->data + offset, ch->len - offset); + cmark_strbuf_put(&cmark_node->string_content, ch->data + offset, ch->len - offset); } -static void remove_trailing_blank_lines(strbuf *ln) +static void remove_trailing_blank_lines(cmark_strbuf *ln) { int i; @@ -133,13 +133,13 @@ static void remove_trailing_blank_lines(strbuf *ln) } if (i < 0) { - strbuf_clear(ln); + cmark_strbuf_clear(ln); return; } - i = strbuf_strchr(ln, '\n', i); + i = cmark_strbuf_strchr(ln, '\n', i); if (i >= 0) - strbuf_truncate(ln, i); + cmark_strbuf_truncate(ln, i); } // Check to see if a cmark_node ends with a blank line, descending @@ -200,10 +200,10 @@ finalize(cmark_parser *parser, cmark_node* b, int line_number) switch (b->type) { case NODE_PARAGRAPH: - while (strbuf_at(&b->string_content, 0) == '[' && + while (cmark_strbuf_at(&b->string_content, 0) == '[' && (pos = cmark_parse_reference_inline(&b->string_content, parser->refmap))) { - strbuf_drop(&b->string_content, pos); + cmark_strbuf_drop(&b->string_content, pos); } if (is_blank(&b->string_content, 0)) { // remove blank node (former reference def) @@ -214,29 +214,29 @@ finalize(cmark_parser *parser, cmark_node* b, int line_number) case NODE_CODE_BLOCK: if (!b->as.code.fenced) { // indented code remove_trailing_blank_lines(&b->string_content); - strbuf_putc(&b->string_content, '\n'); + cmark_strbuf_putc(&b->string_content, '\n'); } else { // first line of contents becomes info - firstlinelen = strbuf_strchr(&b->string_content, '\n', 0); + firstlinelen = cmark_strbuf_strchr(&b->string_content, '\n', 0); - strbuf tmp = GH_BUF_INIT; + cmark_strbuf tmp = GH_BUF_INIT; houdini_unescape_html_f( &tmp, b->string_content.ptr, firstlinelen ); - strbuf_trim(&tmp); - strbuf_unescape(&tmp); - b->as.code.info = chunk_buf_detach(&tmp); + cmark_strbuf_trim(&tmp); + cmark_strbuf_unescape(&tmp); + b->as.code.info = cmark_chunk_buf_detach(&tmp); - strbuf_drop(&b->string_content, firstlinelen + 1); + cmark_strbuf_drop(&b->string_content, firstlinelen + 1); } - b->as.literal = chunk_buf_detach(&b->string_content); + b->as.literal = cmark_chunk_buf_detach(&b->string_content); break; case NODE_HTML: - b->as.literal = chunk_buf_detach(&b->string_content); + b->as.literal = cmark_chunk_buf_detach(&b->string_content); break; case NODE_LIST: // determine tight/loose status @@ -325,7 +325,7 @@ static void process_inlines(cmark_node* root, cmark_reference_map *refmap) // Attempts to parse a list item marker (bullet or enumerated). // On success, returns length of the marker, and populates // data with the details. On failure, returns 0. -static int parse_list_marker(chunk *input, int pos, cmark_list **dataptr) +static int parse_list_marker(cmark_chunk *input, int pos, cmark_list **dataptr) { unsigned char c; int startpos; @@ -466,15 +466,15 @@ S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len, line_len = end - buffer; } else { - strbuf_put(parser->linebuf, buffer, end - buffer); + cmark_strbuf_put(parser->linebuf, buffer, end - buffer); break; } if (parser->linebuf->size > 0) { - strbuf_put(parser->linebuf, buffer, line_len); + cmark_strbuf_put(parser->linebuf, buffer, line_len); S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size); - strbuf_clear(parser->linebuf); + cmark_strbuf_clear(parser->linebuf); } else { S_process_line(parser, buffer, line_len); @@ -484,11 +484,11 @@ S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len, } } -static void chop_trailing_hashtags(chunk *ch) +static void chop_trailing_hashtags(cmark_chunk *ch) { int n, orig_n; - chunk_rtrim(ch); + cmark_chunk_rtrim(ch); orig_n = n = ch->len - 1; // if string ends in space followed by #s, remove these: @@ -498,7 +498,7 @@ static void chop_trailing_hashtags(chunk *ch) // Check for a be a space before the final #s: if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') { ch->len = n; - chunk_rtrim(ch); + cmark_chunk_rtrim(ch); } } @@ -517,14 +517,14 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) bool blank = false; int first_nonspace; int indent; - chunk input; + cmark_chunk input; utf8proc_detab(parser->curline, buffer, bytes); // Add a newline to the end if not present: // TODO this breaks abstraction: if (parser->curline->ptr[parser->curline->size - 1] != '\n') { - strbuf_putc(parser->curline, '\n'); + cmark_strbuf_putc(parser->curline, '\n'); } input.data = parser->curline->ptr; input.len = parser->curline->size; @@ -645,7 +645,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) container->as.code.fence_char = 0; container->as.code.fence_length = 0; container->as.code.fence_offset = 0; - container->as.code.info = chunk_literal(""); + container->as.code.info = cmark_chunk_literal(""); } else { // indent > 4 in lazy line break; } @@ -663,7 +663,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) offset = first_nonspace + matched; container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1); - int hashpos = chunk_strchr(&input, '#', first_nonspace); + int hashpos = cmark_chunk_strchr(&input, '#', first_nonspace); int level = 0; while (peek_at(&input, hashpos) == '#') { @@ -680,7 +680,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) container->as.code.fence_char = peek_at(&input, first_nonspace); container->as.code.fence_length = matched; container->as.code.fence_offset = first_nonspace - offset; - container->as.code.info = chunk_literal(""); + container->as.code.info = cmark_chunk_literal(""); offset = first_nonspace + matched; } else if ((matched = scan_html_block_tag(&input, first_nonspace))) { @@ -691,8 +691,8 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) } else if (container->type == NODE_PARAGRAPH && (lev = scan_setext_header_line(&input, first_nonspace)) && // check that there is only one line in the paragraph: - strbuf_strrchr(&container->string_content, '\n', - strbuf_len(&container->string_content) - 2) < 0) { + cmark_strbuf_strrchr(&container->string_content, '\n', + cmark_strbuf_len(&container->string_content) - 2) < 0) { container->type = NODE_HEADER; container->as.header.level = lev; @@ -789,7 +789,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) container == last_matched_container && !blank && cur->type == NODE_PARAGRAPH && - strbuf_len(&cur->string_content) > 0) { + cmark_strbuf_len(&cur->string_content) > 0) { add_line(cur, &input, offset); @@ -857,7 +857,7 @@ S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes) parser->current = container; } - strbuf_clear(parser->curline); + cmark_strbuf_clear(parser->curline); } @@ -866,11 +866,11 @@ cmark_node *cmark_parser_finish(cmark_parser *parser) if (parser->linebuf->size) { S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size); - strbuf_clear(parser->linebuf); + cmark_strbuf_clear(parser->linebuf); } finalize_document(parser); - strbuf_free(parser->curline); + cmark_strbuf_free(parser->curline); #if CMARK_DEBUG_NODES if (cmark_node_check(parser->root, stderr)) { abort(); diff --git a/src/buffer.c b/src/buffer.c index 765cb46..73a9728 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -8,36 +8,36 @@ #include "buffer.h" -/* Used as default value for strbuf->ptr so that people can always - * assume ptr is non-NULL and zero terminated even for new strbufs. +/* Used as default value for cmark_strbuf->ptr so that people can always + * assume ptr is non-NULL and zero terminated even for new cmark_strbufs. */ -unsigned char strbuf__initbuf[1]; -unsigned char strbuf__oom[1]; +unsigned char cmark_strbuf__initbuf[1]; +unsigned char cmark_strbuf__oom[1]; #define ENSURE_SIZE(b, d) \ - if ((d) > buf->asize && strbuf_grow(b, (d)) < 0) \ + if ((d) > buf->asize && cmark_strbuf_grow(b, (d)) < 0) \ return -1; #ifndef MIN #define MIN(x,y) ((xasize = 0; buf->size = 0; - buf->ptr = strbuf__initbuf; + buf->ptr = cmark_strbuf__initbuf; if (initial_size) - strbuf_grow(buf, initial_size); + cmark_strbuf_grow(buf, initial_size); } -int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom) +int cmark_strbuf_try_grow(cmark_strbuf *buf, int target_size, bool mark_oom) { unsigned char *new_ptr; int new_size; - if (buf->ptr == strbuf__oom) + if (buf->ptr == cmark_strbuf__oom) return -1; if (target_size <= buf->asize) @@ -63,7 +63,7 @@ int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom) if (!new_ptr) { if (mark_oom) - buf->ptr = strbuf__oom; + buf->ptr = cmark_strbuf__oom; return -1; } @@ -78,32 +78,32 @@ int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom) return 0; } -int strbuf_grow(strbuf *buf, int target_size) +int cmark_strbuf_grow(cmark_strbuf *buf, int target_size) { - return strbuf_try_grow(buf, target_size, true); + return cmark_strbuf_try_grow(buf, target_size, true); } -bool strbuf_oom(const strbuf *buf) +bool cmark_strbuf_oom(const cmark_strbuf *buf) { - return (buf->ptr == strbuf__oom); + return (buf->ptr == cmark_strbuf__oom); } -size_t strbuf_len(const strbuf *buf) +size_t cmark_strbuf_len(const cmark_strbuf *buf) { return buf->size; } -void strbuf_free(strbuf *buf) +void cmark_strbuf_free(cmark_strbuf *buf) { if (!buf) return; - if (buf->ptr != strbuf__initbuf && buf->ptr != strbuf__oom) + if (buf->ptr != cmark_strbuf__initbuf && buf->ptr != cmark_strbuf__oom) free(buf->ptr); - strbuf_init(buf, 0); + cmark_strbuf_init(buf, 0); } -void strbuf_clear(strbuf *buf) +void cmark_strbuf_clear(cmark_strbuf *buf) { buf->size = 0; @@ -111,10 +111,10 @@ void strbuf_clear(strbuf *buf) buf->ptr[0] = '\0'; } -int strbuf_set(strbuf *buf, const unsigned char *data, int len) +int cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, int len) { if (len <= 0 || data == NULL) { - strbuf_clear(buf); + cmark_strbuf_clear(buf); } else { if (data != buf->ptr) { ENSURE_SIZE(buf, len + 1); @@ -126,14 +126,14 @@ int strbuf_set(strbuf *buf, const unsigned char *data, int len) return 0; } -int strbuf_sets(strbuf *buf, const char *string) +int cmark_strbuf_sets(cmark_strbuf *buf, const char *string) { - return strbuf_set(buf, + return cmark_strbuf_set(buf, (const unsigned char *)string, string ? strlen(string) : 0); } -int strbuf_putc(strbuf *buf, int c) +int cmark_strbuf_putc(cmark_strbuf *buf, int c) { ENSURE_SIZE(buf, buf->size + 2); buf->ptr[buf->size++] = c; @@ -141,7 +141,7 @@ int strbuf_putc(strbuf *buf, int c) return 0; } -int strbuf_put(strbuf *buf, const unsigned char *data, int len) +int cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, int len) { if (len <= 0) return 0; @@ -153,12 +153,12 @@ int strbuf_put(strbuf *buf, const unsigned char *data, int len) return 0; } -int strbuf_puts(strbuf *buf, const char *string) +int cmark_strbuf_puts(cmark_strbuf *buf, const char *string) { - return strbuf_put(buf, (const unsigned char *)string, strlen(string)); + return cmark_strbuf_put(buf, (const unsigned char *)string, strlen(string)); } -int strbuf_vprintf(strbuf *buf, const char *format, va_list ap) +int cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap) { const int expected_size = buf->size + (strlen(format) * 2); int len; @@ -174,7 +174,7 @@ int strbuf_vprintf(strbuf *buf, const char *format, va_list ap) if (len < 0) { free(buf->ptr); - buf->ptr = strbuf__oom; + buf->ptr = cmark_strbuf__oom; return -1; } @@ -189,19 +189,19 @@ int strbuf_vprintf(strbuf *buf, const char *format, va_list ap) return 0; } -int strbuf_printf(strbuf *buf, const char *format, ...) +int cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...) { int r; va_list ap; va_start(ap, format); - r = strbuf_vprintf(buf, format, ap); + r = cmark_strbuf_vprintf(buf, format, ap); va_end(ap); return r; } -void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf) +void cmark_strbuf_copy_cstr(char *data, int datasize, const cmark_strbuf *buf) { int copylen; @@ -219,29 +219,29 @@ void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf) data[copylen] = '\0'; } -void strbuf_swap(strbuf *buf_a, strbuf *buf_b) +void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b) { - strbuf t = *buf_a; + cmark_strbuf t = *buf_a; *buf_a = *buf_b; *buf_b = t; } -unsigned char *strbuf_detach(strbuf *buf) +unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) { unsigned char *data = buf->ptr; - if (buf->asize == 0 || buf->ptr == strbuf__oom) { + if (buf->asize == 0 || buf->ptr == cmark_strbuf__oom) { /* return an empty string */ return (unsigned char *)calloc(1, 1); } - strbuf_init(buf, 0); + cmark_strbuf_init(buf, 0); return data; } -void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize) +void cmark_strbuf_attach(cmark_strbuf *buf, unsigned char *ptr, int asize) { - strbuf_free(buf); + cmark_strbuf_free(buf); if (ptr) { buf->ptr = ptr; @@ -251,18 +251,18 @@ void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize) else /* pass 0 to fall back on strlen + 1 */ buf->asize = buf->size + 1; } else { - strbuf_grow(buf, asize); + cmark_strbuf_grow(buf, asize); } } -int strbuf_cmp(const strbuf *a, const strbuf *b) +int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b) { int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size)); return (result != 0) ? result : (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; } -int strbuf_strchr(const strbuf *buf, int c, int pos) +int cmark_strbuf_strchr(const cmark_strbuf *buf, int c, int pos) { const unsigned char *p = (unsigned char *)memchr(buf->ptr + pos, c, buf->size - pos); if (!p) @@ -271,7 +271,7 @@ int strbuf_strchr(const strbuf *buf, int c, int pos) return (int)(p - (const unsigned char *)buf->ptr); } -int strbuf_strrchr(const strbuf *buf, int c, int pos) +int cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, int pos) { int i; @@ -283,7 +283,7 @@ int strbuf_strrchr(const strbuf *buf, int c, int pos) return -1; } -void strbuf_truncate(strbuf *buf, int len) +void cmark_strbuf_truncate(cmark_strbuf *buf, int len) { if (len < buf->size) { buf->size = len; @@ -291,7 +291,7 @@ void strbuf_truncate(strbuf *buf, int len) } } -void strbuf_drop(strbuf *buf, int n) +void cmark_strbuf_drop(cmark_strbuf *buf, int n) { if (n > 0) { buf->size = buf->size - n; @@ -302,7 +302,7 @@ void strbuf_drop(strbuf *buf, int n) } } -void strbuf_rtrim(strbuf *buf) +void cmark_strbuf_rtrim(cmark_strbuf *buf) { if (!buf->size) return; @@ -317,7 +317,7 @@ void strbuf_rtrim(strbuf *buf) buf->ptr[buf->size] = '\0'; } -void strbuf_trim(strbuf *buf) +void cmark_strbuf_trim(cmark_strbuf *buf) { int i = 0; @@ -327,14 +327,14 @@ void strbuf_trim(strbuf *buf) while (i < buf->size && isspace(buf->ptr[i])) i++; - strbuf_drop(buf, i); + cmark_strbuf_drop(buf, i); - strbuf_rtrim(buf); + cmark_strbuf_rtrim(buf); } // Destructively modify string, collapsing consecutive // space and newline characters into a single space. -void strbuf_normalize_whitespace(strbuf *s) +void cmark_strbuf_normalize_whitespace(cmark_strbuf *s) { bool last_char_was_space = false; int r, w; @@ -356,11 +356,11 @@ void strbuf_normalize_whitespace(strbuf *s) } } - strbuf_truncate(s, w); + cmark_strbuf_truncate(s, w); } // Destructively unescape a string: remove backslashes before punctuation chars. -extern void strbuf_unescape(strbuf *buf) +extern void cmark_strbuf_unescape(cmark_strbuf *buf) { int r, w; @@ -371,5 +371,5 @@ extern void strbuf_unescape(strbuf *buf) buf->ptr[w++] = buf->ptr[r]; } - strbuf_truncate(buf, w); + cmark_strbuf_truncate(buf, w); } diff --git a/src/buffer.h b/src/buffer.h index c827ea8..875cf8c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -12,21 +12,21 @@ extern "C" { typedef struct { unsigned char *ptr; int asize, size; -} strbuf; +} cmark_strbuf; -extern unsigned char strbuf__initbuf[]; +extern unsigned char cmark_strbuf__initbuf[]; -extern unsigned char strbuf__oom[]; +extern unsigned char cmark_strbuf__oom[]; -#define GH_BUF_INIT { strbuf__initbuf, 0, 0 } +#define GH_BUF_INIT { cmark_strbuf__initbuf, 0, 0 } /** - * Initialize a strbuf structure. + * Initialize a cmark_strbuf structure. * * For the cases where GH_BUF_INIT cannot be used to do static * initialization. */ -void strbuf_init(strbuf *buf, int initial_size); +void cmark_strbuf_init(cmark_strbuf *buf, int initial_size); /** * Attempt to grow the buffer to hold at least `target_size` bytes. @@ -36,7 +36,7 @@ void strbuf_init(strbuf *buf, int initial_size); * existing buffer content will be preserved, but calling code must handle * that buffer was not expanded. */ -int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom); +int cmark_strbuf_try_grow(cmark_strbuf *buf, int target_size, bool mark_oom); /** * Grow the buffer to hold at least `target_size` bytes. @@ -46,65 +46,65 @@ int strbuf_try_grow(strbuf *buf, int target_size, bool mark_oom); * * @return 0 on success or -1 on failure */ -int strbuf_grow(strbuf *buf, int target_size); +int cmark_strbuf_grow(cmark_strbuf *buf, int target_size); -void strbuf_free(strbuf *buf); -void strbuf_swap(strbuf *buf_a, strbuf *buf_b); +void cmark_strbuf_free(cmark_strbuf *buf); +void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b); /** - * Test if there have been any reallocation failures with this strbuf. + * Test if there have been any reallocation failures with this cmark_strbuf. * - * Any function that writes to a strbuf can fail due to memory allocation - * issues. If one fails, the strbuf will be marked with an OOM error and - * further calls to modify the buffer will fail. Check strbuf_oom() at the + * Any function that writes to a cmark_strbuf can fail due to memory allocation + * issues. If one fails, the cmark_strbuf will be marked with an OOM error and + * further calls to modify the buffer will fail. Check cmark_strbuf_oom() at the * end of your sequence and it will be true if you ran out of memory at any * point with that buffer. * * @return false if no error, true if allocation error */ -bool strbuf_oom(const strbuf *buf); +bool cmark_strbuf_oom(const cmark_strbuf *buf); -size_t strbuf_len(const strbuf *buf); +size_t cmark_strbuf_len(const cmark_strbuf *buf); -int strbuf_cmp(const strbuf *a, const strbuf *b); +int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b); -void strbuf_attach(strbuf *buf, unsigned char *ptr, int asize); -unsigned char *strbuf_detach(strbuf *buf); -void strbuf_copy_cstr(char *data, int datasize, const strbuf *buf); +void cmark_strbuf_attach(cmark_strbuf *buf, unsigned char *ptr, int asize); +unsigned char *cmark_strbuf_detach(cmark_strbuf *buf); +void cmark_strbuf_copy_cstr(char *data, int datasize, const cmark_strbuf *buf); -static inline const char *strbuf_cstr(const strbuf *buf) +static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf) { return (char *)buf->ptr; } -#define strbuf_at(buf, n) ((buf)->ptr[n]) +#define cmark_strbuf_at(buf, n) ((buf)->ptr[n]) /* * Functions below that return int value error codes will return 0 on * success or -1 on failure (which generally means an allocation failed). - * Using a strbuf where the allocation has failed with result in -1 from + * Using a cmark_strbuf where the allocation has failed with result in -1 from * all further calls using that buffer. As a result, you can ignore the * return code of these functions and call them in a series then just call - * strbuf_oom at the end. + * cmark_strbuf_oom at the end. */ -int strbuf_set(strbuf *buf, const unsigned char *data, int len); -int strbuf_sets(strbuf *buf, const char *string); -int strbuf_putc(strbuf *buf, int c); -int strbuf_put(strbuf *buf, const unsigned char *data, int len); -int strbuf_puts(strbuf *buf, const char *string); -int strbuf_printf(strbuf *buf, const char *format, ...) +int cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, int len); +int cmark_strbuf_sets(cmark_strbuf *buf, const char *string); +int cmark_strbuf_putc(cmark_strbuf *buf, int c); +int cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, int len); +int cmark_strbuf_puts(cmark_strbuf *buf, const char *string); +int cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...) CMARK_ATTRIBUTE((format (printf, 2, 3))); -int strbuf_vprintf(strbuf *buf, const char *format, va_list ap); -void strbuf_clear(strbuf *buf); - -int strbuf_strchr(const strbuf *buf, int c, int pos); -int strbuf_strrchr(const strbuf *buf, int c, int pos); -void strbuf_drop(strbuf *buf, int n); -void strbuf_truncate(strbuf *buf, int len); -void strbuf_rtrim(strbuf *buf); -void strbuf_trim(strbuf *buf); -void strbuf_normalize_whitespace(strbuf *s); -void strbuf_unescape(strbuf *s); +int cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap); +void cmark_strbuf_clear(cmark_strbuf *buf); + +int cmark_strbuf_strchr(const cmark_strbuf *buf, int c, int pos); +int cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, int pos); +void cmark_strbuf_drop(cmark_strbuf *buf, int n); +void cmark_strbuf_truncate(cmark_strbuf *buf, int len); +void cmark_strbuf_rtrim(cmark_strbuf *buf); +void cmark_strbuf_trim(cmark_strbuf *buf); +void cmark_strbuf_normalize_whitespace(cmark_strbuf *s); +void cmark_strbuf_unescape(cmark_strbuf *s); #ifdef __cplusplus } diff --git a/src/chunk.h b/src/chunk.h index b29c1b9..0f48791 100644 --- a/src/chunk.h +++ b/src/chunk.h @@ -11,9 +11,9 @@ typedef struct { unsigned char *data; int len; int alloc; // also implies a NULL-terminated string -} chunk; +} cmark_chunk; -static inline void chunk_free(chunk *c) +static inline void cmark_chunk_free(cmark_chunk *c) { if (c->alloc) free(c->data); @@ -23,7 +23,7 @@ static inline void chunk_free(chunk *c) c->len = 0; } -static inline void chunk_ltrim(chunk *c) +static inline void cmark_chunk_ltrim(cmark_chunk *c) { assert(!c->alloc); @@ -33,7 +33,7 @@ static inline void chunk_ltrim(chunk *c) } } -static inline void chunk_rtrim(chunk *c) +static inline void cmark_chunk_rtrim(cmark_chunk *c) { while (c->len > 0) { if (!isspace(c->data[c->len - 1])) @@ -43,19 +43,19 @@ static inline void chunk_rtrim(chunk *c) } } -static inline void chunk_trim(chunk *c) +static inline void cmark_chunk_trim(cmark_chunk *c) { - chunk_ltrim(c); - chunk_rtrim(c); + cmark_chunk_ltrim(c); + cmark_chunk_rtrim(c); } -static inline int chunk_strchr(chunk *ch, int c, int offset) +static inline int cmark_chunk_strchr(cmark_chunk *ch, int c, int offset) { const unsigned char *p = (unsigned char *)memchr(ch->data + offset, c, ch->len - offset); return p ? (int)(p - ch->data) : ch->len; } -static inline const char *chunk_to_cstr(chunk *c) +static inline const char *cmark_chunk_to_cstr(cmark_chunk *c) { unsigned char *str; @@ -73,7 +73,7 @@ static inline const char *chunk_to_cstr(chunk *c) return (char *)str; } -static inline void chunk_set_cstr(chunk *c, const char *str) +static inline void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) { if (c->alloc) { free(c->data); @@ -84,24 +84,24 @@ static inline void chunk_set_cstr(chunk *c, const char *str) memcpy(c->data, str, c->len + 1); } -static inline chunk chunk_literal(const char *data) +static inline cmark_chunk cmark_chunk_literal(const char *data) { - chunk c = {(unsigned char *)data, data ? strlen(data) : 0, 0}; + cmark_chunk c = {(unsigned char *)data, data ? strlen(data) : 0, 0}; return c; } -static inline chunk chunk_dup(const chunk *ch, int pos, int len) +static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, int pos, int len) { - chunk c = {ch->data + pos, len, 0}; + cmark_chunk c = {ch->data + pos, len, 0}; return c; } -static inline chunk chunk_buf_detach(strbuf *buf) +static inline cmark_chunk cmark_chunk_buf_detach(cmark_strbuf *buf) { - chunk c; + cmark_chunk c; c.len = buf->size; - c.data = strbuf_detach(buf); + c.data = cmark_strbuf_detach(buf); c.alloc = 1; return c; diff --git a/src/houdini.h b/src/houdini.h index 9e1200e..9f00f6d 100644 --- a/src/houdini.h +++ b/src/houdini.h @@ -31,19 +31,19 @@ extern "C" { #define HOUDINI_ESCAPED_SIZE(x) (((x) * 12) / 10) #define HOUDINI_UNESCAPED_SIZE(x) (x) -extern size_t houdini_unescape_ent(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_html(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_html0(strbuf *ob, const uint8_t *src, size_t size, int secure); -extern int houdini_unescape_html(strbuf *ob, const uint8_t *src, size_t size); -extern void houdini_unescape_html_f(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_xml(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_uri(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_url(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_unescape_uri(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_unescape_url(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_escape_js(strbuf *ob, const uint8_t *src, size_t size); -extern int houdini_unescape_js(strbuf *ob, const uint8_t *src, size_t size); +extern size_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_html(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_html0(cmark_strbuf *ob, const uint8_t *src, size_t size, int secure); +extern int houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_xml(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_uri(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_url(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_href(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_unescape_uri(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_unescape_url(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_escape_js(cmark_strbuf *ob, const uint8_t *src, size_t size); +extern int houdini_unescape_js(cmark_strbuf *ob, const uint8_t *src, size_t size); #ifdef __cplusplus } diff --git a/src/houdini_href_e.c b/src/houdini_href_e.c index 1c99432..c8949d4 100644 --- a/src/houdini_href_e.c +++ b/src/houdini_href_e.c @@ -49,7 +49,7 @@ static const char HREF_SAFE[] = { }; int -houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size) +houdini_escape_href(cmark_strbuf *ob, const uint8_t *src, size_t size) { static const uint8_t hex_chars[] = "0123456789ABCDEF"; size_t i = 0, org; @@ -63,7 +63,7 @@ houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size) i++; if (likely(i > org)) - strbuf_put(ob, src + org, i - org); + cmark_strbuf_put(ob, src + org, i - org); /* escaping */ if (i >= size) @@ -73,14 +73,14 @@ houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size) /* amp appears all the time in URLs, but needs * HTML-entity escaping to be inside an href */ case '&': - strbuf_puts(ob, "&"); + cmark_strbuf_puts(ob, "&"); break; /* the single quote is a valid URL character * according to the standard; it needs HTML * entity escaping too */ case '\'': - strbuf_puts(ob, "'"); + cmark_strbuf_puts(ob, "'"); break; /* the space can be escaped to %20 or a plus @@ -89,7 +89,7 @@ houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size) * when building GET strings */ #if 0 case ' ': - strbuf_putc(ob, '+'); + cmark_strbuf_putc(ob, '+'); break; #endif @@ -97,7 +97,7 @@ houdini_escape_href(strbuf *ob, const uint8_t *src, size_t size) default: hex_str[1] = hex_chars[(src[i] >> 4) & 0xF]; hex_str[2] = hex_chars[src[i] & 0xF]; - strbuf_put(ob, hex_str, 3); + cmark_strbuf_put(ob, hex_str, 3); } i++; diff --git a/src/houdini_html_e.c b/src/houdini_html_e.c index db5034b..4e523f5 100644 --- a/src/houdini_html_e.c +++ b/src/houdini_html_e.c @@ -45,7 +45,7 @@ static const char *HTML_ESCAPES[] = { }; int -houdini_escape_html0(strbuf *ob, const uint8_t *src, size_t size, int secure) +houdini_escape_html0(cmark_strbuf *ob, const uint8_t *src, size_t size, int secure) { size_t i = 0, org, esc = 0; @@ -55,7 +55,7 @@ houdini_escape_html0(strbuf *ob, const uint8_t *src, size_t size, int secure) i++; if (i > org) - strbuf_put(ob, src + org, i - org); + cmark_strbuf_put(ob, src + org, i - org); /* escaping */ if (unlikely(i >= size)) @@ -63,9 +63,9 @@ houdini_escape_html0(strbuf *ob, const uint8_t *src, size_t size, int secure) /* The forward slash is only escaped in secure mode */ if ((src[i] == '/' || src[i] == '\'') && !secure) { - strbuf_putc(ob, src[i]); + cmark_strbuf_putc(ob, src[i]); } else { - strbuf_puts(ob, HTML_ESCAPES[esc]); + cmark_strbuf_puts(ob, HTML_ESCAPES[esc]); } i++; @@ -75,7 +75,7 @@ houdini_escape_html0(strbuf *ob, const uint8_t *src, size_t size, int secure) } int -houdini_escape_html(strbuf *ob, const uint8_t *src, size_t size) +houdini_escape_html(cmark_strbuf *ob, const uint8_t *src, size_t size) { return houdini_escape_html0(ob, src, size, 1); } diff --git a/src/houdini_html_u.c b/src/houdini_html_u.c index b88b9d1..9a2e262 100644 --- a/src/houdini_html_u.c +++ b/src/houdini_html_u.c @@ -8,7 +8,7 @@ #include "html_unescape.h" size_t -houdini_unescape_ent(strbuf *ob, const uint8_t *src, size_t size) +houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src, size_t size) { size_t i = 0; @@ -55,7 +55,7 @@ houdini_unescape_ent(strbuf *ob, const uint8_t *src, size_t size) const struct html_ent *entity = find_entity((char *)src, i); if (entity != NULL) { - strbuf_put(ob, entity->utf8, entity->utf8_len); + cmark_strbuf_put(ob, entity->utf8, entity->utf8_len); return i + 1; } @@ -68,7 +68,7 @@ houdini_unescape_ent(strbuf *ob, const uint8_t *src, size_t size) } int -houdini_unescape_html(strbuf *ob, const uint8_t *src, size_t size) +houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src, size_t size) { size_t i = 0, org, ent; @@ -82,10 +82,10 @@ houdini_unescape_html(strbuf *ob, const uint8_t *src, size_t size) if (i >= size) return 0; - strbuf_grow(ob, HOUDINI_UNESCAPED_SIZE(size)); + cmark_strbuf_grow(ob, HOUDINI_UNESCAPED_SIZE(size)); } - strbuf_put(ob, src + org, i - org); + cmark_strbuf_put(ob, src + org, i - org); } /* escaping */ @@ -99,14 +99,14 @@ houdini_unescape_html(strbuf *ob, const uint8_t *src, size_t size) /* not really an entity */ if (ent == 0) - strbuf_putc(ob, '&'); + cmark_strbuf_putc(ob, '&'); } return 1; } -void houdini_unescape_html_f(strbuf *ob, const uint8_t *src, size_t size) +void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src, size_t size) { if (!houdini_unescape_html(ob, src, size)) - strbuf_put(ob, src, size); + cmark_strbuf_put(ob, src, size); } diff --git a/src/html.c b/src/html.c index 34552bf..b007d67 100644 --- a/src/html.c +++ b/src/html.c @@ -11,7 +11,7 @@ // Functions to convert cmark_nodes to HTML strings. -static void escape_html(strbuf *dest, const unsigned char *source, int length) +static void escape_html(cmark_strbuf *dest, const unsigned char *source, int length) { if (length < 0) length = strlen((char *)source); @@ -19,7 +19,7 @@ static void escape_html(strbuf *dest, const unsigned char *source, int length) houdini_escape_html0(dest, source, (size_t)length, 0); } -static void escape_href(strbuf *dest, const unsigned char *source, int length) +static void escape_href(cmark_strbuf *dest, const unsigned char *source, int length) { if (length < 0) length = strlen((char *)source); @@ -27,14 +27,14 @@ static void escape_href(strbuf *dest, const unsigned char *source, int length) houdini_escape_href(dest, source, (size_t)length); } -static inline void cr(strbuf *html) +static inline void cr(cmark_strbuf *html) { if (html->size && html->ptr[html->size - 1] != '\n') - strbuf_putc(html, '\n'); + cmark_strbuf_putc(html, '\n'); } struct render_state { - strbuf* html; + cmark_strbuf* html; cmark_node *plain; }; @@ -44,7 +44,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate) struct render_state *state = vstate; cmark_node *parent; cmark_node *grandparent; - strbuf *html = state->html; + cmark_strbuf *html = state->html; char start_header[] = ""; char end_header[] = ""; bool tight; @@ -66,7 +66,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate) case CMARK_NODE_LINEBREAK: case CMARK_NODE_SOFTBREAK: - strbuf_putc(html, ' '); + cmark_strbuf_putc(html, ' '); break; default: @@ -82,10 +82,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate) case CMARK_NODE_BLOCK_QUOTE: if (entering) { cr(html); - strbuf_puts(html, "
\n"); + cmark_strbuf_puts(html, "
\n"); } else { cr(html); - strbuf_puts(html, "
\n"); + cmark_strbuf_puts(html, "
\n"); } break; @@ -96,17 +96,17 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate) if (entering) { cr(html); if (list_type == CMARK_BULLET_LIST) { - strbuf_puts(html, "