Commit 51da223bf37d7b234c07372c0ba4c95c5ad87541

Martin Mitas 2019-05-11T00:11:44

Minor cleanup. * md_analyze_line: Use while() loop instead of bunch of gotos. * Remove some manual optimizations. The optimizing skills of compilers have improved in the recent years. With gcc 8.3 at -O3 there is no measurable change.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
diff --git a/md4c/md4c.c b/md4c/md4c.c
index fb8649f..9c2c186 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -5504,340 +5504,328 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
         }
     }
 
-redo:
-    /* Check whether we are fenced code continuation. */
-    if(pivot_line->type == MD_LINE_FENCEDCODE) {
-        line->beg = off;
-
-        /* We are another MD_LINE_FENCEDCODE unless we are closing fence
-         * which we transform into MD_LINE_BLANK. */
-        if(line->indent < ctx->code_indent_offset) {
-            if(md_is_closing_code_fence(ctx, CH(pivot_line->beg), off, &off)) {
-                line->type = MD_LINE_BLANK;
-                ctx->last_line_has_list_loosening_effect = FALSE;
-                goto done;
+    while(TRUE) {
+        /* Check whether we are fenced code continuation. */
+        if(pivot_line->type == MD_LINE_FENCEDCODE) {
+            line->beg = off;
+
+            /* We are another MD_LINE_FENCEDCODE unless we are closing fence
+             * which we transform into MD_LINE_BLANK. */
+            if(line->indent < ctx->code_indent_offset) {
+                if(md_is_closing_code_fence(ctx, CH(pivot_line->beg), off, &off)) {
+                    line->type = MD_LINE_BLANK;
+                    ctx->last_line_has_list_loosening_effect = FALSE;
+                    break;
+                }
             }
-        }
 
-        /* Change indentation accordingly to the initial code fence. */
-        if(n_parents == ctx->n_containers) {
-            if(line->indent > pivot_line->indent)
-                line->indent -= pivot_line->indent;
-            else
-                line->indent = 0;
+            /* Change indentation accordingly to the initial code fence. */
+            if(n_parents == ctx->n_containers) {
+                if(line->indent > pivot_line->indent)
+                    line->indent -= pivot_line->indent;
+                else
+                    line->indent = 0;
 
-            line->type = MD_LINE_FENCEDCODE;
-            goto done;
+                line->type = MD_LINE_FENCEDCODE;
+                break;
+            }
         }
-    }
 
-    /* Check whether we are HTML block continuation. */
-    if(pivot_line->type == MD_LINE_HTML  &&  ctx->html_block_type > 0) {
-        int html_block_type;
+        /* Check whether we are HTML block continuation. */
+        if(pivot_line->type == MD_LINE_HTML  &&  ctx->html_block_type > 0) {
+            int html_block_type;
 
-        html_block_type = md_is_html_block_end_condition(ctx, off, &off);
-        if(html_block_type > 0) {
-            MD_ASSERT(html_block_type == ctx->html_block_type);
+            html_block_type = md_is_html_block_end_condition(ctx, off, &off);
+            if(html_block_type > 0) {
+                MD_ASSERT(html_block_type == ctx->html_block_type);
 
-            /* Make sure this is the last line of the block. */
-            ctx->html_block_type = 0;
+                /* Make sure this is the last line of the block. */
+                ctx->html_block_type = 0;
 
-            /* Some end conditions serve as blank lines at the same time. */
-            if(html_block_type == 6 || html_block_type == 7) {
-                line->type = MD_LINE_BLANK;
-                line->indent = 0;
-                goto done;
+                /* Some end conditions serve as blank lines at the same time. */
+                if(html_block_type == 6 || html_block_type == 7) {
+                    line->type = MD_LINE_BLANK;
+                    line->indent = 0;
+                    break;
+                }
             }
-        }
 
-        if(n_parents == ctx->n_containers) {
-            line->type = MD_LINE_HTML;
-            goto done;
-        }
-    }
-
-    /* Check for blank line. */
-    if(off >= ctx->size  ||  ISNEWLINE(off)) {
-        if(pivot_line->type == MD_LINE_INDENTEDCODE  &&  n_parents == ctx->n_containers) {
-            line->type = MD_LINE_INDENTEDCODE;
-            if(line->indent > ctx->code_indent_offset)
-                line->indent -= ctx->code_indent_offset;
-            else
-                line->indent = 0;
-            ctx->last_line_has_list_loosening_effect = FALSE;
-        } else {
-            line->type = MD_LINE_BLANK;
-            ctx->last_line_has_list_loosening_effect = (n_parents > 0  &&
-                    n_brothers + n_children == 0  &&
-                    ctx->containers[n_parents-1].ch != _T('>'));
-
-#if 1
-            /* See https://github.com/mity/md4c/issues/6
-             *
-             * This ugly checking tests we are in (yet empty) list item but not
-             * its very first line (with the list item mark).
-             *
-             * If we are such blank line, then any following non-blank line
-             * which would be part of this list item actually ends the list
-             * because "a list item can begin with at most one blank line."
-             */
-            if(n_parents > 0  &&  ctx->containers[n_parents-1].ch != _T('>')  &&
-               n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
-               ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
-            {
-                MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
-                if(top_block->type == MD_BLOCK_LI)
-                    ctx->last_list_item_starts_with_two_blank_lines = TRUE;
+            if(n_parents == ctx->n_containers) {
+                line->type = MD_LINE_HTML;
+                break;
             }
-#endif
         }
-        goto done_on_eol;
-    } else {
-#if 1
-        /* This is 2nd half of the hack. If the flag is set (that is there
-         * were 2nd blank line at the start of the list item) and we would also
-         * belonging to such list item, then interrupt the list. */
-        ctx->last_line_has_list_loosening_effect = FALSE;
-        if(ctx->last_list_item_starts_with_two_blank_lines) {
-            if(n_parents > 0  &&  ctx->containers[n_parents-1].ch != _T('>')  &&
-               n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
-               ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
-            {
-                MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
-                if(top_block->type == MD_BLOCK_LI)
-                    n_parents--;
+
+        /* Check for blank line. */
+        if(off >= ctx->size  ||  ISNEWLINE(off)) {
+            if(pivot_line->type == MD_LINE_INDENTEDCODE  &&  n_parents == ctx->n_containers) {
+                line->type = MD_LINE_INDENTEDCODE;
+                if(line->indent > ctx->code_indent_offset)
+                    line->indent -= ctx->code_indent_offset;
+                else
+                    line->indent = 0;
+                ctx->last_line_has_list_loosening_effect = FALSE;
+            } else {
+                line->type = MD_LINE_BLANK;
+                ctx->last_line_has_list_loosening_effect = (n_parents > 0  &&
+                        n_brothers + n_children == 0  &&
+                        ctx->containers[n_parents-1].ch != _T('>'));
+
+    #if 1
+                /* See https://github.com/mity/md4c/issues/6
+                 *
+                 * This ugly checking tests we are in (yet empty) list item but not
+                 * its very first line (with the list item mark).
+                 *
+                 * If we are such blank line, then any following non-blank line
+                 * which would be part of this list item actually ends the list
+                 * because "a list item can begin with at most one blank line."
+                 */
+                if(n_parents > 0  &&  ctx->containers[n_parents-1].ch != _T('>')  &&
+                   n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
+                   ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
+                {
+                    MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
+                    if(top_block->type == MD_BLOCK_LI)
+                        ctx->last_list_item_starts_with_two_blank_lines = TRUE;
+                }
+    #endif
             }
+            break;
+        } else {
+    #if 1
+            /* This is 2nd half of the hack. If the flag is set (that is there
+             * were 2nd blank line at the start of the list item) and we would also
+             * belonging to such list item, then interrupt the list. */
+            ctx->last_line_has_list_loosening_effect = FALSE;
+            if(ctx->last_list_item_starts_with_two_blank_lines) {
+                if(n_parents > 0  &&  ctx->containers[n_parents-1].ch != _T('>')  &&
+                   n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
+                   ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
+                {
+                    MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
+                    if(top_block->type == MD_BLOCK_LI)
+                        n_parents--;
+                }
 
-            ctx->last_list_item_starts_with_two_blank_lines = FALSE;
+                ctx->last_list_item_starts_with_two_blank_lines = FALSE;
+            }
+    #endif
         }
-#endif
-    }
 
-    /* Check whether we are Setext underline. */
-    if(line->indent < ctx->code_indent_offset  &&  pivot_line->type == MD_LINE_TEXT
-        &&  (CH(off) == _T('=') || CH(off) == _T('-'))
-        &&  (n_parents == ctx->n_containers))
-    {
-        unsigned level;
+        /* Check whether we are Setext underline. */
+        if(line->indent < ctx->code_indent_offset  &&  pivot_line->type == MD_LINE_TEXT
+            &&  (CH(off) == _T('=') || CH(off) == _T('-'))
+            &&  (n_parents == ctx->n_containers))
+        {
+            unsigned level;
 
-        if(md_is_setext_underline(ctx, off, &off, &level)) {
-            line->type = MD_LINE_SETEXTUNDERLINE;
-            line->data = level;
-            goto done;
+            if(md_is_setext_underline(ctx, off, &off, &level)) {
+                line->type = MD_LINE_SETEXTUNDERLINE;
+                line->data = level;
+                break;
+            }
         }
-    }
 
-    /* Check for thematic break line. */
-    if(line->indent < ctx->code_indent_offset  &&  ISANYOF(off, _T("-_*"))  &&  off >= hr_killer) {
-        if(md_is_hr_line(ctx, off, &off, &hr_killer)) {
-            line->type = MD_LINE_HR;
-            goto done;
+        /* Check for thematic break line. */
+        if(line->indent < ctx->code_indent_offset  &&  ISANYOF(off, _T("-_*"))  &&  off >= hr_killer) {
+            if(md_is_hr_line(ctx, off, &off, &hr_killer)) {
+                line->type = MD_LINE_HR;
+                break;
+            }
         }
-    }
 
-    /* Check for "brother" container. I.e. whether we are another list item
-     * in already started list. */
-    if(n_parents < ctx->n_containers  &&  n_brothers + n_children == 0) {
-        OFF tmp;
+        /* Check for "brother" container. I.e. whether we are another list item
+         * in already started list. */
+        if(n_parents < ctx->n_containers  &&  n_brothers + n_children == 0) {
+            OFF tmp;
 
-        if(md_is_container_mark(ctx, line->indent, off, &tmp, &container)  &&
-           md_is_container_compatible(&ctx->containers[n_parents], &container))
-        {
-            pivot_line = &md_dummy_blank_line;
-
-            off = tmp;
+            if(md_is_container_mark(ctx, line->indent, off, &tmp, &container)  &&
+               md_is_container_compatible(&ctx->containers[n_parents], &container))
+            {
+                pivot_line = &md_dummy_blank_line;
 
-            total_indent += container.contents_indent - container.mark_indent;
-            line->indent = md_line_indentation(ctx, total_indent, off, &off);
-            total_indent += line->indent;
-            line->beg = off;
+                off = tmp;
 
-            /* Some of the following whitespace actually still belongs to the mark. */
-            if(off >= ctx->size || ISNEWLINE(off)) {
-                container.contents_indent++;
-            } else if(line->indent <= ctx->code_indent_offset) {
-                container.contents_indent += line->indent;
-                line->indent = 0;
-            } else {
-                container.contents_indent += 1;
-                line->indent--;
-            }
+                total_indent += container.contents_indent - container.mark_indent;
+                line->indent = md_line_indentation(ctx, total_indent, off, &off);
+                total_indent += line->indent;
+                line->beg = off;
+
+                /* Some of the following whitespace actually still belongs to the mark. */
+                if(off >= ctx->size || ISNEWLINE(off)) {
+                    container.contents_indent++;
+                } else if(line->indent <= ctx->code_indent_offset) {
+                    container.contents_indent += line->indent;
+                    line->indent = 0;
+                } else {
+                    container.contents_indent += 1;
+                    line->indent--;
+                }
 
-            ctx->containers[n_parents].mark_indent = container.mark_indent;
-            ctx->containers[n_parents].contents_indent = container.contents_indent;
+                ctx->containers[n_parents].mark_indent = container.mark_indent;
+                ctx->containers[n_parents].contents_indent = container.contents_indent;
 
-            n_brothers++;
-            goto redo;
+                n_brothers++;
+                continue;
+            }
         }
-    }
 
-    /* Check for indented code.
-     * Note indented code block cannot interrupt a paragraph. */
-    if(line->indent >= ctx->code_indent_offset  &&
-        (pivot_line->type == MD_LINE_BLANK || pivot_line->type == MD_LINE_INDENTEDCODE))
-    {
-        line->type = MD_LINE_INDENTEDCODE;
-        MD_ASSERT(line->indent >= ctx->code_indent_offset);
-        line->indent -= ctx->code_indent_offset;
-        line->data = 0;
-        goto done;
-    }
-
-    /* Check for start of a new container block. */
-    if(line->indent < ctx->code_indent_offset  &&
-       md_is_container_mark(ctx, line->indent, off, &off, &container))
-    {
-        if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
-                    (off >= ctx->size || ISNEWLINE(off)))
+        /* Check for indented code.
+         * Note indented code block cannot interrupt a paragraph. */
+        if(line->indent >= ctx->code_indent_offset  &&
+            (pivot_line->type == MD_LINE_BLANK || pivot_line->type == MD_LINE_INDENTEDCODE))
         {
-            /* Noop. List mark followed by a blank line cannot interrupt a paragraph. */
-        } else if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
-                    (container.ch == _T('.') || container.ch == _T(')'))  &&  container.start != 1)
-        {
-            /* Noop. Ordered list cannot interrupt a paragraph unless the start index is 1. */
-        } else {
-            total_indent += container.contents_indent - container.mark_indent;
-            line->indent = md_line_indentation(ctx, total_indent, off, &off);
-            total_indent += line->indent;
+            line->type = MD_LINE_INDENTEDCODE;
+            MD_ASSERT(line->indent >= ctx->code_indent_offset);
+            line->indent -= ctx->code_indent_offset;
+            line->data = 0;
+            break;
+        }
 
-            line->beg = off;
-            line->data = container.ch;
-
-            /* Some of the following whitespace actually still belongs to the mark. */
-            if(off >= ctx->size || ISNEWLINE(off)) {
-                container.contents_indent++;
-            } else if(line->indent <= ctx->code_indent_offset) {
-                container.contents_indent += line->indent;
-                line->indent = 0;
+        /* Check for start of a new container block. */
+        if(line->indent < ctx->code_indent_offset  &&
+           md_is_container_mark(ctx, line->indent, off, &off, &container))
+        {
+            if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
+                        (off >= ctx->size || ISNEWLINE(off)))
+            {
+                /* Noop. List mark followed by a blank line cannot interrupt a paragraph. */
+            } else if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
+                        (container.ch == _T('.') || container.ch == _T(')'))  &&  container.start != 1)
+            {
+                /* Noop. Ordered list cannot interrupt a paragraph unless the start index is 1. */
             } else {
-                container.contents_indent += 1;
-                line->indent--;
-            }
+                total_indent += container.contents_indent - container.mark_indent;
+                line->indent = md_line_indentation(ctx, total_indent, off, &off);
+                total_indent += line->indent;
+
+                line->beg = off;
+                line->data = container.ch;
+
+                /* Some of the following whitespace actually still belongs to the mark. */
+                if(off >= ctx->size || ISNEWLINE(off)) {
+                    container.contents_indent++;
+                } else if(line->indent <= ctx->code_indent_offset) {
+                    container.contents_indent += line->indent;
+                    line->indent = 0;
+                } else {
+                    container.contents_indent += 1;
+                    line->indent--;
+                }
 
-            if(n_brothers + n_children == 0)
-                pivot_line = &md_dummy_blank_line;
+                if(n_brothers + n_children == 0)
+                    pivot_line = &md_dummy_blank_line;
 
-            if(n_children == 0)
-                MD_CHECK(md_leave_child_containers(ctx, n_parents + n_brothers));
+                if(n_children == 0)
+                    MD_CHECK(md_leave_child_containers(ctx, n_parents + n_brothers));
 
-            n_children++;
-            MD_CHECK(md_push_container(ctx, &container));
-            goto redo;
+                n_children++;
+                MD_CHECK(md_push_container(ctx, &container));
+                continue;
+            }
         }
-    }
 
-    /* Check whether we are table continuation. */
-    if(pivot_line->type == MD_LINE_TABLE  &&  md_is_table_row(ctx, off, &off)  &&
-       n_parents == ctx->n_containers)
-    {
-        line->type = MD_LINE_TABLE;
-        goto done;
-    }
+        /* Check whether we are table continuation. */
+        if(pivot_line->type == MD_LINE_TABLE  &&  md_is_table_row(ctx, off, &off)  &&
+           n_parents == ctx->n_containers)
+        {
+            line->type = MD_LINE_TABLE;
+            break;
+        }
 
-    /* Check for ATX header. */
-    if(line->indent < ctx->code_indent_offset  &&  CH(off) == _T('#')) {
-        unsigned level;
+        /* Check for ATX header. */
+        if(line->indent < ctx->code_indent_offset  &&  CH(off) == _T('#')) {
+            unsigned level;
 
-        if(md_is_atxheader_line(ctx, off, &line->beg, &off, &level)) {
-            line->type = MD_LINE_ATXHEADER;
-            line->data = level;
-            goto done;
+            if(md_is_atxheader_line(ctx, off, &line->beg, &off, &level)) {
+                line->type = MD_LINE_ATXHEADER;
+                line->data = level;
+                break;
+            }
         }
-    }
 
-    /* Check whether we are starting code fence. */
-    if(CH(off) == _T('`') || CH(off) == _T('~')) {
-        if(md_is_opening_code_fence(ctx, off, &off)) {
-            line->type = MD_LINE_FENCEDCODE;
-            line->data = 1;
-            goto done;
+        /* Check whether we are starting code fence. */
+        if(CH(off) == _T('`') || CH(off) == _T('~')) {
+            if(md_is_opening_code_fence(ctx, off, &off)) {
+                line->type = MD_LINE_FENCEDCODE;
+                line->data = 1;
+                break;
+            }
         }
-    }
 
-    /* Check for start of raw HTML block. */
-    if(CH(off) == _T('<')  &&  !(ctx->parser.flags & MD_FLAG_NOHTMLBLOCKS))
-    {
-        ctx->html_block_type = md_is_html_block_start_condition(ctx, off);
-
-        /* HTML block type 7 cannot interrupt paragraph. */
-        if(ctx->html_block_type == 7  &&  pivot_line->type == MD_LINE_TEXT)
-            ctx->html_block_type = 0;
+        /* Check for start of raw HTML block. */
+        if(CH(off) == _T('<')  &&  !(ctx->parser.flags & MD_FLAG_NOHTMLBLOCKS))
+        {
+            ctx->html_block_type = md_is_html_block_start_condition(ctx, off);
 
-        if(ctx->html_block_type > 0) {
-            /* The line itself also may immediately close the block. */
-            if(md_is_html_block_end_condition(ctx, off, &off) == ctx->html_block_type) {
-                /* Make sure this is the last line of the block. */
+            /* HTML block type 7 cannot interrupt paragraph. */
+            if(ctx->html_block_type == 7  &&  pivot_line->type == MD_LINE_TEXT)
                 ctx->html_block_type = 0;
-            }
 
-            line->type = MD_LINE_HTML;
-            goto done;
-        }
-    }
+            if(ctx->html_block_type > 0) {
+                /* The line itself also may immediately close the block. */
+                if(md_is_html_block_end_condition(ctx, off, &off) == ctx->html_block_type) {
+                    /* Make sure this is the last line of the block. */
+                    ctx->html_block_type = 0;
+                }
 
-    /* Check for table underline. */
-    if((ctx->parser.flags & MD_FLAG_TABLES)  &&  pivot_line->type == MD_LINE_TEXT  &&
-       (CH(off) == _T('|') || CH(off) == _T('-') || CH(off) == _T(':'))  &&
-       n_parents == ctx->n_containers)
-    {
-        unsigned col_count;
+                line->type = MD_LINE_HTML;
+                break;
+            }
+        }
 
-        if(ctx->current_block != NULL  &&  ctx->current_block->n_lines == 1  &&
-            md_is_table_underline(ctx, off, &off, &col_count)  &&
-            md_is_table_row(ctx, pivot_line->beg, NULL))
+        /* Check for table underline. */
+        if((ctx->parser.flags & MD_FLAG_TABLES)  &&  pivot_line->type == MD_LINE_TEXT  &&
+           (CH(off) == _T('|') || CH(off) == _T('-') || CH(off) == _T(':'))  &&
+           n_parents == ctx->n_containers)
         {
-            line->data = col_count;
-            line->type = MD_LINE_TABLEUNDERLINE;
-            goto done;
-        }
-    }
+            unsigned col_count;
 
-    /* By default, we are normal text line. */
-    line->type = MD_LINE_TEXT;
-    if(pivot_line->type == MD_LINE_TEXT  &&  n_brothers + n_children == 0) {
-        /* Lazy continuation. */
-        n_parents = ctx->n_containers;
-    }
+            if(ctx->current_block != NULL  &&  ctx->current_block->n_lines == 1  &&
+                md_is_table_underline(ctx, off, &off, &col_count)  &&
+                md_is_table_row(ctx, pivot_line->beg, NULL))
+            {
+                line->data = col_count;
+                line->type = MD_LINE_TABLEUNDERLINE;
+                break;
+            }
+        }
 
-    /* Check for task mark. */
-    if((ctx->parser.flags & MD_FLAG_TASKLISTS)  &&  n_brothers + n_children > 0  &&
-       ISANYOF_(ctx->containers[ctx->n_containers-1].ch, _T("-+*.)")))
-    {
-        OFF tmp = off;
+        /* By default, we are normal text line. */
+        line->type = MD_LINE_TEXT;
+        if(pivot_line->type == MD_LINE_TEXT  &&  n_brothers + n_children == 0) {
+            /* Lazy continuation. */
+            n_parents = ctx->n_containers;
+        }
 
-        while(tmp < ctx->size  &&  tmp < off + 3  &&  ISBLANK(tmp))
-            tmp++;
-        if(tmp + 2 < ctx->size  &&  CH(tmp) == _T('[')  &&
-           ISANYOF(tmp+1, _T("xX "))  &&  CH(tmp+2) == _T(']')  &&
-           (tmp + 3 == ctx->size  ||  ISBLANK(tmp+3)  ||  ISNEWLINE(tmp+3)))
+        /* Check for task mark. */
+        if((ctx->parser.flags & MD_FLAG_TASKLISTS)  &&  n_brothers + n_children > 0  &&
+           ISANYOF_(ctx->containers[ctx->n_containers-1].ch, _T("-+*.)")))
         {
-            MD_CONTAINER* task_container = (n_children > 0 ? &ctx->containers[ctx->n_containers-1] : &container);
-            task_container->is_task = TRUE;
-            task_container->task_mark_off = tmp + 1;
-            off = tmp + 3;
-            while(ISWHITESPACE(off))
-                off++;
-            line->beg = off;
+            OFF tmp = off;
+
+            while(tmp < ctx->size  &&  tmp < off + 3  &&  ISBLANK(tmp))
+                tmp++;
+            if(tmp + 2 < ctx->size  &&  CH(tmp) == _T('[')  &&
+               ISANYOF(tmp+1, _T("xX "))  &&  CH(tmp+2) == _T(']')  &&
+               (tmp + 3 == ctx->size  ||  ISBLANK(tmp+3)  ||  ISNEWLINE(tmp+3)))
+            {
+                MD_CONTAINER* task_container = (n_children > 0 ? &ctx->containers[ctx->n_containers-1] : &container);
+                task_container->is_task = TRUE;
+                task_container->task_mark_off = tmp + 1;
+                off = tmp + 3;
+                while(ISWHITESPACE(off))
+                    off++;
+                line->beg = off;
+            }
         }
     }
 
-done:
-    /* Scan for end of the line.
-     *
-     * Note this is bottleneck of this function as we itereate over (almost)
-     * all line contents after some initial line indentation. To optimize, we
-     * try to eat multiple chars in every loop iteration.
-     *
-     * (Measured ~6% performance boost of md2html with this optimization for
-     * normal kind of input.)
-     */
-    while(off + 4 < ctx->size  &&  !ISNEWLINE(off+0)  &&  !ISNEWLINE(off+1)
-                               &&  !ISNEWLINE(off+2)  &&  !ISNEWLINE(off+3))
-        off += 4;
+    /* Scan for end of the line. */
     while(off < ctx->size  &&  !ISNEWLINE(off))
         off++;
 
-done_on_eol:
     /* Set end of the line. */
     line->end = off;