From ca5217ff94f8a40a703596542a25ba4dbb20b1e3 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 26 Oct 2023 09:55:13 -0700 Subject: [PATCH] Track underscore bottom separately mod 3, like asterisk The reasoning that a failed delimiter means future delimiters will also fail only applies if the reason they failed was not the multiple-of-three rule. This was already implemented correctly for asterisks, but not for underscore. This commit ports [97da298] from commonmark.js to cmark. [97da298]: https://github.com/commonmark/commonmark.js/commit/97da298feb94e8c404c9c734a8b5965bbcbb398e --- src/inlines.c | 13 ++++++++----- test/regression.txt | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/inlines.c b/src/inlines.c index b0741383f..1313428d5 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -653,9 +653,11 @@ static void process_emphasis(subject *subj, bufsize_t stack_bottom) { delimiter *old_closer; bool opener_found; int openers_bottom_index = 0; - bufsize_t openers_bottom[9] = {stack_bottom, stack_bottom, stack_bottom, - stack_bottom, stack_bottom, stack_bottom, - stack_bottom, stack_bottom, stack_bottom}; + bufsize_t openers_bottom[15] = {stack_bottom, stack_bottom, stack_bottom, + stack_bottom, stack_bottom, stack_bottom, + stack_bottom, stack_bottom, stack_bottom, + stack_bottom, stack_bottom, stack_bottom, + stack_bottom, stack_bottom, stack_bottom}; // move back to first relevant delim. candidate = subj->last_delim; @@ -675,10 +677,11 @@ static void process_emphasis(subject *subj, bufsize_t stack_bottom) { openers_bottom_index = 1; break; case '_': - openers_bottom_index = 2; + openers_bottom_index = 2 + + (closer->can_open ? 3 : 0) + (closer->length % 3); break; case '*': - openers_bottom_index = 3 + + openers_bottom_index = 8 + (closer->can_open ? 3 : 0) + (closer->length % 3); break; default: diff --git a/test/regression.txt b/test/regression.txt index f905677d6..1707956d7 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -212,4 +212,32 @@ x

x

```````````````````````````````` +An underscore that is not part of a delimiter should not prevent another +pair of underscores from forming part of their own. +```````````````````````````````` example +__!_!__ + +__!x!__ + +**!*!** + +--- + +_*__*_* + +_*xx*_* + +_*__-_- + +_*xx-_- +. +

!_!

+

!x!

+

!*!

+
+

__*

+

xx*

+

*__--

+

*xx--

+````````````````````````````````