Skip to content

Commit

Permalink
Enforce const on input document
Browse files Browse the repository at this point in the history
  • Loading branch information
vmg committed Sep 2, 2011
1 parent 6091758 commit b8ed678
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/sundown.c
Expand Up @@ -64,7 +64,7 @@ main(int argc, char **argv)
sdhtml_renderer(&callbacks, &options, 0); sdhtml_renderer(&callbacks, &options, 0);
markdown = sd_markdown_new(0, 16, &callbacks, &options); markdown = sd_markdown_new(0, 16, &callbacks, &options);


sd_markdown_render(ob, ib, markdown); sd_markdown_render(ob, ib->data, ib->size, markdown);
sd_markdown_free(markdown); sd_markdown_free(markdown);


/* writing the result to stdout */ /* writing the result to stdout */
Expand Down
76 changes: 34 additions & 42 deletions src/markdown.c
Expand Up @@ -45,8 +45,8 @@
struct link_ref { struct link_ref {
unsigned int id; unsigned int id;


struct buf link; struct buf *link;
struct buf title; struct buf *title;


struct link_ref *next; struct link_ref *next;
}; };
Expand Down Expand Up @@ -163,7 +163,7 @@ unscape_text(struct buf *ob, struct buf *src)
} }


static unsigned int static unsigned int
hash_link_ref(uint8_t *link_ref, size_t length) hash_link_ref(const uint8_t *link_ref, size_t length)
{ {
size_t i; size_t i;
unsigned int hash = 0; unsigned int hash = 0;
Expand All @@ -174,27 +174,21 @@ hash_link_ref(uint8_t *link_ref, size_t length)
return hash; return hash;
} }


static void add_link_ref( static struct link_ref *
add_link_ref(
struct link_ref **references, struct link_ref **references,
uint8_t *name, size_t name_size, const uint8_t *name, size_t name_size)
uint8_t *link, size_t link_size,
uint8_t *title, size_t title_size)
{ {
struct link_ref *ref = calloc(1, sizeof(struct link_ref)); struct link_ref *ref = calloc(1, sizeof(struct link_ref));


if (!ref) if (!ref)
return; return NULL;


ref->id = hash_link_ref(name, name_size); ref->id = hash_link_ref(name, name_size);
ref->next = references[ref->id % REF_TABLE_SIZE]; ref->next = references[ref->id % REF_TABLE_SIZE];


ref->link.data = link;
ref->link.size = link_size;

ref->title.data = title;
ref->title.size = title_size;

references[ref->id % REF_TABLE_SIZE] = ref; references[ref->id % REF_TABLE_SIZE] = ref;
return ref;
} }


static struct link_ref * static struct link_ref *
Expand Down Expand Up @@ -225,7 +219,11 @@ free_link_refs(struct link_ref **references)
struct link_ref *next; struct link_ref *next;


while (r) { while (r) {
next = r->next; free(r); r = next; next = r->next;
bufrelease(r->link);
bufrelease(r->title);
free(r);
r = next;
} }
} }
} }
Expand Down Expand Up @@ -976,9 +974,8 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset
goto cleanup; goto cleanup;


/* keeping link and title from link_ref */ /* keeping link and title from link_ref */
link = &lr->link; link = lr->link;
if (lr->title.size) title = lr->title;
title = &lr->title;
i++; i++;
} }


