[Mono-list] Patch for String.Replace
Jackson Harper
jackson@latitudegeo.com
19 Aug 2003 19:55:15 -0700
--=-3htak2bg6xRTourU+7XP
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Mon, 2003-08-18 at 04:36, Jonathan Pryor wrote:
> Perhaps I'm missing something, but your patch could be much simpler.
> All you need to do is, before the "ret = mono_string_new_size(...)" line
> (line 351), have this:
>
> if (occur == 0)
> return me;
The occurr variable is only modified if the string being replaced is
not the same size as the replacement string.
I can use your check if the strings are a different size though.
Attached is a new patch.
Jackson
>
> Since "occur" keeps track of how many times "oldValue" occurs within
> "me", if occur is 0, you don't need to do any processing at all. This
> would require no changes to the loop as well.
>
> - Jon
>
> On Mon, 2003-08-18 at 11:30, Jackson Harper wrote:
> > 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
> >
> >
>
--=-3htak2bg6xRTourU+7XP
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 20 Aug 2003 03:05:33 -0000
@@ -343,28 +343,36 @@
for (i = 0; i <= srclen - oldstrlen; i++)
if (0 == memcmp(src + i, oldstr, oldstrlen * sizeof(gunichar2)))
occurr++;
-
+ if (occurr == 0)
+ return me;
newsize = srclen + ((newstrlen - oldstrlen) * occurr);
} 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++;
}
- }
+
+ if (ret == NULL)
+ return me;
return ret;
}
--=-3htak2bg6xRTourU+7XP--