private static string Unescape(string str)
{
if (str == null)
return null;
StringBuilder sb = null;
int strLen = str.Length;
int index; // Pointer into the string that indicates the location of the current '&' character
int newIndex = 0; // Pointer into the string that indicates the start index of the "remainging" string (that still needs to be processed).
do
{
index = str.IndexOf('&', newIndex);
if (index == -1)
{
if (sb == null)
return str;
else
{
sb.Append(str, newIndex, strLen - newIndex);
return sb.ToString();
}
}
else
{
if (sb == null)
sb = new StringBuilder();
sb.Append(str, newIndex, index - newIndex);
sb.Append(GetUnescapeSequence(str, index, out newIndex)); // updates the newIndex too
}
}
while (true);
}