[Mono-list] Patch for String.Replace

Jackson Harper jackson@latitudegeo.com
18 Aug 2003 08:30:41 -0700


--=-7ITRduklSmemwJELiz7t
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello,

	Attached is a patch that changes the behavoir of String.Replace. If the
replace method does not alter the string at all it returns a reference
to the unaltered string. I have been told on #mono that this is what is
happening on MS. Would be great if some more people can confirm this.

	I've also attached a test case that I will integrate into the unit
tests if someone can confirm that this is the same behavoir as MS.


Jackson



--=-7ITRduklSmemwJELiz7t
Content-Disposition: attachment; filename=string-icalls.c.patch
Content-Type: text/x-patch; name=string-icalls.c.patch; charset=
Content-Transfer-Encoding: 7bit

Index: string-icalls.c
===================================================================
RCS file: /cvs/public/mono/mono/metadata/string-icalls.c,v
retrieving revision 1.22
diff -u -r1.22 string-icalls.c
--- string-icalls.c	11 May 2003 23:52:40 -0000	1.22
+++ string-icalls.c	18 Aug 2003 05:05:37 -0000
@@ -348,23 +348,30 @@
  	} else
 		newsize = srclen;
 
-	ret = mono_string_new_size( mono_domain_get (), newsize);
-	dest = mono_string_chars(ret);
-
+        ret = NULL;
 	i = 0;
 	while (i < srclen) {
 		if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2))) {
+                        if (ret == NULL) {
+                                ret = mono_string_new_size( mono_domain_get (), newsize);
+                                dest = mono_string_chars(ret);
+                                memcpy (dest, src, i * sizeof(gunichar2));
+                        }
 			if (newstrlen > 0) {
 				memcpy(dest + destpos, newstr, newstrlen * sizeof(gunichar2));
 				destpos += newstrlen;
 			}
 			i += oldstrlen;
-		} else {
+                        continue;
+		} else if (ret != NULL) {
 			dest[destpos] = src[i];
-			destpos++;
-			i++;
 		}
+                destpos++;
+                i++;
 	}
+
+        if (ret == NULL)
+                return me;
 
 	return ret;
 }

--=-7ITRduklSmemwJELiz7t
Content-Disposition: attachment; filename=string-replace.cs
Content-Type: text/plain; name=string-replace.cs; charset=
Content-Transfer-Encoding: 7bit


using System;

namespace T {

        public class Driver
        {
                public static int Main ()
                {
                        if (TestA () && TestB ())
                                return 0;
                        return -1;
                }

                private static bool TestA ()
                {
                        string s = "KKKKKKKKKKKKKKKK";
                        string ss = s.Replace ("n", "b");
                        return String.ReferenceEquals (s, ss);
                }

                private static bool TestB ()
                {
                        string s = "KKKKKKKKKKKKKKKK";
                        string ss = s.Replace ("K", "A");
                        return !(String.ReferenceEquals (s, ss));
                }
        }

}


--=-7ITRduklSmemwJELiz7t--