JsonFx.BuildTools.HtmlDistiller.HtmlDistiller.DecodeHtmlEntity C# (CSharp) Method

DecodeHtmlEntity() public static method

Decodes HTML entities into special characters
public static DecodeHtmlEntity ( string source, int index, char &entity ) : int
source string
index int
entity char
return int
        public static int DecodeHtmlEntity(string source, int index, out char entity)
        {
            int Start = index;
            int End = source.Length-1;

            // consume '&'
            index++;

            if ((index < End) &&
                (source[index] == HtmlDistiller.EntityNumChar))
            {
                // entity is Unicode Code Point

                // consume '#'
                index++;

                bool isHex = false;
                if ((index < End) &&
                    (Char.ToLowerInvariant(source[index]) == HtmlDistiller.EntityHexChar))
                {
                    isHex = true;

                    // consume 'x'
                    index++;

                }

                int NumStart = index;
                while ((index < End) && (source[index] != ';'))
                {
                    char ch = Char.ToUpperInvariant(source[index]);
                    if (!Char.IsDigit(ch) &&
                        (isHex && (ch < HtmlDistiller.HexStartChar || ch > HtmlDistiller.HexEndChar)))
                    {
                        break;
                    }

                    index++;
                }

                int codePoint;
                if (Int32.TryParse(
                    source.Substring(NumStart, index-NumStart),
                    isHex ? NumberStyles.AllowHexSpecifier : NumberStyles.None,
                    CultureInfo.InvariantCulture,
                    out codePoint))
                {
                    entity = (char)codePoint;
                    if (source[index] == ';')
                    {
                        index++;
                    }
                    return index-Start;
                }
                else
                {
                    entity = EntityStartChar;
                    return 1;
                }
            }

            int NameStart = index;
            while ((index < End) && (source[index] != ';'))
            {
                if (!Char.IsLetter(source, index))
                {
                    break;
                }

                index++;
            }

            entity = HtmlDistiller.MapEntityName(source.Substring(NameStart, index-NameStart));
            if (entity == '\0')
            {
                entity = HtmlDistiller.EntityStartChar;
                return 1;
            }

            if (source[index] == ';')
            {
                index++;
            }
            return index-Start;
        }