Remove allocation in EntityHelper.DecodeEntity

This commit is contained in:
MihaZupan
2020-04-06 04:36:10 +02:00
parent 665f5f5e51
commit a8b6e2c2e4
5 changed files with 30 additions and 36 deletions

View File

@@ -17,9 +17,9 @@ namespace Markdig.Helpers
{
public const int TabSize = 4;
public const char ZeroSafeChar = '\uFFFD';
public const char ReplacementChar = '\uFFFD';
public const string ZeroSafeString = "\uFFFD";
public const string ReplacementCharString = "\uFFFD";
private const char HighSurrogateStart = '\ud800';
private const char HighSurrogateEnd = '\udbff';

View File

@@ -33,6 +33,7 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Markdig.Helpers
{
@@ -60,8 +61,8 @@ namespace Markdig.Helpers
/// <returns>The unicode character set or <c>null</c> if the entity was not recognized.</returns>
public static string DecodeEntity(int utf32)
{
if (!CharHelper.IsInInclusiveRange(utf32, 0, 1114111) || CharHelper.IsInInclusiveRange(utf32, 55296, 57343))
return null;
if (!CharHelper.IsInInclusiveRange(utf32, 1, 1114111) || CharHelper.IsInInclusiveRange(utf32, 55296, 57343))
return CharHelper.ReplacementCharString;
if (utf32 < 65536)
return char.ToString((char)utf32);
@@ -80,6 +81,24 @@ namespace Markdig.Helpers
});
}
public static void DecodeEntity(int utf32, StringBuilder sb)
{
if (!CharHelper.IsInInclusiveRange(utf32, 1, 1114111) || CharHelper.IsInInclusiveRange(utf32, 55296, 57343))
{
sb.Append(CharHelper.ReplacementChar);
}
else if (utf32 < 65536)
{
sb.Append((char)utf32);
}
else
{
utf32 -= 65536;
sb.Append((char)((uint)utf32 / 1024 + 55296));
sb.Append((char)((uint)utf32 % 1024 + 56320));
}
}
#region [ EntityMap ]
/// <summary>
/// Source: http://www.w3.org/html/wg/drafts/html/master/syntax.html#named-character-references

View File

@@ -450,11 +450,7 @@ namespace Markdig.Helpers
while ((searchPos = text.IndexOfAny(search, searchPos)) != -1)
{
if (sb == null)
{
sb = StringBuilderCache.Local();
sb.Length = 0;
}
sb ??= StringBuilderCache.Local();
c = text[searchPos];
if (removeBackSlash && c == '\\')
{
@@ -472,10 +468,7 @@ namespace Markdig.Helpers
}
else if (c == '&')
{
int entityNameStart;
int entityNameLength;
int numericEntity;
var match = ScanEntity(new StringSlice(text, searchPos, text.Length - 1), out numericEntity, out entityNameStart, out entityNameLength);
var match = ScanEntity(new StringSlice(text, searchPos, text.Length - 1), out int numericEntity, out int entityNameStart, out int entityNameLength);
if (match == 0)
{
searchPos++;
@@ -497,36 +490,18 @@ namespace Markdig.Helpers
else if (numericEntity >= 0)
{
sb.Append(text, lastPos, searchPos - match - lastPos);
if (numericEntity == 0)
{
sb.Append('\0'.EscapeInsecure());
}
else
{
var decoded = EntityHelper.DecodeEntity(numericEntity);
if (decoded != null)
{
sb.Append(decoded);
}
else
{
sb.Append('\uFFFD');
}
}
EntityHelper.DecodeEntity(numericEntity, sb);
lastPos = searchPos;
}
}
}
}
if (sb == null)
if (sb == null || lastPos == 0)
return text;
sb.Append(text, lastPos, text.Length - lastPos);
var result = sb.ToString();
sb.Length = 0;
return result;
return sb.GetStringAndReset();
}
/// <summary>

View File

@@ -39,7 +39,7 @@ namespace Markdig.Parsers.Inlines
}
else if (entityValue >= 0)
{
literal = (entityValue == 0 ? null : EntityHelper.DecodeEntity(entityValue)) ?? CharHelper.ZeroSafeString;
literal = EntityHelper.DecodeEntity(entityValue);
}
return literal != null;
}

View File

@@ -131,7 +131,7 @@ namespace Markdig.Parsers
/// <param name="text">The text to secure.</param>
private string FixupZero(string text)
{
return text.Replace('\0', CharHelper.ZeroSafeChar);
return text.Replace('\0', CharHelper.ReplacementChar);
}
private sealed class ContainerItemCache : DefaultObjectCache<ContainerItem>