Commit ad4f28bb85136cde06eb01b5cb4255b28870f145

Martin Mitas 2017-07-24T20:09:23

md_analyze_simple_pairing_mark: Fix the "rule of three". If the first emphasis opener is refused due the rule of three, a previous opener is examined. However the variable opener_orig_size_module3 was not (re)set accordingly. Fixes #21.

diff --git a/md4c/md4c.c b/md4c/md4c.c
index 30a29cc..f41a088 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -3487,26 +3487,33 @@ md_analyze_simple_pairing_mark(MD_CTX* ctx, MD_MARKCHAIN* chain, int mark_index,
         SZ opener_size = opener->end - opener->beg;
         SZ closer_size = mark->end - mark->beg;
 
-        if(apply_rule_of_three  &&
-                ((mark->flags & MD_MARK_EMPH_INTRAWORD) || (opener->flags & MD_MARK_EMPH_INTRAWORD)))
-        {
-            SZ opener_orig_size_modulo3;
+        /* Apply the "rule of three". */
+        if(apply_rule_of_three) {
+            while((mark->flags & MD_MARK_EMPH_INTRAWORD) || (opener->flags & MD_MARK_EMPH_INTRAWORD)) {
+                SZ opener_orig_size_modulo3;
+
+                switch(opener->flags & MD_MARK_EMPH_MODULO3_MASK) {
+                    case MD_MARK_EMPH_MODULO3_0:    opener_orig_size_modulo3 = 0; break;
+                    case MD_MARK_EMPH_MODULO3_1:    opener_orig_size_modulo3 = 1; break;
+                    case MD_MARK_EMPH_MODULO3_2:    opener_orig_size_modulo3 = 2; break;
+                    default:                        MD_UNREACHABLE(); break;
+                }
 
-            switch(opener->flags & MD_MARK_EMPH_MODULO3_MASK) {
-                case MD_MARK_EMPH_MODULO3_0:    opener_orig_size_modulo3 = 0; break;
-                case MD_MARK_EMPH_MODULO3_1:    opener_orig_size_modulo3 = 1; break;
-                case MD_MARK_EMPH_MODULO3_2:    opener_orig_size_modulo3 = 2; break;
-                default:                        MD_UNREACHABLE(); break;
-            }
+                if((opener_orig_size_modulo3 + closer_size) % 3 != 0) {
+                    /* This opener is suitable. */
+                    break;
+                }
 
-            while((opener_orig_size_modulo3 + closer_size) % 3 == 0) {
-                if(opener->prev < 0)
+                if(opener->prev >= 0) {
+                    /* Try previous opener. */
+                    opener_index = opener->prev;
+                    opener = &ctx->marks[opener_index];
+                    opener_size = opener->end - opener->beg;
+                    closer_size = mark->end - mark->beg;
+                } else {
+                    /* No suitable opener found. */
                     goto cannot_resolve;
-
-                opener_index = opener->prev;
-                opener = &ctx->marks[opener_index];
-                opener_size = opener->end - opener->beg;
-                closer_size = mark->end - mark->beg;
+                }
             }
         }
 
diff --git a/test/coverage.txt b/test/coverage.txt
index b6c868b..2a1be58 100644
--- a/test/coverage.txt
+++ b/test/coverage.txt
@@ -102,6 +102,14 @@ a***b* c*
 ````````````````````````````````
 
 
+### [Issue 21](https://github.com/mity/md4c/issues/21)
+```````````````````````````````` example
+a*b**c*
+.
+<p>a<em>b**c</em></p>
+````````````````````````````````
+
+
 
 ## Code coverage