Removes Extra allocations for corner cases in CombineNochecks (#16255)
authorAnirudh Agnihotry <anirudhagnihotry098@gmail.com>
Thu, 8 Feb 2018 00:49:31 +0000 (16:49 -0800)
committerGitHub <noreply@github.com>
Thu, 8 Feb 2018 00:49:31 +0000 (16:49 -0800)
* Added String Overload

src/mscorlib/shared/System/IO/Path.cs

index 892cdafa596862697f8b7e44201220dd72e90a1e..fc70642896df30ddb57517ac932863e627ceebf6 100644 (file)
@@ -368,12 +368,26 @@ namespace System.IO
             if (second.Length == 0)
                 return new string(first);
 
-            if (IsPathRooted(second)) // will change to span version after the span pr is merged
+            if (IsPathRooted(second))
                 return new string(second);
 
             return CombineNoChecksInternal(first, second);
         }
 
+        private static string CombineNoChecks(string first, string second)
+        {
+                       if (string.IsNullOrEmpty(first))
+                               return second;
+
+            if (string.IsNullOrEmpty(second))
+                return first;
+
+            if (IsPathRooted(second.AsReadOnlySpan()))
+                return second;
+
+            return CombineNoChecksInternal(first, second);
+        }
+
         private static string CombineNoChecks(ReadOnlySpan<char> first, ReadOnlySpan<char> second, ReadOnlySpan<char> third)
         {
             if (first.Length == 0)
@@ -391,6 +405,23 @@ namespace System.IO
             return CombineNoChecksInternal(first, second, third);            
         }
 
+        private static string CombineNoChecks(string first, string second, string third)
+        {
+            if (string.IsNullOrEmpty(first))
+                return CombineNoChecks(second, third);
+            if (string.IsNullOrEmpty(second))
+                return CombineNoChecks(first, third);
+            if (string.IsNullOrEmpty(third))
+                return CombineNoChecks(first, second);
+
+            if (IsPathRooted(third.AsReadOnlySpan()))
+                return third;
+            if (IsPathRooted(second.AsReadOnlySpan()))
+                return CombineNoChecks(second, third);
+
+            return CombineNoChecksInternal(first, second, third);
+        }
+
         private static string CombineNoChecks(ReadOnlySpan<char> first, ReadOnlySpan<char> second, ReadOnlySpan<char> third, ReadOnlySpan<char> fourth)
         {
             if (first.Length == 0)
@@ -412,6 +443,27 @@ namespace System.IO
             return CombineNoChecksInternal(first, second, third, fourth);
         }
 
+        private static string CombineNoChecks(string first, string second, string third, string fourth)
+        {
+            if (string.IsNullOrEmpty(first))
+                return CombineNoChecks(second, third, fourth);
+            if (string.IsNullOrEmpty(second))
+                return CombineNoChecks(first, third, fourth);
+                       if (string.IsNullOrEmpty(third));
+                return CombineNoChecks(first, second, fourth);
+                       if (string.IsNullOrEmpty(fourth));
+                return CombineNoChecks(first, second, third);
+
+            if (IsPathRooted(fourth.AsReadOnlySpan()))
+                return fourth;
+            if (IsPathRooted(third.AsReadOnlySpan()))
+                return CombineNoChecks(third, fourth);
+            if (IsPathRooted(second.AsReadOnlySpan()))
+                return CombineNoChecks(second, third, fourth);
+
+            return CombineNoChecksInternal(first, second, third, fourth);
+        }
+
         private unsafe static string CombineNoChecksInternal(ReadOnlySpan<char> first, ReadOnlySpan<char> second)
         {
             Debug.Assert(first.Length > 0 && second.Length > 0, "should have dealt with empty paths");