Skip to content

Commit

Permalink
remove StringReplace (#4569)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 24, 2024
1 parent 1f999fb commit ddeb315
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 63 deletions.
47 changes: 0 additions & 47 deletions src/client/Microsoft.Identity.Client/Utils/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,6 @@ public static string NullIfWhiteSpace(this string s)
{
return string.IsNullOrWhiteSpace(s) ? null : s;
}


/// <summary>
/// Culture aware String.Replace
/// </summary>
public static string Replace(this string src, string oldValue, string newValue, StringComparison comparison)
{
if (string.IsNullOrWhiteSpace(src))
{
return src;
}

if (string.IsNullOrWhiteSpace(oldValue))
{
throw new ArgumentException("oldValue cannot be empty");
}

// skip the loop entirely if oldValue and newValue are the same
if (string.Compare(oldValue, newValue, comparison) == 0)
{
return src;
}

if (oldValue.Length > src.Length)
{
return src;
}

var sb = new StringBuilder();

int previousIndex = 0;
int index = src.IndexOf(oldValue, comparison);

while (index != -1)
{
sb.Append(src.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;

previousIndex = index;
index = src.IndexOf(oldValue, index, comparison);
}

sb.Append(src.Substring(previousIndex));

return sb.ToString();
}

#if NETSTANDARD2_0 || NETFRAMEWORK || WINDOWS_APP
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@ namespace Microsoft.Identity.Test.Unit.CoreTests
[TestClass]
public class StringExtensionTests
{
[TestMethod]
public void StringReplace()
{
Assert.AreEqual("hi common !", "hi {tenant} !".Replace("{tenant}", "common", StringComparison.OrdinalIgnoreCase));
Assert.AreEqual("hi commoncommon !", "hi {tenant}{tenant} !".Replace("{tenant}", "common", StringComparison.OrdinalIgnoreCase));
Assert.AreEqual("hi common--common !", "hi {tenant}--{tenant} !".Replace("{tenant}", "common", StringComparison.OrdinalIgnoreCase));
Assert.AreEqual("hi common !", "hi {tenaNt} !".Replace("{tEnant}", "common", StringComparison.OrdinalIgnoreCase));

Assert.AreEqual("hi common !", "hi {tenant_id} !".Replace("{tenant_ID}", "common", StringComparison.OrdinalIgnoreCase));
Assert.AreEqual("hi {tenant_id} !", "hi {tenant_id} !".Replace("nothing", "common", StringComparison.OrdinalIgnoreCase));

Assert.AreEqual("", "".Replace("nothing", "common", StringComparison.OrdinalIgnoreCase));
AssertException.Throws<ArgumentException>(() =>
"hi {tenant} !".Replace("", "common", StringComparison.OrdinalIgnoreCase));
}

[TestMethod]
public void NullIfEmpty()
{
Expand Down

0 comments on commit ddeb315

Please sign in to comment.