Example #1
0
 public void WriteComplete(char[] buffer, int offset, int count, IFallback fallback)
 {
     if (fallback != null || lineModeEncoding)
     {
         byte   b     = 0;
         byte[] array = null;
         uint   num   = 0u;
         bool   flag  = false;
         bool   flag2 = false;
         if (fallback != null)
         {
             array = fallback.GetUnsafeAsciiMap(out b);
             if (array != null)
             {
                 num = (uint)array.Length;
             }
             flag  = fallback.HasUnsafeUnicode();
             flag2 = fallback.TreatNonAsciiAsUnsafe(encoding.WebName);
         }
         while (count != 0)
         {
             while (count != 0 && lineBufferCount != lineBuffer.Length)
             {
                 char c = buffer[offset];
                 if (fallback != null && (((uint)c < num && (array[(int)c] & b) != 0) || (!encodingCompleteUnicode && (c >= '\u007f' || c < ' ') && codePageMap.IsUnsafeExtendedCharacter(c)) || (flag && c >= '\u007f' && (flag2 || fallback.IsUnsafeUnicode(c, isFirstChar)))))
                 {
                     if (!fallback.FallBackChar(c, lineBuffer, ref lineBufferCount, lineBuffer.Length))
                     {
                         break;
                     }
                     isFirstChar = false;
                 }
                 else
                 {
                     lineBuffer[lineBufferCount++] = c;
                     isFirstChar = false;
                     if (lineModeEncoding)
                     {
                         if (c == '\n' || c == '\r')
                         {
                             lineBufferLastNL = lineBufferCount;
                         }
                         else if (lineBufferLastNL > lineBuffer.Length - 256)
                         {
                             count--;
                             offset++;
                             break;
                         }
                     }
                 }
                 count--;
                 offset++;
             }
             if (lineModeEncoding && (lineBufferLastNL > lineBuffer.Length - 256 || (lineBufferCount > lineBuffer.Length - 32 && lineBufferLastNL != 0)))
             {
                 EncodeBuffer(lineBuffer, 0, lineBufferLastNL, false);
                 lineBufferCount -= lineBufferLastNL;
                 if (lineBufferCount != 0)
                 {
                     Buffer.BlockCopy(lineBuffer, lineBufferLastNL * 2, lineBuffer, 0, lineBufferCount * 2);
                 }
             }
             else if (lineBufferCount > lineBuffer.Length - Math.Max(minCharsEncode, 32))
             {
                 EncodeBuffer(lineBuffer, 0, lineBufferCount, false);
                 lineBufferCount = 0;
             }
             lineBufferLastNL = 0;
         }
         return;
     }
     if (count > minCharsEncode)
     {
         if (lineBufferCount != 0)
         {
             EncodeBuffer(lineBuffer, 0, lineBufferCount, false);
             lineBufferCount  = 0;
             lineBufferLastNL = 0;
         }
         EncodeBuffer(buffer, offset, count, false);
         return;
     }
     Buffer.BlockCopy(buffer, offset * 2, lineBuffer, lineBufferCount * 2, count * 2);
     lineBufferCount += count;
     if (lineBufferCount > lineBuffer.Length - minCharsEncode)
     {
         EncodeBuffer(lineBuffer, 0, lineBufferCount, false);
         lineBufferCount  = 0;
         lineBufferLastNL = 0;
     }
 }
        public void WriteComplete(char[] buffer, int offset, int count, IFallback fallback)
        {
            InternalDebug.Assert(!endOfFile);
            InternalDebug.Assert(encoding != null);

            if (fallback != null || lineModeEncoding)
            {
                byte   unsafeAsciiMask       = 0;
                byte[] unsafeAsciiMap        = null;
                uint   unsafeAsciiMapLength  = 0;
                var    hasUnsafeUnicode      = false;
                var    treatNonAsciiAsUnsafe = false;

                if (fallback != null)
                {
                    unsafeAsciiMap = fallback.GetUnsafeAsciiMap(out unsafeAsciiMask);
                    if (unsafeAsciiMap != null)
                    {
                        unsafeAsciiMapLength = (uint)unsafeAsciiMap.Length;
                    }

                    hasUnsafeUnicode      = fallback.HasUnsafeUnicode();
                    treatNonAsciiAsUnsafe = fallback.TreatNonAsciiAsUnsafe(encoding.WebName);
                }

                while (0 != count)
                {
                    for (; 0 != count && lineBufferCount != lineBuffer.Length; count--, offset++)
                    {
                        var ch = buffer[offset];

                        if (fallback != null)
                        {
                            if (((uint)ch < unsafeAsciiMapLength && (unsafeAsciiMap[(int)ch] & unsafeAsciiMask) != 0) ||
                                (!encodingCompleteUnicode && (ch >= 0x7F || ch < ' ') && codePageMap.IsUnsafeExtendedCharacter(ch)) ||
                                (hasUnsafeUnicode && ch >= 0x7F && (treatNonAsciiAsUnsafe || fallback.IsUnsafeUnicode(ch, isFirstChar))))
                            {
                                if (!fallback.FallBackChar(ch, lineBuffer, ref lineBufferCount, lineBuffer.Length))
                                {
                                    break;
                                }
                                isFirstChar = false;
                                continue;
                            }
                        }



                        lineBuffer[lineBufferCount++] = ch;
                        isFirstChar = false;

                        if (lineModeEncoding)
                        {
                            if (ch == '\n' || ch == '\r')
                            {
                                lineBufferLastNL = lineBufferCount;
                            }
                            else if (lineBufferLastNL > lineBuffer.Length - LineSpaceThreshold)
                            {
                                count--;
                                offset++;

                                break;
                            }
                        }
                    }



                    if (lineModeEncoding &&
                        (lineBufferLastNL > lineBuffer.Length - LineSpaceThreshold ||
                         (lineBufferCount > lineBuffer.Length - SpaceThreshold &&
                          lineBufferLastNL != 0)))
                    {
                        EncodeBuffer(lineBuffer, 0, lineBufferLastNL, false);

                        lineBufferCount -= lineBufferLastNL;

                        if (lineBufferCount != 0)
                        {
                            Buffer.BlockCopy(lineBuffer, lineBufferLastNL * 2, lineBuffer, 0, lineBufferCount * 2);
                        }
                    }
                    else if (lineBufferCount > lineBuffer.Length - Math.Max(minCharsEncode, SpaceThreshold))
                    {
                        EncodeBuffer(lineBuffer, 0, lineBufferCount, false);

                        lineBufferCount = 0;
                    }

                    lineBufferLastNL = 0;
                }
            }
            else
            {
                if (count > minCharsEncode)
                {
                    if (lineBufferCount != 0)
                    {
                        EncodeBuffer(lineBuffer, 0, lineBufferCount, false);

                        lineBufferCount  = 0;
                        lineBufferLastNL = 0;
                    }



                    EncodeBuffer(buffer, offset, count, false);
                }
                else
                {
                    InternalDebug.Assert(lineBufferCount + count <= lineBuffer.Length);



                    Buffer.BlockCopy(buffer, offset * 2, lineBuffer, lineBufferCount * 2, count * 2);
                    lineBufferCount += count;

                    if (lineBufferCount > lineBuffer.Length - minCharsEncode)
                    {
                        EncodeBuffer(lineBuffer, 0, lineBufferCount, false);

                        lineBufferCount  = 0;
                        lineBufferLastNL = 0;
                    }
                }
            }
        }