Fix inefficient string manipulations in RegularExpressions tests (dotnet/corefx#42241)
authorJan Kotas <jkotas@microsoft.com>
Thu, 31 Oct 2019 01:57:48 +0000 (18:57 -0700)
committerStephen Toub <stoub@microsoft.com>
Thu, 31 Oct 2019 01:57:48 +0000 (21:57 -0400)
* Fix inefficient string manipulations in RegularExpressions tests

* CR feedback

* Consistent style

Commit migrated from https://github.com/dotnet/corefx/commit/54efc1141aba6c1572f3128372ee8835d063c850

src/libraries/System.Text.RegularExpressions/tests/Regex.Replace.Tests.cs

index a2befc0..cef5a82 100644 (file)
@@ -24,20 +24,11 @@ namespace System.Text.RegularExpressions.Tests
             yield return new object[] { "([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z]([a-z])))))))))))))))", "abcdefghiklmnop", "$3", RegexOptions.None, 15, 0, "cdefghiklmnop" };
 
             // Stress
-            string pattern = string.Empty;
-            for (int i = 0; i < 1000; i++)
-                pattern += "([a-z]";
-            for (int i = 0; i < 1000; i++)
-                pattern += ")";
-            string input = string.Empty;
-            for (int i = 0; i < 200; i++)
-                input += "abcde";
-            yield return new object[] { pattern, input, "$1000", RegexOptions.None, input.Length, 0, "e" };
+            string pattern = string.Concat(Enumerable.Repeat("([a-z]", 1000).Concat(Enumerable.Repeat(")", 1000)));
+            string input = string.Concat(Enumerable.Repeat("abcde", 200));
 
-            string expected = string.Empty;
-            for (int i = 0; i < 200; i++)
-                expected += "abcde";
-            yield return new object[] { pattern, input, "$1", RegexOptions.None, input.Length, 0, expected };
+            yield return new object[] { pattern, input, "$1000", RegexOptions.None, input.Length, 0, "e" };
+            yield return new object[] { pattern, input, "$1", RegexOptions.None, input.Length, 0, input };
 
             // Undefined group
             yield return new object[] { "([a_z])(.+)", "abc", "$3", RegexOptions.None, 3, 0, "$3" };