Expand Down Expand Up @@ -1012,9 +1009,8 @@ char_link(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t offset
goto cleanup; goto cleanup;


/* keeping link and title from link_ref */ /* keeping link and title from link_ref */
link = &lr->link; link = lr->link;
if (lr->title.size) title = lr->title;
title = &lr->title;


/* rewinding the whitespace */ /* rewinding the whitespace */
i = txt_e + 1; i = txt_e + 1;
Expand Down Expand Up @@ -2100,7 +2096,7 @@ parse_block(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size


/* is_ref • returns whether a line is a reference or not */ /* is_ref • returns whether a line is a reference or not */
static int static int
is_ref(uint8_t *data, size_t beg, size_t end, size_t *last, struct link_ref **refs) is_ref(const uint8_t *data, size_t beg, size_t end, size_t *last, struct link_ref **refs)
{ {
/* int n; */ /* int n; */
size_t i = 0; size_t i = 0;
Expand Down Expand Up @@ -2194,21 +2190,17 @@ is_ref(uint8_t *data, size_t beg, size_t end, size_t *last, struct link_ref **re
*last = line_end; *last = line_end;


if (refs) { if (refs) {
uint8_t *title_str = NULL; struct link_ref *ref;
size_t title_size = 0;
ref = add_link_ref(refs, data + id_offset, id_end - id_offset);

ref->link = bufnew(link_end - link_offset);
bufput(ref->link, data + link_offset, link_end - link_offset);


if (title_end > title_offset) { if (title_end > title_offset) {
title_str = data + title_offset; ref->title = bufnew(title_end - title_offset);
title_size = title_end - title_offset; bufput(ref->title, data + title_offset, title_end - title_offset);
} }

add_link_ref( refs,
data + id_offset, /* identifier */
id_end - id_offset,
data + link_offset, /* link url */
link_end - link_offset,
title_str, /* title (optional) */
title_size);
} }


return 1; return 1;
Expand Down Expand Up @@ -2303,7 +2295,7 @@ sd_markdown_new(
} }


void void
sd_markdown_render(struct buf *ob, const struct buf *ib, struct sd_markdown *md) sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md)
{ {
static const float MARKDOWN_GROW_FACTOR = 1.4f; static const float MARKDOWN_GROW_FACTOR = 1.4f;


Expand All @@ -2315,28 +2307,28 @@ sd_markdown_render(struct buf *ob, const struct buf *ib, struct sd_markdown *md)
return; return;


/* Preallocate enough space for our buffer to avoid expanding while copying */ /* Preallocate enough space for our buffer to avoid expanding while copying */
bufgrow(text, ib->size); bufgrow(text, doc_size);


/* reset the references table */ /* reset the references table */
memset(&md->refs, 0x0, REF_TABLE_SIZE * sizeof(void *)); memset(&md->refs, 0x0, REF_TABLE_SIZE * sizeof(void *));


/* first pass: looking for references, copying everything else */ /* first pass: looking for references, copying everything else */
beg = 0; beg = 0;
while (beg < ib->size) /* iterating over lines */ while (beg < doc_size) /* iterating over lines */
if (is_ref(ib->data, beg, ib->size, &end, md->refs)) if (is_ref(document, beg, doc_size, &end, md->refs))
beg = end; beg = end;
else { /* skipping to the next line */ else { /* skipping to the next line */
end = beg; end = beg;
while (end < ib->size && ib->data[end] != '\n' && ib->data[end] != '\r') while (end < doc_size && document[end] != '\n' && document[end] != '\r')
end++; end++;


/* adding the line body if present */ /* adding the line body if present */
if (end > beg) if (end > beg)
expand_tabs(text, ib->data + beg, end - beg); expand_tabs(text, document + beg, end - beg);


while (end < ib->size && (ib->data[end] == '\n' || ib->data[end] == '\r')) { while (end < doc_size && (document[end] == '\n' || document[end] == '\r')) {
/* add one \n per newline */ /* add one \n per newline */
if (ib->data[end] == '\n' || (end + 1 < ib->size && ib->data[end + 1] != '\n')) if (document[end] == '\n' || (end + 1 < doc_size && document[end + 1] != '\n'))
bufputc(text, '\n'); bufputc(text, '\n');
end++; end++;
} }
Expand Down
4 changes: 2 additions & 2 deletions src/markdown.h
Expand Up @@ -114,10 +114,10 @@ sd_markdown_new(
unsigned int extensions, unsigned int extensions,
size_t max_nesting, size_t max_nesting,
const struct sd_callbacks *callbacks, const struct sd_callbacks *callbacks,
void *opaque); void *opaque);


extern void extern void
sd_markdown_render(struct buf *ob, const struct buf *ib, struct sd_markdown *md); sd_markdown_render(struct buf *ob, const uint8_t *document, size_t doc_size, struct sd_markdown *md);


extern void extern void
sd_markdown_free(struct sd_markdown *md); sd_markdown_free(struct sd_markdown *md);
Expand Down

0 comments on commit b8ed678

Please sign in to comment.