From 1bbe20dca1fdf7e81bbba981e41fc5fdb6068511 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 18:22:28 +0100 Subject: [PATCH 1/7] Rewrite Descendants(MarkdownObject) iteratively --- .../Syntax/MarkdownObjectExtensions.cs | 260 +++++++++--------- 1 file changed, 130 insertions(+), 130 deletions(-) diff --git a/src/Markdig/Syntax/MarkdownObjectExtensions.cs b/src/Markdig/Syntax/MarkdownObjectExtensions.cs index c7fac83f..4875ae09 100644 --- a/src/Markdig/Syntax/MarkdownObjectExtensions.cs +++ b/src/Markdig/Syntax/MarkdownObjectExtensions.cs @@ -1,131 +1,131 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. -// See the license.txt file in the project root for more information. - -using System.Collections.Generic; -using Markdig.Syntax.Inlines; - -namespace Markdig.Syntax -{ - /// - /// Extensions for visiting or - /// - public static class MarkdownObjectExtensions - { - /// - /// Iterates over the descendant elements for the specified markdown element, including and . - /// - /// The markdown object. - /// An iteration over the descendant elements - public static IEnumerable Descendants(this MarkdownObject markdownObject) - { - // TODO: implement a recursiveless method - - var block = markdownObject as ContainerBlock; - if (block != null) - { - foreach (var subBlock in block) - { - yield return subBlock; - - foreach (var sub in subBlock.Descendants()) - { - yield return sub; - } - - // Visit leaf block that have inlines - var leafBlock = subBlock as LeafBlock; - if (leafBlock?.Inline != null) - { - foreach (var subInline in Descendants(leafBlock.Inline)) - { - yield return subInline; - } - } - } - } - else - { - var inline = markdownObject as ContainerInline; - if (inline != null) - { - var child = inline.FirstChild; - while (child != null) - { - var next = child.NextSibling; - yield return child; - - foreach (var sub in child.Descendants()) - { - yield return sub; - } - - child = next; - } - } - } - } - - /// - /// Iterates over the descendant elements for the specified markdown element and filters by the type {T}. - /// - /// Type to use for filtering the descendants - /// The inline markdown object. - /// - /// An iteration over the descendant elements - /// - public static IEnumerable Descendants(this ContainerInline inline) where T : Inline - { - var child = inline.FirstChild; - while (child != null) - { - var next = child.NextSibling; - var childT = child as T; - if (childT != null) - { - yield return childT; - } - - var subContainer = child as ContainerInline; - if (subContainer != null) - { - foreach (var sub in subContainer.Descendants()) - { - yield return sub; - } - } - - child = next; - } - } - - /// - /// Iterates over the descendant elements for the specified markdown element and filters by the type {T}. - /// - /// Type to use for filtering the descendants - /// The markdown object. - /// - /// An iteration over the descendant elements - /// - public static IEnumerable Descendants(this ContainerBlock block) where T : Block - { - foreach (var subBlock in block) - { - var subBlockT = subBlock as T; - if (subBlockT != null) - { - yield return subBlockT; - } - - var subBlockContainer = subBlock as ContainerBlock; - if (subBlockContainer != null) - { - foreach (var sub in subBlockContainer.Descendants()) - { - yield return sub; - } - } - } - } - } +// Copyright (c) Alexandre Mutel. All rights reserved. +// This file is licensed under the BSD-Clause 2 license. +// See the license.txt file in the project root for more information. + +using System.Collections.Generic; +using Markdig.Syntax.Inlines; + +namespace Markdig.Syntax +{ + /// + /// Extensions for visiting or + /// + public static class MarkdownObjectExtensions + { + /// + /// Iterates over the descendant elements for the specified markdown element, including and . + /// The descendant elements are returned in DFS-like order. + /// + /// The markdown object. + /// An iteration over the descendant elements + public static IEnumerable Descendants(this MarkdownObject markdownObject) + { + // Fast-path an object with no children to avoid allocating Stack objects + if (!(markdownObject is ContainerBlock) && !(markdownObject is ContainerInline)) yield break; + + // TODO: A single Stack<(MarkdownObject block, bool push)> when ValueTuples are available + Stack stack = new Stack(); + Stack pushStack = new Stack(); + + stack.Push(markdownObject); + pushStack.Push(false); + + while (stack.Count > 0) + { + var block = stack.Pop(); + var push = pushStack.Pop(); + if (block is ContainerBlock containerBlock) + { + if (push) yield return containerBlock; + int subBlockIndex = containerBlock.Count; + while (subBlockIndex-- > 0) + { + var subBlock = containerBlock[subBlockIndex]; + if (subBlock is LeafBlock leafBlock) + { + if (leafBlock.Inline != null) + { + stack.Push(leafBlock.Inline); + pushStack.Push(false); + } + } + stack.Push(subBlock); + pushStack.Push(true); + } + } + else if (block is ContainerInline containerInline) + { + if (push) yield return containerInline; + var child = containerInline.LastChild; + while (child != null) + { + stack.Push(child); + pushStack.Push(true); + child = child.PreviousSibling; + } + } + else yield return block; + } + } + + /// + /// Iterates over the descendant elements for the specified markdown element and filters by the type {T}. + /// + /// Type to use for filtering the descendants + /// The inline markdown object. + /// + /// An iteration over the descendant elements + /// + public static IEnumerable Descendants(this ContainerInline inline) where T : Inline + { + var child = inline.FirstChild; + while (child != null) + { + var next = child.NextSibling; + T childT = child as T; + if (childT != null) + { + yield return childT; + } + + ContainerInline subContainer = child as ContainerInline; + if (subContainer != null) + { + foreach (var sub in subContainer.Descendants()) + { + yield return sub; + } + } + + child = next; + } + } + + /// + /// Iterates over the descendant elements for the specified markdown element and filters by the type {T}. + /// + /// Type to use for filtering the descendants + /// The markdown object. + /// + /// An iteration over the descendant elements + /// + public static IEnumerable Descendants(this ContainerBlock block) where T : Block + { + foreach (var subBlock in block) + { + if (subBlock is T subBlockT) + { + yield return subBlockT; + } + + if (subBlock is ContainerBlock subBlockContainer) + { + foreach (var sub in subBlockContainer.Descendants()) + { + yield return sub; + } + } + } + } + } } \ No newline at end of file From 37325f1bdcd7ab0678b842e66cf044badfdad71b Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 18:27:58 +0100 Subject: [PATCH 2/7] Use existing Descantants(ContainerInline) implementation --- .../Syntax/MarkdownObjectExtensions.cs | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/src/Markdig/Syntax/MarkdownObjectExtensions.cs b/src/Markdig/Syntax/MarkdownObjectExtensions.cs index 4875ae09..64433392 100644 --- a/src/Markdig/Syntax/MarkdownObjectExtensions.cs +++ b/src/Markdig/Syntax/MarkdownObjectExtensions.cs @@ -77,29 +77,7 @@ namespace Markdig.Syntax /// An iteration over the descendant elements /// public static IEnumerable Descendants(this ContainerInline inline) where T : Inline - { - var child = inline.FirstChild; - while (child != null) - { - var next = child.NextSibling; - T childT = child as T; - if (childT != null) - { - yield return childT; - } - - ContainerInline subContainer = child as ContainerInline; - if (subContainer != null) - { - foreach (var sub in subContainer.Descendants()) - { - yield return sub; - } - } - - child = next; - } - } + => inline.FindDescendants(); /// /// Iterates over the descendant elements for the specified markdown element and filters by the type {T}. From 4519d3820afa312141fb0550b80f426d56f95d66 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 18:52:30 +0100 Subject: [PATCH 3/7] Rewrite Descendants(ContainerBlock) iteratively --- .../Syntax/MarkdownObjectExtensions.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Markdig/Syntax/MarkdownObjectExtensions.cs b/src/Markdig/Syntax/MarkdownObjectExtensions.cs index 64433392..702df296 100644 --- a/src/Markdig/Syntax/MarkdownObjectExtensions.cs +++ b/src/Markdig/Syntax/MarkdownObjectExtensions.cs @@ -89,8 +89,20 @@ namespace Markdig.Syntax /// public static IEnumerable Descendants(this ContainerBlock block) where T : Block { - foreach (var subBlock in block) + // Fast-path an empty container to avoid allocating a Stack + if (block.Count == 0) yield break; + + Stack stack = new Stack(); + + int childrenCount = block.Count; + while (childrenCount-- > 0) { + stack.Push(block[childrenCount]); + } + + while (stack.Count > 0) + { + var subBlock = stack.Pop(); if (subBlock is T subBlockT) { yield return subBlockT; @@ -98,9 +110,10 @@ namespace Markdig.Syntax if (subBlock is ContainerBlock subBlockContainer) { - foreach (var sub in subBlockContainer.Descendants()) + childrenCount = subBlockContainer.Count; + while (childrenCount-- > 0) { - yield return sub; + stack.Push(subBlockContainer[childrenCount]); } } } From 282da043b767ca146496fd3cdc6845800629e03e Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 18:52:48 +0100 Subject: [PATCH 4/7] Rewrite Descendants(ContainerInline) iteratively --- src/Markdig/Syntax/Inlines/ContainerInline.cs | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Markdig/Syntax/Inlines/ContainerInline.cs b/src/Markdig/Syntax/Inlines/ContainerInline.cs index f7535a01..cb954a16 100644 --- a/src/Markdig/Syntax/Inlines/ContainerInline.cs +++ b/src/Markdig/Syntax/Inlines/ContainerInline.cs @@ -94,25 +94,36 @@ namespace Markdig.Syntax.Inlines /// An enumeration of T public IEnumerable FindDescendants() where T : Inline { - var child = FirstChild; - while (child != null) - { - var next = child.NextSibling; + // Fast-path an empty container to avoid allocating a Stack + if (LastChild == null) yield break; - if (child is T) - { - yield return (T)child; - } + Stack stack = new Stack(); - if (child is ContainerInline) - { - foreach (var subChild in ((ContainerInline) child).FindDescendants()) - { - yield return subChild; - } - } - - child = next; + var child = LastChild; + while (child != null) + { + stack.Push(child); + child = child.PreviousSibling; + } + + while (stack.Count > 0) + { + child = stack.Pop(); + + if (child is T childT) + { + yield return childT; + } + + if (child is ContainerInline containerInline) + { + child = containerInline.LastChild; + while (child != null) + { + stack.Push(child); + child = child.PreviousSibling; + } + } } } From f1b736f91825f289b60723e123b46f1a1195e1ec Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 19:19:42 +0100 Subject: [PATCH 5/7] Add Test for Descendants order --- src/Markdig.Tests/Markdig.Tests.csproj | 1 + src/Markdig.Tests/TestDescendantsOrder.cs | 79 +++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/Markdig.Tests/TestDescendantsOrder.cs diff --git a/src/Markdig.Tests/Markdig.Tests.csproj b/src/Markdig.Tests/Markdig.Tests.csproj index 23801f21..c461cd76 100644 --- a/src/Markdig.Tests/Markdig.Tests.csproj +++ b/src/Markdig.Tests/Markdig.Tests.csproj @@ -58,6 +58,7 @@ + diff --git a/src/Markdig.Tests/TestDescendantsOrder.cs b/src/Markdig.Tests/TestDescendantsOrder.cs new file mode 100644 index 00000000..fd38a0b5 --- /dev/null +++ b/src/Markdig.Tests/TestDescendantsOrder.cs @@ -0,0 +1,79 @@ +using NUnit.Framework; +using Markdig.Syntax; +using Markdig.Syntax.Inlines; +using System.Linq; +using System.Collections.Generic; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestDescendantsOrder + { + [Test] + [TestCase("Foo *bar* ***foo***\n\nHello world\n\n# Header 123\n\n`Something`\n\n1. Test `abc` *def*\n2. ghi\n```c#\nEmpty\n```\nTest\n")] + // Note - Tested agains the entire tests dataset when implemented + public void AssertSameDescendantsOrder(string markdown) + { + var syntaxTree = Markdown.Parse(markdown, new MarkdownPipelineBuilder().UseAdvancedExtensions().Build()); + + var descendants_legacy = Descendants_Legacy(syntaxTree).ToList(); + var descendants_new = syntaxTree.Descendants().ToList(); + + Assert.AreEqual(descendants_legacy.Count, descendants_new.Count); + + for (int i = 0; i < descendants_legacy.Count; i++) + { + Assert.AreSame(descendants_legacy[i], descendants_new[i]); + } + } + + private static IEnumerable Descendants_Legacy(MarkdownObject markdownObject) + { + // TODO: implement a recursiveless method + + var block = markdownObject as ContainerBlock; + if (block != null) + { + foreach (var subBlock in block) + { + yield return subBlock; + + foreach (var sub in Descendants_Legacy(subBlock)) + { + yield return sub; + } + + // Visit leaf block that have inlines + var leafBlock = subBlock as LeafBlock; + if (leafBlock?.Inline != null) + { + foreach (var subInline in Descendants_Legacy(leafBlock.Inline)) + { + yield return subInline; + } + } + } + } + else + { + var inline = markdownObject as ContainerInline; + if (inline != null) + { + var child = inline.FirstChild; + while (child != null) + { + var next = child.NextSibling; + yield return child; + + foreach (var sub in Descendants_Legacy(child)) + { + yield return sub; + } + + child = next; + } + } + } + } + } +} From b2a542b43e3a7598b5110c23e72771e082daaaea Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 18 Jan 2019 20:23:17 +0100 Subject: [PATCH 6/7] Simplify push condition --- src/Markdig/Syntax/MarkdownObjectExtensions.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Markdig/Syntax/MarkdownObjectExtensions.cs b/src/Markdig/Syntax/MarkdownObjectExtensions.cs index 702df296..c47b3688 100644 --- a/src/Markdig/Syntax/MarkdownObjectExtensions.cs +++ b/src/Markdig/Syntax/MarkdownObjectExtensions.cs @@ -33,10 +33,9 @@ namespace Markdig.Syntax while (stack.Count > 0) { var block = stack.Pop(); - var push = pushStack.Pop(); + if (pushStack.Pop()) yield return block; if (block is ContainerBlock containerBlock) { - if (push) yield return containerBlock; int subBlockIndex = containerBlock.Count; while (subBlockIndex-- > 0) { @@ -55,7 +54,6 @@ namespace Markdig.Syntax } else if (block is ContainerInline containerInline) { - if (push) yield return containerInline; var child = containerInline.LastChild; while (child != null) { @@ -64,7 +62,6 @@ namespace Markdig.Syntax child = child.PreviousSibling; } } - else yield return block; } } From 95cbf0e6d21953967df042995e04d716510d4269 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 19 Jan 2019 11:35:01 +0100 Subject: [PATCH 7/7] Cache Schema markdown in Tests --- src/Markdig.Tests/TestDescendantsOrder.cs | 12 +++++++++--- src/Markdig.Tests/TestParser.cs | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Markdig.Tests/TestDescendantsOrder.cs b/src/Markdig.Tests/TestDescendantsOrder.cs index fd38a0b5..643bb903 100644 --- a/src/Markdig.Tests/TestDescendantsOrder.cs +++ b/src/Markdig.Tests/TestDescendantsOrder.cs @@ -10,9 +10,15 @@ namespace Markdig.Tests public class TestDescendantsOrder { [Test] - [TestCase("Foo *bar* ***foo***\n\nHello world\n\n# Header 123\n\n`Something`\n\n1. Test `abc` *def*\n2. ghi\n```c#\nEmpty\n```\nTest\n")] - // Note - Tested agains the entire tests dataset when implemented - public void AssertSameDescendantsOrder(string markdown) + public void TestSchemas() + { + foreach (var markdown in TestParser.SpecsMarkdown) + { + AssertSameDescendantsOrder(markdown); + } + } + + private void AssertSameDescendantsOrder(string markdown) { var syntaxTree = Markdown.Parse(markdown, new MarkdownPipelineBuilder().UseAdvancedExtensions().Build()); diff --git a/src/Markdig.Tests/TestParser.cs b/src/Markdig.Tests/TestParser.cs index dbe24d40..237bd005 100644 --- a/src/Markdig.Tests/TestParser.cs +++ b/src/Markdig.Tests/TestParser.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Text.RegularExpressions; using Markdig.Extensions.JiraLinks; @@ -155,5 +156,20 @@ namespace Markdig.Tests html = html.Normalize(NormalizationForm.FormKD); return html; } + + public static readonly string[] SpecsMarkdown; + static TestParser() + { + string assemblyDir = Path.GetDirectoryName(typeof(TestParser).Assembly.Location); + string specsDir = Path.GetFullPath(Path.Combine(assemblyDir, "../../Specs")); + + var files = Directory.GetFiles(specsDir).Where(file => file.EndsWith(".md", StringComparison.Ordinal)).ToList(); + SpecsMarkdown = new string[files.Count]; + + for (int i = 0; i < files.Count; i++) + { + SpecsMarkdown[i] = File.ReadAllText(files[i]); + } + } } } \ No newline at end of file