Merge remote-tracking branch 'origin/7.1.1' into 7.1.2

This commit is contained in:
Shannon
2014-04-30 11:16:33 +10:00
2 changed files with 20 additions and 7 deletions

View File

@@ -1191,17 +1191,18 @@ namespace Umbraco.Core
/// <returns>Updated string</returns>
public static string Replace(this string source, string oldString, string newString, StringComparison stringComparison)
{
var index = source.IndexOf(oldString, stringComparison);
// This initialisation ensures the first check starts at index zero of the source. On successive checks for
// a match, the source is skipped to immediately after the last replaced occurrence for efficiency
// and to avoid infinite loops when oldString and newString compare equal.
int index = -1 * newString.Length;
// Determine if we found a match
var matchFound = index >= 0;
if (matchFound)
// Determine if there are any matches left in source, starting from just after the result of replacing the last match.
while((index = source.IndexOf(oldString, index + newString.Length, stringComparison)) >= 0)
{
// Remove the old text
// Remove the old text.
source = source.Remove(index, oldString.Length);
// Add the replacemenet text
// Add the replacemenet text.
source = source.Insert(index, newString);
}