summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJan Kotas <jkotas@microsoft.com>2016-02-21 06:22:06 -0800
committerJan Kotas <jkotas@microsoft.com>2016-02-21 06:22:06 -0800
commit53148ebbc2b62f7a9b0d2369003ef7087a4c3e3c (patch)
tree8e4b2b7fb4a90f6c2cb4ed5481f5cbc581154fee /src
parentdec83bd64d40fcff76e14f5700b8fb69413e4057 (diff)
parent74709851898330c6ac3a20eb04d085e9b0e2dcf0 (diff)
downloadcoreclr-53148ebbc2b62f7a9b0d2369003ef7087a4c3e3c.tar.gz
coreclr-53148ebbc2b62f7a9b0d2369003ef7087a4c3e3c.tar.bz2
coreclr-53148ebbc2b62f7a9b0d2369003ef7087a4c3e3c.zip
Merge pull request #3278 from jamesqo/patch-7
Short-circuit for empty strings in string.Insert and Remove
Diffstat (limited to 'src')
-rw-r--r--src/mscorlib/src/System/String.cs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs
index f544fc9273..0877954142 100644
--- a/src/mscorlib/src/System/String.cs
+++ b/src/mscorlib/src/System/String.cs
@@ -2851,12 +2851,17 @@ namespace System {
Contract.Ensures(Contract.Result<String>() != null);
Contract.Ensures(Contract.Result<String>().Length == this.Length + value.Length);
Contract.EndContractBlock();
+
int oldLength = Length;
int insertLength = value.Length;
+
+ if (oldLength == 0)
+ return value;
+ if (insertLength == 0)
+ return this;
+
// In case this computation overflows, newLength will be negative and FastAllocateString throws OutOfMemoryException
int newLength = oldLength + insertLength;
- if (newLength == 0)
- return String.Empty;
String result = FastAllocateString(newLength);
unsafe
{
@@ -2937,9 +2942,13 @@ namespace System {
Contract.Ensures(Contract.Result<String>() != null);
Contract.Ensures(Contract.Result<String>().Length == this.Length - count);
Contract.EndContractBlock();
+
+ if (count == 0)
+ return this;
int newLength = Length - count;
if (newLength == 0)
return String.Empty;
+
String result = FastAllocateString(newLength);
unsafe
{