src/Markdig/Helpers/HtmlHelper.cs
/// <summary>
/// Destructively unescape a string: remove backslashes before punctuation or symbol characters.
/// </summary>
/// <param name="text">The string data that will be changed by unescaping any punctuation or symbol characters.</param>
/// <param name="removeBackSlash">if set to <c>true</c> [remove back slash].</param>
/// <returns></returns>
Destructively unescape a string: remove backslashes before punctuation or symbol characters.
Destructively unescape a string : remove backslashes before punctuation or symbol characters .
public static string Unescape(string? text, bool removeBackSlash = true)
        {
            if (string.IsNullOrEmpty(text))
            {
                return string.Empty;
            }
            int searchPos = 0;
            int lastPos = 0;
            char c;
            char[] search = removeBackSlash ? SearchBackAndAmp : SearchAmp;
            StringBuilder? sb = null;
            while ((searchPos = text!.IndexOfAny(search, searchPos)) != -1)
            {
                sb ??= StringBuilderCache.Local();
                c = text[searchPos];
                if (removeBackSlash && c == '\\')
                {
                    searchPos++;
                    if (text.Length == searchPos)
                        break;
                    c = text[searchPos];
                    if (c.IsEscapableSymbol())
                    {
                        sb.Append(text, lastPos, searchPos - lastPos - 1);
                        lastPos = searchPos;
                    }
                }
                else if (c == '&')
                {
                    var match = ScanEntity(new StringSlice(text, searchPos, text.Length - 1), out int numericEntity, out int entityNameStart, out int entityNameLength);
                    if (match == 0)
                    {
                        searchPos++;
                    }
                    else
                    {
                        searchPos += match;
                        if (entityNameLength > 0)
                        {
                            var decoded = EntityHelper.DecodeEntity(text.AsSpan(entityNameStart, entityNameLength));
                            if (decoded != null)
                            {
                                sb.Append(text, lastPos, searchPos - match - lastPos);
                                sb.Append(decoded);
                                lastPos = searchPos;
                            }
                        }
                        else if (numericEntity >= 0)
                        {
                            sb.Append(text, lastPos, searchPos - match - lastPos);
                            EntityHelper.DecodeEntity(numericEntity, sb);
                            lastPos = searchPos;
                        }
                    }
                }
            }
            if (sb is null || lastPos == 0)
                return text;
            sb.Append(text, lastPos, text.Length - lastPos);
            return sb.GetStringAndReset();
        }
public static string Unescape ( string ? text , bool removeBackSlash = true ) { if ( string . IsNullOrEmpty ( text ) ) { return string . Empty ; } int searchPos = 0 ; int lastPos = 0 ; char c ; char [ ] search = removeBackSlash ? SearchBackAndAmp : SearchAmp ; StringBuilder ? sb = null ; while ( ( searchPos = text ! . IndexOfAny ( search , searchPos ) ) != - 1 ) { sb ??= StringBuilderCache . Local ( ) ; c = text [ searchPos ] ; if ( removeBackSlash && c == ' \\ ' ) { searchPos ++ ; if ( text . Length == searchPos ) break ; c = text [ searchPos ] ; if ( c . IsEscapableSymbol ( ) ) { sb . Append ( text , lastPos , searchPos - lastPos - 1 ) ; lastPos = searchPos ; } } else if ( c == ' ' ) { var match = ScanEntity ( new StringSlice ( text , searchPos , text . Length - 1 ) , out int numericEntity , out int entityNameStart , out int entityNameLength ) ; if ( match == 0 ) { searchPos ++ ; } else { searchPos += match ; if ( entityNameLength > 0 ) { var decoded = EntityHelper . DecodeEntity ( text . AsSpan ( entityNameStart , entityNameLength ) ) ; if ( decoded != null ) { sb . Append ( text , lastPos , searchPos - match - lastPos ) ; sb . Append ( decoded ) ; lastPos = searchPos ; } } else if ( numericEntity >= 0 ) { sb . Append ( text , lastPos , searchPos - match - lastPos ) ; EntityHelper . DecodeEntity ( numericEntity , sb ) ; lastPos = searchPos ; } } } } if ( sb is null || lastPos == 0 ) return text ; sb . Append ( text , lastPos , text . Length - lastPos ) ; return sb . GetStringAndReset ( ) ; }
Destructively unescape a string: remove backslashes before punctuation or symbol characters.
Destructively unescape a string : remove backslashes before punctuation or symbol characters .
// Credits: code from CommonMark.NET // Copyright (c) 2014, Kārlis Gaņģis All rights reserved.  // See license for details:  https://github.com/Knagis/CommonMark.NET/blob/master/LICENSE.md // remove backslashes before punctuation chars:
None
text
string?
The string data that will be changed by unescaping any punctuation or symbol characters.
The string data that will be changed by unescaping any punctuation or symbol characters .
removeBackSlash
bool
if set to true
if set to true