Skip to content

Instantly share code, notes, and snippets.

@Athari
Created May 13, 2013 19:18
Show Gist options
  • Save Athari/5570726 to your computer and use it in GitHub Desktop.
Save Athari/5570726 to your computer and use it in GitHub Desktop.
Decompiled StringBuilder from .NET 3.5
// Type: System.Text.StringBuilder
// Assembly: mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// Assembly location: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
namespace System.Text
{
[ComVisible(true)]
[Serializable]
public sealed class StringBuilder : ISerializable
{
internal IntPtr m_currentThread = Thread.InternalGetCurrentThread();
internal const int DefaultCapacity = 16;
private const string CapacityField = "Capacity";
private const string MaxCapacityField = "m_MaxCapacity";
private const string StringValueField = "m_StringValue";
private const string ThreadIDField = "m_currentThread";
internal int m_MaxCapacity;
internal volatile string m_StringValue;
public int Capacity
{
get
{
return this.m_StringValue.Capacity;
}
set
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
if (value < 0)
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_NegativeCapacity"));
if (value < threadSafeString.Length)
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity"));
if (value > this.MaxCapacity)
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_Capacity"));
int capacity = threadSafeString.Capacity;
if (value == capacity)
return;
string forStringBuilder = string.GetStringForStringBuilder(threadSafeString, value);
this.ReplaceString(tid, forStringBuilder);
}
}
public int MaxCapacity
{
get
{
return this.m_MaxCapacity;
}
}
public int Length
{
get
{
return this.m_StringValue.Length;
}
set
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
if (value == 0)
{
threadSafeString.SetLength(0);
this.ReplaceString(tid, threadSafeString);
}
else
{
int length = threadSafeString.Length;
int num = value;
if (num < 0)
throw new ArgumentOutOfRangeException("newlength", Environment.GetResourceString("ArgumentOutOfRange_NegativeLength"));
if (num > this.MaxCapacity)
throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity"));
if (num == length)
return;
if (num <= threadSafeString.Capacity)
{
if (num > length)
{
for (int index = length; index < num; ++index)
threadSafeString.InternalSetCharNoBoundsCheck(index, char.MinValue);
}
threadSafeString.InternalSetCharNoBoundsCheck(num, char.MinValue);
threadSafeString.SetLength(num);
this.ReplaceString(tid, threadSafeString);
}
else
{
int capacity = num > threadSafeString.Capacity ? num : threadSafeString.Capacity;
string forStringBuilder = string.GetStringForStringBuilder(threadSafeString, capacity);
forStringBuilder.SetLength(num);
this.ReplaceString(tid, forStringBuilder);
}
}
}
}
[IndexerName("Chars")]
public char this[int index]
{
get
{
return this.m_StringValue[index];
}
set
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
threadSafeString.SetChar(index, value);
this.ReplaceString(tid, threadSafeString);
}
}
public StringBuilder()
: this(16)
{
}
public StringBuilder(int capacity)
: this(string.Empty, capacity)
{
}
public StringBuilder(string value)
: this(value, 16)
{
}
public StringBuilder(string value, int capacity)
: this(value, 0, value != null ? value.Length : 0, capacity)
{
}
public StringBuilder(string value, int startIndex, int length, int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity", string.Format((IFormatProvider) CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), new object[1]
{
(object) "capacity"
}));
else if (length < 0)
{
throw new ArgumentOutOfRangeException("length", string.Format((IFormatProvider) CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum"), new object[1]
{
(object) "length"
}));
}
else
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex"));
if (value == null)
value = string.Empty;
if (startIndex > value.Length - length)
throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_IndexLength"));
this.m_MaxCapacity = int.MaxValue;
if (capacity == 0)
capacity = 16;
while (capacity < length)
{
capacity *= 2;
if (capacity < 0)
{
capacity = length;
break;
}
}
this.m_StringValue = string.GetStringForStringBuilder(value, startIndex, length, capacity);
}
}
public StringBuilder(int capacity, int maxCapacity)
{
if (capacity > maxCapacity)
throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_Capacity"));
if (maxCapacity < 1)
throw new ArgumentOutOfRangeException("maxCapacity", Environment.GetResourceString("ArgumentOutOfRange_SmallMaxCapacity"));
if (capacity < 0)
{
throw new ArgumentOutOfRangeException("capacity", string.Format((IFormatProvider) CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), new object[1]
{
(object) "capacity"
}));
}
else
{
if (capacity == 0)
capacity = Math.Min(16, maxCapacity);
this.m_StringValue = string.GetStringForStringBuilder(string.Empty, capacity);
this.m_MaxCapacity = maxCapacity;
}
}
private string GetThreadSafeString(out IntPtr tid)
{
string str = this.m_StringValue;
tid = Thread.InternalGetCurrentThread();
if (this.m_currentThread == tid)
return str;
else
return string.GetStringForStringBuilder(str, str.Capacity);
}
public int EnsureCapacity(int capacity)
{
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedPosCapacity"));
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
if (!this.NeedsAllocation(threadSafeString, capacity))
return threadSafeString.Capacity;
string newString = this.GetNewString(threadSafeString, capacity);
this.ReplaceString(tid, newString);
return newString.Capacity;
}
public override string ToString()
{
string str = this.m_StringValue;
if (this.m_currentThread != Thread.InternalGetCurrentThread() || 2 * str.Length < str.ArrayLength)
return string.InternalCopy(str);
str.ClearPostNullChar();
this.m_currentThread = IntPtr.Zero;
return str;
}
public string ToString(int startIndex, int length)
{
return this.m_StringValue.InternalSubStringWithChecks(startIndex, length, true);
}
public StringBuilder Append(char value, int repeatCount)
{
if (repeatCount == 0)
return this;
if (repeatCount < 0)
throw new ArgumentOutOfRangeException("repeatCount", Environment.GetResourceString("ArgumentOutOfRange_NegativeCount"));
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
int requiredLength = length + repeatCount;
if (requiredLength < 0)
throw new OutOfMemoryException();
if (!this.NeedsAllocation(threadSafeString, requiredLength))
{
threadSafeString.AppendInPlace(value, repeatCount, length);
this.ReplaceString(tid, threadSafeString);
return this;
}
else
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.AppendInPlace(value, repeatCount, length);
this.ReplaceString(tid, newString);
return this;
}
}
public StringBuilder Append(char[] value, int startIndex, int charCount)
{
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
else
throw new ArgumentNullException("value");
}
else
{
if (charCount == 0)
return this;
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
if (charCount < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
if (charCount > value.Length - startIndex)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Index"));
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
int requiredLength = length + charCount;
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.AppendInPlace(value, startIndex, charCount, length);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.AppendInPlace(value, startIndex, charCount, length);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
}
public StringBuilder Append(string value)
{
if (value == null)
return this;
string currentString = this.m_StringValue;
IntPtr currentThread = Thread.InternalGetCurrentThread();
if (this.m_currentThread != currentThread)
currentString = string.GetStringForStringBuilder(currentString, currentString.Capacity);
int length = currentString.Length;
int requiredLength = length + value.Length;
if (this.NeedsAllocation(currentString, requiredLength))
{
string newString = this.GetNewString(currentString, requiredLength);
newString.AppendInPlace(value, length);
this.ReplaceString(currentThread, newString);
}
else
{
currentString.AppendInPlace(value, length);
this.ReplaceString(currentThread, currentString);
}
return this;
}
internal unsafe StringBuilder Append(char* value, int count)
{
if ((IntPtr) value == IntPtr.Zero)
return this;
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
int requiredLength = length + count;
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.AppendInPlace(value, count, length);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.AppendInPlace(value, count, length);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
private bool NeedsAllocation(string currentString, int requiredLength)
{
return currentString.ArrayLength <= requiredLength;
}
private string GetNewString(string currentString, int requiredLength)
{
int num = this.m_MaxCapacity;
if (requiredLength < 0)
throw new OutOfMemoryException();
if (requiredLength > num)
throw new ArgumentOutOfRangeException("requiredLength", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity"));
int capacity = currentString.Capacity * 2;
if (capacity < requiredLength)
capacity = requiredLength;
if (capacity > num)
capacity = num;
if (capacity <= 0)
throw new ArgumentOutOfRangeException("newCapacity", Environment.GetResourceString("ArgumentOutOfRange_NegativeCapacity"));
else
return string.GetStringForStringBuilder(currentString, capacity);
}
private void ReplaceString(IntPtr tid, string value)
{
this.m_currentThread = tid;
this.m_StringValue = value;
}
public StringBuilder Append(string value, int startIndex, int count)
{
if (value == null)
{
if (startIndex == 0 && count == 0)
return this;
else
throw new ArgumentNullException("value");
}
else if (count <= 0)
{
if (count == 0)
return this;
else
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
}
else
{
if (startIndex < 0 || startIndex > value.Length - count)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
int requiredLength = length + count;
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.AppendInPlace(value, startIndex, count, length);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.AppendInPlace(value, startIndex, count, length);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
}
[ComVisible(false)]
public StringBuilder AppendLine()
{
return this.Append(Environment.NewLine);
}
[ComVisible(false)]
public StringBuilder AppendLine(string value)
{
this.Append(value);
return this.Append(Environment.NewLine);
}
[ComVisible(false)]
public unsafe void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
{
if (destination == null)
throw new ArgumentNullException("destination");
if (count < 0)
throw new ArgumentOutOfRangeException(Environment.GetResourceString("Arg_NegativeArgCount"), "count");
if (destinationIndex < 0)
{
throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", new object[1]
{
(object) "destinationIndex"
}), "destinationIndex");
}
else
{
if (destinationIndex > destination.Length - count)
throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_OffsetOut"));
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
if (sourceIndex < 0 || sourceIndex > length)
throw new ArgumentOutOfRangeException(Environment.GetResourceString("ArgumentOutOfRange_Index"));
if (sourceIndex > length - count)
throw new ArgumentException(Environment.GetResourceString("Arg_LongerThanSrcString"));
if (count == 0)
return;
fixed (char* chPtr1 = &destination[destinationIndex])
fixed (char* chPtr2 = threadSafeString)
Buffer.memcpyimpl((byte*) ((IntPtr) chPtr2 + (IntPtr) sourceIndex * 2), (byte*) chPtr1, count * 2);
}
}
public StringBuilder Insert(int index, string value, int count)
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
if (index < 0 || index > length)
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
if (count < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (value != null && value.Length != 0)
{
if (count != 0)
{
int requiredLength;
try
{
requiredLength = checked (length + value.Length * count);
}
catch (OverflowException ex)
{
throw new OutOfMemoryException();
}
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.InsertInPlace(index, value, count, length, requiredLength);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.InsertInPlace(index, value, count, length, requiredLength);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
}
return this;
}
public StringBuilder Remove(int startIndex, int length)
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length1 = threadSafeString.Length;
if (length < 0)
throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NegativeLength"));
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex"));
if (length > length1 - startIndex)
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
threadSafeString.RemoveInPlace(startIndex, length, length1);
this.ReplaceString(tid, threadSafeString);
return this;
}
public StringBuilder Append(bool value)
{
return this.Append(value.ToString());
}
[CLSCompliant(false)]
public StringBuilder Append(sbyte value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(byte value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(char value)
{
string currentString = this.m_StringValue;
IntPtr currentThread = Thread.InternalGetCurrentThread();
if (this.m_currentThread != currentThread)
currentString = string.GetStringForStringBuilder(currentString, currentString.Capacity);
int length = currentString.Length;
if (!this.NeedsAllocation(currentString, length + 1))
{
currentString.AppendInPlace(value, length);
this.ReplaceString(currentThread, currentString);
return this;
}
else
{
string newString = this.GetNewString(currentString, length + 1);
newString.AppendInPlace(value, length);
this.ReplaceString(currentThread, newString);
return this;
}
}
public StringBuilder Append(short value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(int value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(long value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(float value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(double value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(Decimal value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
[CLSCompliant(false)]
public StringBuilder Append(ushort value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
[CLSCompliant(false)]
public StringBuilder Append(uint value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
[CLSCompliant(false)]
public StringBuilder Append(ulong value)
{
return this.Append(value.ToString((IFormatProvider) CultureInfo.CurrentCulture));
}
public StringBuilder Append(object value)
{
if (value == null)
return this;
else
return this.Append(value.ToString());
}
public StringBuilder Append(char[] value)
{
if (value == null)
return this;
int length1 = value.Length;
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length2 = threadSafeString.Length;
int requiredLength = length2 + value.Length;
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.AppendInPlace(value, 0, length1, length2);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.AppendInPlace(value, 0, length1, length2);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
public StringBuilder Insert(int index, string value)
{
if (value == null)
return this.Insert(index, value, 0);
else
return this.Insert(index, value, 1);
}
public StringBuilder Insert(int index, bool value)
{
return this.Insert(index, value.ToString(), 1);
}
[CLSCompliant(false)]
public StringBuilder Insert(int index, sbyte value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, byte value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, short value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, char value)
{
return this.Insert(index, char.ToString(value), 1);
}
public StringBuilder Insert(int index, char[] value)
{
if (value == null)
return this.Insert(index, value, 0, 0);
else
return this.Insert(index, value, 0, value.Length);
}
public StringBuilder Insert(int index, char[] value, int startIndex, int charCount)
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
if (index < 0 || index > length)
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
if (value == null)
{
if (startIndex == 0 && charCount == 0)
return this;
else
throw new ArgumentNullException(Environment.GetResourceString("ArgumentNull_String"));
}
else
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_StartIndex"));
if (charCount < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GenericPositive"));
if (startIndex > value.Length - charCount)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
if (charCount == 0)
return this;
int requiredLength = length + charCount;
if (this.NeedsAllocation(threadSafeString, requiredLength))
{
string newString = this.GetNewString(threadSafeString, requiredLength);
newString.InsertInPlace(index, value, startIndex, charCount, length, requiredLength);
this.ReplaceString(tid, newString);
}
else
{
threadSafeString.InsertInPlace(index, value, startIndex, charCount, length, requiredLength);
this.ReplaceString(tid, threadSafeString);
}
return this;
}
}
public StringBuilder Insert(int index, int value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, long value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, float value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, double value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, Decimal value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
[CLSCompliant(false)]
public StringBuilder Insert(int index, ushort value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
[CLSCompliant(false)]
public StringBuilder Insert(int index, uint value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
[CLSCompliant(false)]
public StringBuilder Insert(int index, ulong value)
{
return this.Insert(index, value.ToString((IFormatProvider) CultureInfo.CurrentCulture), 1);
}
public StringBuilder Insert(int index, object value)
{
if (value == null)
return this;
else
return this.Insert(index, value.ToString(), 1);
}
public StringBuilder AppendFormat(string format, object arg0)
{
return this.AppendFormat((IFormatProvider) null, format, new object[1]
{
arg0
});
}
public StringBuilder AppendFormat(string format, object arg0, object arg1)
{
return this.AppendFormat((IFormatProvider) null, format, new object[2]
{
arg0,
arg1
});
}
public StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2)
{
return this.AppendFormat((IFormatProvider) null, format, arg0, arg1, arg2);
}
public StringBuilder AppendFormat(string format, params object[] args)
{
return this.AppendFormat((IFormatProvider) null, format, args);
}
private static void FormatError()
{
throw new FormatException(Environment.GetResourceString("Format_InvalidString"));
}
public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args)
{
if (format == null || args == null)
throw new ArgumentNullException(format == null ? "format" : "args");
char[] chArray = format.ToCharArray(0, format.Length);
int index1 = 0;
int length = chArray.Length;
char ch = char.MinValue;
ICustomFormatter customFormatter = (ICustomFormatter) null;
if (provider != null)
customFormatter = (ICustomFormatter) provider.GetFormat(typeof (ICustomFormatter));
while (true)
{
bool flag;
int repeatCount;
do
{
int startIndex1 = index1;
int num1 = index1;
while (index1 < length)
{
ch = chArray[index1];
++index1;
if ((int) ch == 125)
{
if (index1 < length && (int) chArray[index1] == 125)
++index1;
else
StringBuilder.FormatError();
}
if ((int) ch == 123)
{
if (index1 < length && (int) chArray[index1] == 123)
{
++index1;
}
else
{
--index1;
break;
}
}
chArray[num1++] = ch;
}
if (num1 > startIndex1)
this.Append(chArray, startIndex1, num1 - startIndex1);
if (index1 != length)
{
int index2 = index1 + 1;
if (index2 == length || (int) (ch = chArray[index2]) < 48 || (int) ch > 57)
StringBuilder.FormatError();
int index3 = 0;
do
{
index3 = index3 * 10 + (int) ch - 48;
++index2;
if (index2 == length)
StringBuilder.FormatError();
ch = chArray[index2];
}
while ((int) ch >= 48 && (int) ch <= 57 && index3 < 1000000);
if (index3 >= args.Length)
throw new FormatException(Environment.GetResourceString("Format_IndexOutOfRange"));
while (index2 < length && (int) (ch = chArray[index2]) == 32)
++index2;
flag = false;
int num2 = 0;
if ((int) ch == 44)
{
++index2;
while (index2 < length && (int) chArray[index2] == 32)
++index2;
if (index2 == length)
StringBuilder.FormatError();
ch = chArray[index2];
if ((int) ch == 45)
{
flag = true;
++index2;
if (index2 == length)
StringBuilder.FormatError();
ch = chArray[index2];
}
if ((int) ch < 48 || (int) ch > 57)
StringBuilder.FormatError();
do
{
num2 = num2 * 10 + (int) ch - 48;
++index2;
if (index2 == length)
StringBuilder.FormatError();
ch = chArray[index2];
}
while ((int) ch >= 48 && (int) ch <= 57 && num2 < 1000000);
}
while (index2 < length && (int) (ch = chArray[index2]) == 32)
++index2;
object obj = args[index3];
string format1 = (string) null;
if ((int) ch == 58)
{
int index4 = index2 + 1;
int startIndex2 = index4;
int num3 = index4;
while (true)
{
if (index4 == length)
StringBuilder.FormatError();
ch = chArray[index4];
++index4;
if ((int) ch == 123)
{
if (index4 < length && (int) chArray[index4] == 123)
++index4;
else
StringBuilder.FormatError();
}
else if ((int) ch == 125)
{
if (index4 < length && (int) chArray[index4] == 125)
++index4;
else
break;
}
chArray[num3++] = ch;
}
index2 = index4 - 1;
if (num3 > startIndex2)
format1 = new string(chArray, startIndex2, num3 - startIndex2);
}
if ((int) ch != 125)
StringBuilder.FormatError();
index1 = index2 + 1;
string str = (string) null;
if (customFormatter != null)
str = customFormatter.Format(format1, obj, provider);
if (str == null)
{
if (obj is IFormattable)
str = ((IFormattable) obj).ToString(format1, provider);
else if (obj != null)
str = obj.ToString();
}
if (str == null)
str = string.Empty;
repeatCount = num2 - str.Length;
if (!flag && repeatCount > 0)
this.Append(' ', repeatCount);
this.Append(str);
}
else
goto label_74;
}
while (!flag || repeatCount <= 0);
this.Append(' ', repeatCount);
}
label_74:
return this;
}
public StringBuilder Replace(string oldValue, string newValue)
{
return this.Replace(oldValue, newValue, 0, this.Length);
}
[MethodImpl(MethodImplOptions.InternalCall)]
public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count);
public bool Equals(StringBuilder sb)
{
if (sb == null || this.Capacity != sb.Capacity || this.MaxCapacity != sb.MaxCapacity)
return false;
else
return this.m_StringValue.Equals(sb.m_StringValue);
}
public StringBuilder Replace(char oldChar, char newChar)
{
return this.Replace(oldChar, newChar, 0, this.Length);
}
public StringBuilder Replace(char oldChar, char newChar, int startIndex, int count)
{
IntPtr tid;
string threadSafeString = this.GetThreadSafeString(out tid);
int length = threadSafeString.Length;
if ((uint) startIndex > (uint) length)
throw new ArgumentOutOfRangeException("startIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
if (count < 0 || startIndex > length - count)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_Index"));
threadSafeString.ReplaceCharInPlace(oldChar, newChar, startIndex, count, length);
this.ReplaceString(tid, threadSafeString);
return this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment