Commit f37a89f5d75fc6d2557c8f71c43189e9a1e2e6d1

Martin Mitáš 2024-02-01T21:55:45

md_is_inline_link_spec: Use md_lookup_line() instead of walking. Fixes #236.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index cf22807..295e84a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,15 @@
 # MD4C Change Log
 
 
+## Next Version (Work in Progress)
+
+Fixes:
+
+ - [#236](https://github.com/mity/md4c/issues/236):
+   Fix quadratic time behavior caused by one-by-one walking over block lines
+   instead of calling `md_lookup_line()`.
+
+
 ## Version 0.5.2
 
 Changes:
diff --git a/src/md4c.c b/src/md4c.c
index 812bde5..cb44e3d 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -2332,8 +2332,7 @@ md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
     OFF off = beg;
     int ret = FALSE;
 
-    while(off >= lines[line_index].end)
-        line_index++;
+    md_lookup_line(off, lines, n_lines, &line_index);
 
     MD_ASSERT(CH(off) == _T('('));
     off++;
diff --git a/test/pathological-tests.py b/test/pathological-tests.py
index d72453d..924cbe9 100644
--- a/test/pathological-tests.py
+++ b/test/pathological-tests.py
@@ -99,7 +99,10 @@ pathological = {
     "huge table":
             (("th|" * 10000 + "\n" + "-|" * 10000 + "\n" + "td\n" * 10000),
             re.compile(""),
-            "--ftables")
+            "--ftables"),
+    "many broken links":
+            (("]([\n" * 50000),
+            re.compile("<p>(\]\(\[\r?\n){49999}\]\(\[</p>"))
 }
 
 whitespace_re = re.compile('/s+/')