Esempio n. 1
0
 private string GetSubstitute(char ch)
 {
     return(AsciiEncoderFallback.GetCharacterFallback(ch));
 }
Esempio n. 2
0
        /// <summary>
        /// Encodes the character.
        /// </summary>
        /// <param name="ch">The character to encode.</param>
        /// <param name="outputBuffer">The output buffer.</param>
        /// <param name="outputBufferCount">The output buffer count.</param>
        /// <param name="outputEnd">The output end.</param>
        /// <returns>
        /// <c>true</c> if encoding is successful; otherwise, <c>false</c>.
        /// </returns>
        bool IFallback.FallBackChar(char ch, char[] outputBuffer, ref int outputBufferCount, int outputEnd)
        {
            if (this.htmlEscape)
            {
                HtmlEntityIndex namedEntityId = 0;

                if (ch <= '>')
                {
                    if (ch == '>')
                    {
                        namedEntityId = HtmlEntityIndex.gt;
                    }
                    else if (ch == '<')
                    {
                        namedEntityId = HtmlEntityIndex.lt;
                    }
                    else if (ch == '&')
                    {
                        namedEntityId = HtmlEntityIndex.amp;
                    }
                    else if (ch == '\"')
                    {
                        namedEntityId = HtmlEntityIndex.quot;
                    }
                }
                else if ((char)0xA0 <= ch && ch <= (char)0xFF)
                {
                    namedEntityId = HtmlSupport.EntityMap[(int)ch - 0xA0];
                }

                if ((int)namedEntityId != 0)
                {
                    string strQuote = HtmlNameData.entities[(int)namedEntityId].name;

                    if (outputEnd - outputBufferCount < strQuote.Length + 2)
                    {
                        return(false);
                    }

                    outputBuffer[outputBufferCount++] = '&';
                    strQuote.CopyTo(0, outputBuffer, outputBufferCount, strQuote.Length);
                    outputBufferCount += strQuote.Length;
                    outputBuffer[outputBufferCount++] = ';';
                }
                else
                {
                    uint value = (uint)ch;
                    int  len   = (value < 0x10) ? 1 : (value < 0x100) ? 2 : (value < 0x1000) ? 3 : 4;
                    if (outputEnd - outputBufferCount < len + 4)
                    {
                        return(false);
                    }

                    outputBuffer[outputBufferCount++] = '&';
                    outputBuffer[outputBufferCount++] = '#';
                    outputBuffer[outputBufferCount++] = 'x';

                    int offset = outputBufferCount + len;
                    while (value != 0)
                    {
                        uint digit = value & 0xF;
                        outputBuffer[--offset] = (char)(digit + (digit < 10 ? '0' : 'A' - 10));
                        value >>= 4;
                    }

                    outputBufferCount += len;

                    outputBuffer[outputBufferCount++] = ';';
                }
            }
            else
            {
                string substitute = AsciiEncoderFallback.GetCharacterFallback(ch);

                if (substitute != null)
                {
                    if (outputEnd - outputBufferCount < substitute.Length)
                    {
                        return(false);
                    }

                    substitute.CopyTo(0, outputBuffer, outputBufferCount, substitute.Length);
                    outputBufferCount += substitute.Length;
                }
                else
                {
                    InternalDebug.Assert(outputEnd - outputBufferCount > 0);
                    outputBuffer[outputBufferCount++] = ch;
                }
            }

            return(true);
        }
 /// <summary>
 /// Encodes the character.
 /// </summary>
 /// <param name="ch">The character to encode.</param>
 /// <param name="outputBuffer">The output buffer.</param>
 /// <param name="outputBufferCount">The output buffer count.</param>
 /// <param name="outputEnd">The output end.</param>
 /// <returns>
 /// <c>true</c> if encoding is successful; otherwise, <c>false</c>.
 /// </returns>
 bool IFallback.FallBackChar(char ch, char[] outputBuffer, ref int outputBufferCount, int outputEnd)
 {
     if (htmlEscape)
     {
         HtmlEntityIndex htmlEntityIndex = (HtmlEntityIndex)0;
         if (ch <= '>')
         {
             if (ch == '>')
             {
                 htmlEntityIndex = HtmlEntityIndex.gt;
             }
             else if (ch == '<')
             {
                 htmlEntityIndex = HtmlEntityIndex.lt;
             }
             else if (ch == '&')
             {
                 htmlEntityIndex = HtmlEntityIndex.amp;
             }
             else if (ch == '"')
             {
                 htmlEntityIndex = HtmlEntityIndex.quot;
             }
         }
         else if ('\u00a0' <= ch && ch <= 'ÿ')
         {
             htmlEntityIndex = HtmlSupport.EntityMap[(int)(ch - '\u00a0')];
         }
         if (htmlEntityIndex != (HtmlEntityIndex)0)
         {
             string name = HtmlNameData.entities[(int)htmlEntityIndex].name;
             if (outputEnd - outputBufferCount < name.Length + 2)
             {
                 return(false);
             }
             outputBuffer[outputBufferCount++] = '&';
             name.CopyTo(0, outputBuffer, outputBufferCount, name.Length);
             outputBufferCount += name.Length;
             outputBuffer[outputBufferCount++] = ';';
         }
         else
         {
             uint num  = (uint)ch;
             int  num2 = (num < 16u) ? 1 : ((num < 256u) ? 2 : ((num < 4096u) ? 3 : 4));
             if (outputEnd - outputBufferCount < num2 + 4)
             {
                 return(false);
             }
             outputBuffer[outputBufferCount++] = '&';
             outputBuffer[outputBufferCount++] = '#';
             outputBuffer[outputBufferCount++] = 'x';
             int num3 = outputBufferCount + num2;
             while (num != 0u)
             {
                 uint num4 = num & 15u;
                 outputBuffer[--num3] = (char)((ulong)num4 + (ulong)((num4 < 10u) ? 48L : 55L));
                 num >>= 4;
             }
             outputBufferCount += num2;
             outputBuffer[outputBufferCount++] = ';';
         }
     }
     else
     {
         string characterFallback = AsciiEncoderFallback.GetCharacterFallback(ch);
         if (characterFallback != null)
         {
             if (outputEnd - outputBufferCount < characterFallback.Length)
             {
                 return(false);
             }
             characterFallback.CopyTo(0, outputBuffer, outputBufferCount, characterFallback.Length);
             outputBufferCount += characterFallback.Length;
         }
         else
         {
             outputBuffer[outputBufferCount++] = ch;
         }
     }
     return(true);
 }