Skip to content

Commit

Permalink
Merge pull request #8 from erikdesjardins/remove-printf
Browse files Browse the repository at this point in the history
Remove usage of vsnprintf
  • Loading branch information
erikdesjardins authored Nov 21, 2017
2 parents 2510c38 + 89ca2d4 commit 8f87f48
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 56 deletions.
24 changes: 18 additions & 6 deletions html/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,25 @@ rndr_header(struct buf *ob, const struct buf *text, int level, void *opaque)
bufputc(ob, '\n');

if (options->flags & HTML_TOC) {
bufprintf(ob, "<h%d id=\"", level);
BUFPUTSL(ob, "<h");
bufputi(ob, level);
BUFPUTSL(ob, " id=\"");
if (options->toc_id_prefix) {
bufputs(ob, options->toc_id_prefix);
}
bufprintf(ob, "toc_%d\">", options->toc_data.header_count++);
BUFPUTSL(ob, "toc_");
bufputi(ob, options->toc_data.header_count++);
BUFPUTSL(ob, "\">");
} else {
bufprintf(ob, "<h%d>", level);
BUFPUTSL(ob, "<h");
bufputi(ob, level);
BUFPUTSL(ob, ">");
}

if (text) bufput(ob, text->data, text->size);
bufprintf(ob, "</h%d>\n", level);
BUFPUTSL(ob, "</h");
bufputi(ob, level);
BUFPUTSL(ob, ">\n");
}

static int
Expand Down Expand Up @@ -574,7 +582,9 @@ rndr_tablecell(struct buf *ob, const struct buf *text, int flags, void *opaque,
}

if (col_span > 1) {
bufprintf(ob, " colspan=\"%d\" ", col_span);
BUFPUTSL(ob, " colspan=\"");
bufputi(ob, col_span);
BUFPUTSL(ob, "\" ");
}

switch (flags & MKD_TABLE_ALIGNMASK) {
Expand Down Expand Up @@ -656,7 +666,9 @@ toc_header(struct buf *ob, const struct buf *text, int level, void *opaque)
bufputs(ob, options->toc_id_prefix);
}

bufprintf(ob, "toc_%d\">", options->toc_data.header_count++);
BUFPUTSL(ob, "toc_");
bufputi(ob, options->toc_data.header_count++);
BUFPUTSL(ob, "\">");
if (text)
escape_html(ob, text->data, text->size);
BUFPUTSL(ob, "</a>\n");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snudown-js",
"version": "2.0.2",
"version": "2.0.3",
"description": "a 'native' port of Snudown to JavaScript",
"main": "dist/snudown.js",
"repository": {
Expand Down
81 changes: 35 additions & 46 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
#include <string.h>
#include <assert.h>

/* MSVC compat */
#if defined(_MSC_VER)
# define _buf_vsnprintf _vsnprintf
#else
# define _buf_vsnprintf vsnprintf
#endif

int
bufprefix(const struct buf *buf, const char *prefix)
{
Expand Down Expand Up @@ -109,45 +102,6 @@ bufcstr(struct buf *buf)
return NULL;
}

/* bufprintf: formatted printing to a buffer */
void
bufprintf(struct buf *buf, const char *fmt, ...)
{
va_list ap;
int n;

assert(buf && buf->unit);

if (buf->size >= buf->asize && bufgrow(buf, buf->size + 1) < 0)
return;
va_start(ap, fmt);
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);

if (n < 0) {
#ifdef _MSC_VER
va_start(ap, fmt);
n = _vscprintf(fmt, ap);
va_end(ap);
#else
return;
#endif
}
if ((size_t)n >= buf->asize - buf->size) {
if (bufgrow(buf, buf->size + n + 1) < 0)
return;

va_start(ap, fmt);
n = _buf_vsnprintf((char *)buf->data + buf->size, buf->asize - buf->size, fmt, ap);
va_end(ap);
}

if (n < 0)
return;

buf->size += n;
}

/* bufput: appends raw data to a buffer */
void
bufput(struct buf *buf, const void *data, size_t len)
Expand Down Expand Up @@ -182,6 +136,41 @@ bufputc(struct buf *buf, int c)
buf->size += 1;
}

/* bufputi: appends a formatted integer to a buffer, like vsnprintf("%d") */
void
bufputi(struct buf *buf, int n)
{
// Based on K&R C

// number of null-terminated decimal digits to represent x signed bytes is floor(log10(2^(8x-1)))+2
// which is bounded from above by x*3+2
char buffer[sizeof(int)*3+2];
memset(&buffer, 0, sizeof(buffer));

int sign = n;

if (sign < 0)
n = -n;

int i = 0;

do {
buffer[i++] = n % 10 + '0';
} while ((n /= 10) > 0);

if (sign < 0)
buffer[i++] = '-';

char temp;
for (int j = 0, k = i - 1; j < k; ++j, --k) {
temp = buffer[j];
buffer[j] = buffer[k];
buffer[k] = temp;
}

bufputs(buf, buffer);
}

/* bufrelease: decrease the reference count and free the buffer if needed */
void
bufrelease(struct buf *buf)
Expand Down
6 changes: 3 additions & 3 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ void bufputs(struct buf *, const char *);
/* bufputc: appends a single char to a buffer */
void bufputc(struct buf *, int);

/* bufputi: appends a formatted integer to a buffer, like vsnprintf("%d") */
void bufputi(struct buf *, int);

/* bufrelease: decrease the reference count and free the buffer if needed */
void bufrelease(struct buf *);

Expand All @@ -87,9 +90,6 @@ void bufreset(struct buf *);
/* bufslurp: removes a given number of bytes from the head of the array */
void bufslurp(struct buf *, size_t);

/* bufprintf: formatted printing to a buffer */
void bufprintf(struct buf *, const char *, ...) __attribute__ ((format (printf, 2, 3)));

/* buftruncate: truncates the buffer at `size` */
int buftruncate(struct buf *buf, size_t size);

Expand Down

0 comments on commit 8f87f48

Please sign in to comment.