mono/mono-2.0-StringReplace.patch
2008-10-02 23:53:05 +00:00

45 lines
1.9 KiB
Diff

Index: corlib/System/String.cs
===================================================================
--- mono-2.0/mcs/class/corlib/System/String.cs (revision 112532)
+++ mono-2.0/mcs/class/corlib/System/String.cs (working copy)
@@ -1689,6 +1689,8 @@
}
i = found + oldValue.length;
}
+ if (count == 0)
+ return this;
int nlen = this.length + ((newValue.length - oldValue.length) * count);
String tmp = InternalAllocateStr (nlen);
Index: corlib/System.Text/StringBuilder.cs
===================================================================
--- mono-2.0/mcs/class/corlib/System.Text/StringBuilder.cs (revision 112532)
+++ mono-2.0/mcs/class/corlib/System.Text/StringBuilder.cs (working copy)
@@ -309,15 +309,22 @@
if (oldValue.Length == 0)
throw new ArgumentException ("The old value cannot be zero length.");
- // TODO: OPTIMIZE!
- string replace = _str.Substring(startIndex, count).Replace(oldValue, newValue);
+ string substr = _str.Substring(startIndex, count);
+ string replace = substr.Replace(oldValue, newValue);
+ // return early if no oldValue was found
+ if ((object) replace == (object) substr)
+ return this;
InternalEnsureCapacity (replace.Length + (_length - count));
- string end = _str.Substring (startIndex + count, _length - startIndex - count );
+ // shift end part
+ if (replace.Length < count)
+ String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
+ else if (replace.Length > count)
+ String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
+ // copy middle part back into _str
String.CharCopy (_str, startIndex, replace, 0, replace.Length);
- String.CharCopy (_str, startIndex + replace.Length, end, 0, end.Length);
_length = replace.Length + (_length - count);