cmark

My personal build of CMark ✏️

Commit
455e59fa39fb3cd18a1f01b31eea5ead26fbf7c4
Parent
22fa64426a24df5b906bf39e718acf1d64e5d8cf
Author
John MacFarlane <jgm@berkeley.edu>
Date

Clarify logic in S_advance_offset.

Diffstat

1 file changed, 16 insertions, 5 deletions

Status File Name N° Changes Insertions Deletions
Modified src/blocks.c 21 16 5
diff --git a/src/blocks.c b/src/blocks.c
@@ -17,6 +17,10 @@
 #define CODE_INDENT 4
 #define TAB_STOP 4
 
+#ifndef MIN
+#define MIN(x, y) ((x < y) ? x : y)
+#endif
+
 #define peek_at(i, n) (i)->data[n]
 
 static inline bool S_is_line_end_char(char c) {
@@ -587,11 +591,18 @@ static void S_advance_offset(cmark_parser *parser, cmark_chunk *input,
   while (count > 0 && (c = peek_at(input, parser->offset))) {
     if (c == '\t') {
       chars_to_tab = TAB_STOP - (parser->column % TAB_STOP);
-      parser->partially_consumed_tab = columns && chars_to_tab > count;
-      chars_to_advance = parser->partially_consumed_tab ? count : chars_to_tab;
-      parser->column += chars_to_advance;
-      parser->offset += parser->partially_consumed_tab ? 0 : 1;
-      count -= (columns ? chars_to_advance : 1);
+      if (columns) {
+	parser->partially_consumed_tab = chars_to_tab > count;
+	chars_to_advance = MIN(count, chars_to_tab);
+	parser->column += chars_to_advance;
+        parser->offset += (parser->partially_consumed_tab ? 0 : 1);
+	count -= chars_to_advance;
+      } else {
+	parser->partially_consumed_tab = false;
+	parser->column += chars_to_tab;
+	parser->offset += 1;
+	count -= 1;
+      }
     } else {
       parser->partially_consumed_tab = false;
       parser->offset += 1;