123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace FastReport.Utils
- {
- /// <summary>
- /// Fast alternative of StringBuilder.
- /// </summary>
- public class FastString
- {
- #region Constants
- private const int INIT_CAPACITY = 32;
- #endregion Constants
- #region Fields
- /* private char[] chars = null;
- private int count = 0;
- private int capacity = 0;
- private List<char> repList = null;
- */
- private string result = "";
- StringBuilder sb;
- private bool done = false;
- #endregion Fields
- #region Properties
- /// <summary>
- /// Gets the Length of string.
- /// </summary>
- public int Length
- {
- get { return sb.Length; }
- set
- {
- sb.Length = value;
- done = false;
- }
- }
- /// <summary>
- /// Gets or sets the chars of string.
- /// </summary>
- /// <param name="index"></param>
- /// <returns>Char value</returns>
- public char this[int index]
- {
- get
- {
- if (index >= 0 && index < sb.Length)
- return sb[index];
- else
- throw new IndexOutOfRangeException();
- }
- set
- {
- if (index >= 0 && index < sb.Length)
- sb[index] = value;
- else
- throw new IndexOutOfRangeException();
- done = false;
- }
- }
- /// <summary>
- /// Gets StringBuilder
- /// </summary>
- public StringBuilder StringBuilder
- {
- get { return sb; }
- }
- #endregion Properties
- #region Private Methods
- /* /// <summary>
- /// Reallocate internal array.
- /// </summary>
- /// <param name="addLength">Additional length.</param>
- private void ReAlloc(int addLength)
- {
- if (count + addLength > capacity)
- {
- capacity = (capacity + addLength) > capacity * 2 ? capacity + addLength : capacity * 2;
- //Array.Resize<char>(ref chars, capacity);
- char[] newChars = new char[capacity];
- Array.Copy(chars, newChars, chars.Length);
- chars = newChars;
- }
- }*/
- /// <summary>
- /// Initialize the new array for chars.
- /// </summary>
- /// <param name="iniCapacity">Length of initial array.</param>
- private void Init(int iniCapacity)
- {
- sb = new StringBuilder(iniCapacity);
- //chars = new char[iniCapacity];
- //capacity = iniCapacity;
- }
- #endregion Private Methods
- #region Public Methods
- /// <summary>
- /// Checks the empty array.
- /// </summary>
- /// <returns>True if string is empty.</returns>
- public bool IsEmpty()
- {
- return sb.Length == 0;
- }
- /// <summary>
- /// Converts the array in string.
- /// </summary>
- /// <returns>String value.</returns>
- public override string ToString()
- {
- if (!done)
- {
- result = sb.ToString();
- //result = new string(chars, 0, count);
- done = true;
- }
- return result;
- }
- /// <summary>
- /// Clears the string.
- /// </summary>
- /// <returns>FastString object.</returns>
- public FastString Clear()
- {
- //count = 0;
- sb.Clear();
- done = false;
- return this;
- }
- /// <summary>
- /// Appends the string by string value.
- /// </summary>
- /// <param name="value">String value.</param>
- /// <returns>FastString object.</returns>
- public FastString Append(string value)
- {
- if (!String.IsNullOrEmpty(value))
- {
- /* ReAlloc(value.Length);
- value.CopyTo(0, chars, count, value.Length);
- count += value.Length;*/
- sb.Append(value);
- done = false;
- }
- return this;
- }
- /// <summary>
- /// Appends the string by string value.
- /// </summary>
- /// <param name="value">String value.</param>
- /// <returns>FastString object.</returns>
- public FastString AppendLine(string value)
- {
- /* ReAlloc(value.Length + 2);
- value.CopyTo(0, chars, count, value.Length);
- count += value.Length + 2;
- chars[count - 2] = '\r';
- chars[count - 1] = '\n';*/
- sb.AppendLine(value);
- done = false;
- return this;
- }
- /// <summary>
- /// Append formatted string.
- /// </summary>
- /// <param name="format"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- public FastString AppendFormat(String format, params Object[] args)
- {
- /* string value = String.Format(format, args);
- ReAlloc(value.Length);
- value.CopyTo(0, chars, count, value.Length);
- count += value.Length;*/
- sb.AppendFormat(format, args);
- done = false;
- return this;
- }
- /// <summary>
- /// Appends new line.
- /// </summary>
- /// <returns>FastString object.</returns>
- public FastString AppendLine()
- {
- /* ReAlloc(2);
- count += 2;
- chars[count - 2] = '\r';
- chars[count - 1] = '\n';*/
- sb.AppendLine();
- done = false;
- return this;
- }
- /// <summary>
- /// Appends the string by char value.
- /// </summary>
- /// <param name="value">Char value.</param>
- /// <returns>FastString object.</returns>
- public FastString Append(char value)
- {
- //ReAlloc(1);
- //chars[count++] = value;
- sb.Append(value);
- done = false;
- return this;
- }
- /// <summary>
- /// Appends the another FastString object.
- /// </summary>
- /// <param name="fastString">FastString object.</param>
- /// <returns>FastString object.</returns>
- public FastString Append(FastString fastString)
- {
- if (fastString != null)
- {
- sb.Append(fastString.StringBuilder);
- done = false;
- }
- return this;
- }
- /// <summary>
- /// Appends the string by object data.
- /// </summary>
- /// <param name="value">Object value.</param>
- /// <returns>FastString object.</returns>
- public FastString Append(object value)
- {
- sb.Append(value);
- done = false;
- //Append(value.ToString());
- return this;
- }
- /// <summary>
- /// Copies the substring in char array.
- /// </summary>
- /// <param name="sourceIndex">Start index in source.</param>
- /// <param name="destination">Destination array.</param>
- /// <param name="destinationIndex">Destination index.</param>
- /// <param name="count">Count of chars</param>
- public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
- {
- sb.CopyTo(sourceIndex, destination, destinationIndex, count);
- //Array.Copy(chars, sourceIndex, destination, destinationIndex, count);
- }
- /// <summary>
- /// Removes substring.
- /// </summary>
- /// <param name="startIndex">Start index of removed string.</param>
- /// <param name="length">Length of removed string.</param>
- /// <returns>FastString object.</returns>
- public FastString Remove(int startIndex, int length)
- {
- sb.Remove(startIndex, length);
- /*
- if (startIndex >= 0 && (length + startIndex) <= count)
- {
- int len = count - length - startIndex;
-
- for (int i = 0; i < len; i++)
- chars[startIndex + i] = chars[startIndex + i + length];
- count -= length;
- done = false;
- }*/
- done = false;
- return this;
- }
- /// <summary>
- /// Inserts string.
- /// </summary>
- /// <param name="startIndex">Start index in existing string.</param>
- /// <param name="value">Value of inserting string.</param>
- /// <returns>FastString object.</returns>
- public FastString Insert(int startIndex, string value)
- {
- sb.Insert(startIndex, value);
- /*
- if (value != null && value.Length > 0 && startIndex >= 0 && startIndex <= count)
- {
- int countToMove = count - startIndex;
- ReAlloc(value.Length);
- count += value.Length;
- for (int i = count - 1; i > count - countToMove - 1; i--)
- chars[i] = chars[i - value.Length];
- value.CopyTo(0, chars, startIndex, value.Length);
- }*/
- done = false;
- return this;
- }
- /// <summary>
- /// Replacing the substring on other.
- /// </summary>
- /// <param name="oldValue">Old string value.</param>
- /// <param name="newValue">New string value.</param>
- /// <returns>FastString object.</returns>
- public FastString Replace(string oldValue, string newValue)
- {
- sb.Replace(oldValue, newValue);
- /*
- if (count == 0)
- return this;
- if (repList == null)
- repList = new List<char>();
- for (int i = 0; i < count; i++)
- {
- bool isRep = false;
- if (chars[i] == oldValue[0])
- {
- int k = 1;
- while (k < oldValue.Length && chars[i + k] == oldValue[k])
- k++;
- isRep = (k >= oldValue.Length);
- }
- if (isRep)
- {
- i += oldValue.Length - 1;
- if (newValue != null)
- for (int k = 0; k < newValue.Length; k++)
- repList.Add(newValue[k]);
- }
- else
- repList.Add(chars[i]);
- }
- ReAlloc(repList.Count - count);
- for (int k = 0; k < repList.Count; k++)
- chars[k] = repList[k];
- count = repList.Count;
- repList.Clear();
- */
- done = false;
- return this;
- }
- /// <summary>
- /// Index of substring.
- /// </summary>
- /// <param name="value">Substring for search.</param>
- /// <param name="startIndex">Sarting position for search.</param>
- /// <returns>Position of substring.</returns>
- public int IndexOf(string value, int startIndex)
- {
- if (!String.IsNullOrEmpty(value) &&
- startIndex >= 0 &&
- startIndex < sb.Length)
- {
- int valueIndex = 0;
- for (int i = startIndex; i < sb.Length; i++)
- {
- if (sb[i] == value[valueIndex])
- {
- valueIndex++;
- if (valueIndex == value.Length)
- return i;
- }
- else
- valueIndex = 0;
- }
- }
- return -1;
- }
- /// <summary>
- /// Compare of substring in position.
- /// </summary>
- /// <param name="startIndex">Starting index for comparsion.</param>
- /// <param name="value">Value for compare.</param>
- /// <returns>True if substring is identical in position.</returns>
- public bool SubstringCompare(int startIndex, string value)
- {
- if (!String.IsNullOrEmpty(value) &&
- startIndex >= 0 &&
- startIndex < sb.Length)
- {
- int valueIndex = 0;
- for (int i = 0; i < value.Length; i++)
- {
- if (sb[startIndex + i] == value[i])
- {
- valueIndex++;
- if (valueIndex == value.Length)
- return true;
- }
- else
- valueIndex = 0;
- }
- }
- return false;
- }
- /// <summary>
- /// Returns the substring.
- /// </summary>
- /// <param name="startIndex">Starting index.</param>
- /// <param name="length">Length of substring.</param>
- /// <returns>Substring.</returns>
- public string Substring(int startIndex, int length)
- {
- char[] result = new char[length];
- sb.CopyTo(startIndex, result, 0, length);
- return new string(result);
- /* if (startIndex + length > count)
- return new string(chars, startIndex, count - startIndex);
- else
- return new string(chars, startIndex, length);*/
- }
- #endregion Public Methods
- #region Constructors
- /// <summary>
- /// Creates the new FastString object with initial capacity.
- /// </summary>
- /// <param name="initCapacity">Initial capacity.</param>
- public FastString(int initCapacity)
- {
- Init(initCapacity);
- }
- /// <summary>
- /// Creates the new FastString object with default capacity.
- /// </summary>
- public FastString()
- {
- Init(INIT_CAPACITY);
- }
- /// <summary>
- /// Creates the new FastString object from initial string.
- /// </summary>
- /// <param name="initValue"></param>
- public FastString(string initValue)
- {
- if (!string.IsNullOrEmpty(initValue))
- {
- Init(initValue.Length);
- Append(initValue);
- }
- else
- Init(0);
- }
- #endregion Constructors
- }
- public class FastStringWithPool : FastString
- {
- Dictionary<string, string> pool;
- public FastStringWithPool(Dictionary<string, string> pool) : base()
- {
- this.pool = pool;
- }
- public override string ToString()
- {
- string baseResult = base.ToString();
- string result;
- if (pool.TryGetValue(baseResult, out result))
- return result;
- return pool[baseResult] = baseResult;
- }
- }
- }
|