精简版StringBuilder,提速字符串拼接 QuickStringWriter完整代码

using System;
using System.Collections.Generic;
using System.Text;

namespace blqw
{
    public class QuickStringWriter : IDisposable
    {
        public QuickStringWriter() : this(2048) { }
        /// <summary>
        /// 实例化新的对象,并且指定初始容量
        /// </summary>
        /// <param name="capacity"></param>
        public QuickStringWriter(int capacity)
        {
            NumberBuff = new Char[64];
            Buff = new Char[capacity];
        }

        //设置缓冲区容量
        void SetCapacity(int capacity)
        {
            if (capacity > Buff.Length)
            {
                if (capacity > 6000 * 10000)   //6000W
                {
                    throw new OutOfMemoryException("QuickStringWriter容量不能超过6000万个字符");
                }
            }
            var newbuff = new char[capacity];
            Array.Copy(Buff, 0, newbuff, 0, Math.Min(Position, capacity));
            Buff = newbuff;
            Position = Math.Min(Position, Buff.Length);
        }
        //当容量不足的时候,尝试翻倍空间
        void ToDouble()
        {
            SetCapacity(Math.Min(Buff.Length * 2, 10 * 10000));
        }

        Char[] NumberBuff;
        Char[] Buff;
        int Position;

        public void Dispose()
        {
            NumberBuff = null;
            Buff = null;
        }

        public QuickStringWriter Append(Boolean val)
        {
            if (val)
            {
                Check(4);
                Buff[Position++] = 't';
                Buff[Position++] = 'r';
                Buff[Position++] = 'u';
                Buff[Position++] = 'e';
            }
            else
            {
                Check(5);
                Buff[Position++] = 'f';
                Buff[Position++] = 'a';
                Buff[Position++] = 'l';
                Buff[Position++] = 's';
                Buff[Position++] = 'e';
            }
            return this;
        }
        public QuickStringWriter Append(DateTime val)
        {
            Check(18);
            if (val.Year < 1000)
            {
                Buff[Position++] = '0';
                if (val.Year < 100)
                {
                    Buff[Position++] = '0';
                    if (val.Year < 10)
                    {
                        Buff[Position++] = '0';
                    }
                }
            }
            Append((long)val.Year);
            Buff[Position++] = '-';

            if (val.Month < 10)
            {
                Buff[Position++] = '0';
            }
            Append((long)val.Month);
            Buff[Position++] = '-';

            if (val.Day < 10)
            {
                Buff[Position++] = '0';
            }
            Append((long)val.Day);
            Buff[Position++] = ' ';

            if (val.Hour < 10)
            {
                Buff[Position++] = '0';
            }
            Append((long)val.Hour);
            Buff[Position++] = ':';

            if (val.Minute < 10)
            {
                Buff[Position++] = '0';
            }
            Append((long)val.Minute);
            Buff[Position++] = ':';

            if (val.Second < 10)
            {
                Buff[Position++] = '0';
            }
            Append((long)val.Minute);
            return this;
        }

        public QuickStringWriter Append(Guid val)
        {
            Append(val.ToString());
            return this;
        }

        public QuickStringWriter Append(DateTime val, string format)
        {

            Append(val.ToString(format));
            return this;
        }
        public QuickStringWriter Append(Guid val, string format)
        {
            Append(val.ToString(format));
            return this;
        }

        public QuickStringWriter Append(Decimal val)
        {
            Append(val.ToString());
            return this;
        }
        public QuickStringWriter Append(Double val)
        {
            Append(Convert.ToString(val));
            return this;
        }
        public QuickStringWriter Append(Single val)
        {
            Append(Convert.ToString(val));
            return this;
        }


        public QuickStringWriter Append(SByte val)
        {
            Append((Int64)val);
            return this;
        }
        public QuickStringWriter Append(Int16 val)
        {
            Append((Int64)val);
            return this;
        }
        public QuickStringWriter Append(Int32 val)
        {
            Append((Int64)val);
            return this;
        }

        public override string ToString()
        {
            return new string(Buff, 0, Position);
        }

        public QuickStringWriter Append(Int64 val)
        {
            if (val == 0)
            {
                Buff[Position++] = '0';
                return this;
            }

            var pos = 63;
            if (val < 0)
            {
                Buff[Position++] = '-';
                NumberBuff[pos] = (char)(~(val % 10) + '1');
                if (val < -10)
                {
                    val = val / -10;
                    NumberBuff[--pos] = (char)(val % 10 + '0');
                }
            }
            else
            {
                NumberBuff[pos] = (char)(val % 10 + '0');
            }
            while ((val = val / 10L) != 0L)
            {
                NumberBuff[--pos] = (char)(val % 10L + '0');
            }
            var length = 64 - pos;
            Check(length);
            Array.Copy(NumberBuff, pos, Buff, Position, length);
            Position += length;
            return this;
        }
        public QuickStringWriter Append(Char val)
        {
            Try();
            Buff[Position++] = val;
            return this;
        }
        public QuickStringWriter Append(String val)
        {
            if (val == null || val.Length == 0)
            {
                return this;
            }
            else if (val.Length <= 3)
            {
                Check(val.Length);
                Buff[Position++] = val[0];
                if (val.Length > 1)
                {
                    Buff[Position++] = val[1];
                    if (val.Length > 2)
                    {
                        Buff[Position++] = val[2];
                    }
                }
            }
            else
            {
                Check(val.Length);
                val.CopyTo(0, Buff, Position, val.Length);
                Position += val.Length;
            }
            return this;
        }



        public QuickStringWriter Append(Byte val)
        {
            Append((UInt64)val);
            return this;
        }
        public QuickStringWriter Append(UInt16 val)
        {
            Append((UInt64)val);
            return this;
        }
        public QuickStringWriter Append(UInt32 val)
        {
            Append((UInt64)val);
            return this;
        }
        public QuickStringWriter Append(UInt64 val)
        {
            if (val == 0)
            {
                Buff[Position++] = '0';
                return this;
            }
            var pos = 63;

            NumberBuff[pos] = (char)(val % 10 + '0');

            while ((val = val / 10L) != 0L)
            {
                NumberBuff[--pos] = (char)(val % 10L + '0');
            }
            var length = 64 - pos;
            Check(length);
            Array.Copy(NumberBuff, pos, Buff, Position, length);
            Position += length;
            return this;
        }

        public QuickStringWriter Clear()
        {
            Position = 0;
            return this;
        }

        //当容量不足的时候,尝试翻倍空间
        void Try()
        {
            if (Position >= Buff.Length)
            {
                ToDouble();
            }
        }
        //测试剩余空间大小,如果不足,则扩展至可用大小后再翻倍
        void Check(int count)
        {
            var pre = Position + count;
            if (pre >= Buff.Length)
            {
                SetCapacity(pre * 2);
            }
        }


    }
}

转自http://www.cnblogs.com/blqw/p/QuickStringWriter.html

原文地址:https://www.cnblogs.com/jizhongfong/p/3330234.html