Skip to content

Commit

Permalink
Remove char[], string allocations in HtmlBlockParser
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Mar 14, 2021
1 parent da0ba34 commit 00c175a
Showing 1 changed file with 70 additions and 70 deletions.
140 changes: 70 additions & 70 deletions src/Markdig/Parsers/HtmlBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private BlockState TryParseTagType16(BlockProcessor state, StringSlice line, int
c = line.NextChar();
}

var tag = new char[10];
Span<char> tag = stackalloc char[10];
var count = 0;
for (; count < tag.Length; count++)
{
Expand All @@ -152,13 +152,13 @@ private BlockState TryParseTagType16(BlockProcessor state, StringSlice line, int
return BlockState.None;
}

var tagName = new string(tag, 0, count);
var tagIndex = Array.BinarySearch(HtmlTags, tagName, StringComparer.Ordinal);
if (tagIndex < 0)
if (!HtmlTags.TryMatchExact(tag.Slice(0, count), out var match))
{
return BlockState.None;
}

int tagIndex = match.Value;

// Cannot start with </script </pre or </style
if ((tagIndex == 49 || tagIndex == 50 || tagIndex == 53))
{
Expand Down Expand Up @@ -283,73 +283,73 @@ private BlockState CreateHtmlBlock(BlockProcessor state, HtmlBlockType type, int
return BlockState.Continue;
}

private static readonly string[] HtmlTags =
private static readonly CompactPrefixTree<int> HtmlTags = new(65, 93, 82)
{
"address", // 0
"article", // 1
"aside", // 2
"base", // 3
"basefont", // 4
"blockquote", // 5
"body", // 6
"caption", // 7
"center", // 8
"col", // 9
"colgroup", // 10
"dd", // 11
"details", // 12
"dialog", // 13
"dir", // 14
"div", // 15
"dl", // 16
"dt", // 17
"fieldset", // 18
"figcaption", // 19
"figure", // 20
"footer", // 21
"form", // 22
"frame", // 23
"frameset", // 24
"h1", // 25
"h2", // 26
"h3", // 27
"h4", // 28
"h5", // 29
"h6", // 30
"head", // 31
"header", // 32
"hr", // 33
"html", // 34
"iframe", // 35
"legend", // 36
"li", // 37
"link", // 38
"main", // 39
"menu", // 40
"menuitem", // 41
"nav", // 42
"noframes", // 43
"ol", // 44
"optgroup", // 45
"option", // 46
"p", // 47
"param", // 48
"pre", // 49 <=== special group 1
"script", // 50 <=== special group 1
"section", // 51
"source", // 52
"style", // 53 <=== special group 1
"summary", // 54
"table", // 55
"tbody", // 56
"td", // 57
"tfoot", // 58
"th", // 59
"thead", // 60
"title", // 61
"tr", // 62
"track", // 63
"ul", // 64
{ "address", 0 },
{ "article", 1 },
{ "aside", 2 },
{ "base", 3 },
{ "basefont", 4 },
{ "blockquote", 5 },
{ "body", 6 },
{ "caption", 7 },
{ "center", 8 },
{ "col", 9 },
{ "colgroup", 10 },
{ "dd", 11 },
{ "details", 12 },
{ "dialog", 13 },
{ "dir", 14 },
{ "div", 15 },
{ "dl", 16 },
{ "dt", 17 },
{ "fieldset", 18 },
{ "figcaption", 19 },
{ "figure", 20 },
{ "footer", 21 },
{ "form", 22 },
{ "frame", 23 },
{ "frameset", 24 },
{ "h1", 25 },
{ "h2", 26 },
{ "h3", 27 },
{ "h4", 28 },
{ "h5", 29 },
{ "h6", 30 },
{ "head", 31 },
{ "header", 32 },
{ "hr", 33 },
{ "html", 34 },
{ "iframe", 35 },
{ "legend", 36 },
{ "li", 37 },
{ "link", 38 },
{ "main", 39 },
{ "menu", 40 },
{ "menuitem", 41 },
{ "nav", 42 },
{ "noframes", 43 },
{ "ol", 44 },
{ "optgroup", 45 },
{ "option", 46 },
{ "p", 47 },
{ "param", 48 },
{ "pre", 49 }, // <=== special group 1
{ "script", 50 }, // <=== special group 1
{ "section", 51 },
{ "source", 52 },
{ "style", 53 }, // <=== special group 1
{ "summary", 54 },
{ "table", 55 },
{ "tbody", 56 },
{ "td", 57 },
{ "tfoot", 58 },
{ "th", 59 },
{ "thead", 60 },
{ "title", 61 },
{ "tr", 62 },
{ "track", 63 },
{ "ul", 64 }
};
}
}

0 comments on commit 00c175a

Please sign in to comment.