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

news/2024/6/17 19:06:04
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


http://www.niftyadmin.cn/n/1160007.html

相关文章

Web API入门指南(安全)转

安全检测的工具站点&#xff1a;https://www.owasp.org/index.php/Category:Vulnerability_Scanning_Tools Web API入门指南有些朋友回复问了些安全方面的问题&#xff0c;安全方面可以写的东西实在太多了&#xff0c;这里尽量围绕着Web API的安全性来展开&#xff0c;介绍一些…

JS学习之页面加载

1、window.opener.location.reload();意思是让打开的父窗口刷新。window.opener指的是本窗口的父窗口&#xff0c;window.opener.location.href只是一个链接&#xff0c;如果想实现父窗口的提交就调用&#xff0c;window.opener.action"" 和window.opener.submit(); …

c# 活动窗口 键盘监听

/// /// 監視クラス /// class HotKey { [DllImport("user32.dll ", SetLastError true)] public static extern bool RegisterHotKey( IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk ); [DllImport("user32.dll ", SetLastError true)] publ…

Node.js笔记2

入门二 5. 事件 Node.js中所有的异步I/O操作完成时都会发送一个事件到事件队列。 Events 事件模块 events.EventEmitter 简单用法&#xff1a; var EventEmitter require(events).EventEmitter; var event new EventEmitter(); event.on(some_event, function() { console.lo…

N年的经验在别人眼里是怎么看的?

很多人简历上都喜欢总结这么一句话&#xff1a;N年XXX领域的经验。N值越大&#xff0c;似乎越NB。 可是&#xff0c;我怎么看到很多人做了几十年的饭&#xff0c;水平变化不大&#xff0c;和专业厨师还是差得远。 还有写一辈子字的&#xff0c;开一辈子车的&#xff0c;这些人也…

LAMP兄弟连 视频教程集

电驴的资源&#xff1a;http://www.verycd.com/topics/2843130/?refmsg转载于:https://www.cnblogs.com/SunboyL/p/3345061.html

兔子的数量

一对小兔子一年后长成大兔子&#xff1b;一对大兔子每半年生一对小兔子。大兔子的繁殖期为4年&#xff0c;兔子的寿命是6年。假定第一年年初投放了一对小兔子&#xff0c;试编程计算&#xff0c;第n年末总共会有多少对兔子。 using System;using System.Text.RegularExpression…

谈谈RPC中的异步调用设计

RPC&#xff08;远过程调用&#xff09;在分布式系统中是很常用的基础通讯手段&#xff0c;核心思想是将不同进程之间的通讯抽象为函数调用&#xff0c;基本的过程是调用端通过将参数序列化到流中并发送给服务端&#xff0c;服务端从流中反序列化出参数并完成实际的处理&#x…