From 359fb5b47365abaebb1b76ae52aeb27efdd39ae7 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 26 Mar 2016 12:48:22 -0700 Subject: Handle buffer split across a CRLF line ending (closes #117). Adds an internal field to the parser struct to keep track of last_buffer_ended_with_cr. --- src/blocks.c | 11 ++++++++++- src/parser.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/blocks.c b/src/blocks.c index d10d1ca..f49ad4e 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -90,6 +90,7 @@ cmark_parser *cmark_parser_new(int options) { parser->last_line_length = 0; parser->linebuf = buf; parser->options = options; + parser->last_buffer_ended_with_cr = false; return parser; } @@ -506,6 +507,11 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, const unsigned char *end = buffer + len; static const uint8_t repl[] = {239, 191, 189}; + if (parser->last_buffer_ended_with_cr && *buffer == '\n') { + // skip NL if last buffer ended with CR ; see #117 + buffer++; + } + parser->last_buffer_ended_with_cr = false; while (buffer < end) { const unsigned char *eol; bufsize_t chunk_len; @@ -546,8 +552,11 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, buffer += chunk_len; // skip over line ending characters: - if (buffer < end && *buffer == '\r') + if (buffer < end && *buffer == '\r') { buffer++; + if (buffer == end) + parser->last_buffer_ended_with_cr = true; + } if (buffer < end && *buffer == '\n') buffer++; } diff --git a/src/parser.h b/src/parser.h index b3ff39b..ab21d0f 100644 --- a/src/parser.h +++ b/src/parser.h @@ -27,6 +27,7 @@ struct cmark_parser { bufsize_t last_line_length; cmark_strbuf *linebuf; int options; + bool last_buffer_ended_with_cr; }; #ifdef __cplusplus -- cgit v1.2.3