2008-11-28 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / string-icalls.c
index d86231f0dd8ec88cfe91617fb2775557a6b2413d..9d7a1c503a035a113fdb6f4fffc8474873dcf5e2 100644 (file)
 #include <stdio.h>
 #include <signal.h>
 #include <string.h>
+#include "mono/utils/mono-membar.h"
 #include <mono/metadata/string-icalls.h>
+#include <mono/metadata/class-internals.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/tabledefs.h>
 #include <mono/metadata/loader.h>
 #include <mono/metadata/object.h>
-#include <mono/metadata/unicode.h>
 #include <mono/metadata/exception.h>
+#include <mono/metadata/debug-helpers.h>
 
 /* Internal helper methods */
 
 static gboolean
 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
 
-static gint32
-string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode);
-
-MonoString *
-ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
-{
-       gint32 i, length;
-       MonoDomain *domain;
-
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_domain_get ();
-
-       if (value == NULL)
-               length = 0;
-       else {
-               for (i = 0; *(value + i) != '\0'; i++);
-               length = i;
-       }
-
-       return mono_string_new_utf16 (domain, value, length);
-}
-
-MonoString *
-ves_icall_System_String_ctor_char_int (gpointer dummy, gunichar2 value, gint32 count)
-{
-       MonoDomain *domain;
-       MonoString *res;
-       gunichar2 *chars;
-       gint32 i;
-
-       MONO_ARCH_SAVE_REGS;
-
-       if (count < 0)
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("count"));
-
-       domain = mono_domain_get ();
-       res = mono_string_new_size (domain, count);
-
-       chars = mono_string_chars (res);
-       for (i = 0; i < count; i++)
-               chars [i] = value;
-       
-       return res;
-}
-
-MonoString *
-ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gint32 sindex, gint32 length)
-{
-       gunichar2 *begin;
-       MonoDomain * domain;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_domain_get ();
-
-       if ((value == NULL) && (length != 0))
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
-
-       if ((sindex < 0) || (length < 0))
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
-       
-       if (length == 0) {      /* fixme: return String.Empty here */
-               g_warning ("string doesn't yet support empy strings in char* constructor");
-               g_assert_not_reached ();
-       }
-       
-       begin = (gunichar2 *) (value + sindex);
-
-       return mono_string_new_utf16 (domain, begin, length);
-}
-
-MonoString *
-ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
-{
-       MonoDomain *domain;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_domain_get ();
-
-       if (NULL == value) {    /* fixme: return String.Empty here */
-               g_warning ("string doesn't yet support empy strings in char* constructor");
-               g_assert_not_reached ();
-       }
-
-       return mono_string_new (domain, (const char *) value);
-}
-
-MonoString *
-ves_icall_System_String_ctor_sbytep_int_int (gpointer dummy, gint8 *value, gint32 sindex, gint32 length)
-{
-       guchar *begin;
-       MonoDomain *domain;
-       MonoString *res;
-       gunichar2 *chars;
-       int i;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_domain_get ();
-
-       if ((value == NULL) && (length != 0))
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
-
-       if ((sindex < 0) || (length < 0))
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
-
-       begin = (guchar *) (value + sindex);
-       res = mono_string_new_size (domain, length);
-       chars = mono_string_chars (res);
-       for (i = 0; i < length; ++i)
-               chars [i] = begin [i];
-
-       return res;
-}
-
-MonoString *
-ves_icall_System_String_ctor_chara (gpointer dummy, MonoArray *value)
-{
-       MonoDomain *domain;
-
-       MONO_ARCH_SAVE_REGS;
-
-       domain = mono_domain_get ();
-
-       if (value == NULL)
-               return mono_string_new_utf16 (domain, NULL, 0);
-       else
-               return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, 0),  value->max_length);
-}
-
-MonoString *
-ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value, 
-                                        gint32 sindex, gint32 length)
-{
-       MonoDomain *domain;
-
-       MONO_ARCH_SAVE_REGS;
-
-       if (value == NULL)
-               mono_raise_exception (mono_get_exception_argument_null ("value"));
-       if (sindex < 0)
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("startIndex"));         
-       if (length < 0)
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("length"));
-       if (sindex + length > mono_array_length (value))
-               mono_raise_exception (mono_get_exception_argument_out_of_range ("Out of range"));
-
-       domain = mono_domain_get ();
-       
-       return mono_string_new_utf16 (domain, (gunichar2 *) mono_array_addr(value, gunichar2, sindex), length);
-}
-
-MonoString *
-ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex, 
-                                   gint32 length, MonoObject *enc)
+/* This function is redirected to String.CreateString ()
+   by mono_marshal_get_native_wrapper () */
+void
+ves_icall_System_String_ctor_RedirectToCreateString (void)
 {
-       MONO_ARCH_SAVE_REGS;
-
-       g_warning("string.ctor with encoding obj unimplemented");
        g_assert_not_reached ();
-       return NULL;
-}
-
-MonoBoolean 
-ves_icall_System_String_InternalEquals (MonoString *str1, MonoString *str2)
-{
-       gunichar2 *str1ptr;
-       gunichar2 *str2ptr;
-       gint32 str1len;
-
-       MONO_ARCH_SAVE_REGS;
-
-       /* Length checking is done in C# */
-       str1len = mono_string_length(str1);
-
-       str1ptr = mono_string_chars(str1);
-       str2ptr = mono_string_chars(str2);
-
-       return (0 == memcmp(str1ptr, str2ptr, str1len * sizeof(gunichar2)));
 }
 
 MonoString * 
 ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value, gint32 sindex, gint32 count)
 {
        MonoString * ret;
+       MonoString *current;
        gint32 length;
        gint32 pos;
        gint32 insertlen;
@@ -228,7 +56,10 @@ ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value,
 
        length = 0;
        for (pos = sindex; pos != sindex + count; pos++) {
-               length += mono_string_length(mono_array_get(value, MonoString *, pos));
+               current = mono_array_get (value, MonoString *, pos);
+               if (current != NULL)
+                       length += mono_string_length (current);
+
                if (pos < sindex + count - 1)
                        length += insertlen;
        }
@@ -238,11 +69,14 @@ ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value,
        destpos = 0;
 
        for (pos = sindex; pos != sindex + count; pos++) {
-               src = mono_string_chars(mono_array_get(value, MonoString *, pos));
-               srclen = mono_string_length(mono_array_get(value, MonoString *, pos));
+               current = mono_array_get (value, MonoString *, pos);
+               if (current != NULL) {
+                       src = mono_string_chars (current);
+                       srclen = mono_string_length (current);
 
-               memcpy(dest + destpos, src, srclen * sizeof(gunichar2));
-               destpos += srclen;
+                       memcpy (dest + destpos, src, srclen * sizeof(gunichar2));
+                       destpos += srclen;
+               }
 
                if (pos < sindex + count - 1) {
                        memcpy(dest + destpos, insert, insertlen * sizeof(gunichar2));
@@ -253,152 +87,6 @@ ves_icall_System_String_InternalJoin (MonoString *separator, MonoArray * value,
        return ret;
 }
 
-MonoString * 
-ves_icall_System_String_InternalInsert (MonoString *me, gint32 sindex, MonoString *value)
-{
-       MonoString * ret;
-       gunichar2 *src;
-       gunichar2 *insertsrc;
-       gunichar2 *dest;
-       gint32 srclen;
-       gint32 insertlen;
-
-       MONO_ARCH_SAVE_REGS;
-
-       src = mono_string_chars(me);
-       srclen = mono_string_length(me);
-
-       insertsrc = mono_string_chars(value);
-       insertlen = mono_string_length(value);
-
-       ret = mono_string_new_size( mono_domain_get (), srclen + insertlen);
-       dest = mono_string_chars(ret);
-
-       memcpy(dest, src, sindex * sizeof(gunichar2));
-       memcpy(dest + sindex, insertsrc, insertlen * sizeof(gunichar2));
-       memcpy(dest + sindex + insertlen, src + sindex, (srclen - sindex) * sizeof(gunichar2));
-
-       return ret;
-}
-
-MonoString * 
-ves_icall_System_String_InternalReplace_Char (MonoString *me, gunichar2 oldChar, gunichar2 newChar)
-{
-       MonoString *ret;
-       gunichar2 *src;
-       gunichar2 *dest;
-       gint32 i, srclen;
-
-       MONO_ARCH_SAVE_REGS;
-
-       src = mono_string_chars(me);
-       srclen = mono_string_length(me);
-
-       ret = mono_string_new_size( mono_domain_get (), srclen);
-       dest = mono_string_chars(ret);
-
-       for (i = 0; i != srclen; i++) {
-               if (src[i] == oldChar)
-                       dest[i] = newChar;
-               else
-                       dest[i] = src[i];
-       }
-
-       return ret;
-}
-
-MonoString * 
-ves_icall_System_String_InternalReplace_Str (MonoString *me, MonoString *oldValue, MonoString *newValue)
-{
-       MonoString *ret;
-       gunichar2 *src;
-       gunichar2 *dest;
-       gunichar2 *oldstr;
-       gunichar2 *newstr;
-       gint32 i, destpos;
-       gint32 occurr;
-       gint32 newsize;
-       gint32 oldstrlen;
-       gint32 newstrlen;
-       gint32 srclen;
-
-       MONO_ARCH_SAVE_REGS;
-
-       occurr = 0;
-       destpos = 0;
-
-       oldstr = mono_string_chars(oldValue);
-       oldstrlen = mono_string_length(oldValue);
-
-       if (NULL != newValue) {
-               newstr = mono_string_chars(newValue);
-               newstrlen = mono_string_length(newValue);
-       } else
-               newstrlen = 0;
-
-       src = mono_string_chars(me);
-       srclen = mono_string_length(me);
-
-       if (oldstrlen != newstrlen) {
-               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 = 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;
-                        continue;
-               } else if (ret != NULL) {
-                       dest[destpos] = src[i];
-               }
-                       destpos++;
-                       i++;
-               }
-        
-        if (ret == NULL)
-                return me;
-
-       return ret;
-}
-
-MonoString * 
-ves_icall_System_String_InternalRemove (MonoString *me, gint32 sindex, gint32 count)
-{
-       MonoString * ret;
-       gint32 srclen;
-       gunichar2 *dest;
-       gunichar2 *src;
-
-       MONO_ARCH_SAVE_REGS;
-
-       srclen = mono_string_length(me);
-       ret = mono_string_new_size( mono_domain_get (), srclen - count);
-
-       src = mono_string_chars(me);
-       dest = mono_string_chars(ret);
-
-       memcpy(dest, src, sindex * sizeof(gunichar2));
-       memcpy(dest + sindex, src + sindex + count, (srclen - count - sindex) * sizeof(gunichar2));
-
-       return ret;
-}
-
 void
 ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray *dest, gint32 dindex, gint32 count)
 {
@@ -410,78 +98,156 @@ ves_icall_System_String_InternalCopyTo (MonoString *me, gint32 sindex, MonoArray
        memcpy(destptr, src + sindex, sizeof(gunichar2) * count);
 }
 
+/* System.StringSplitOptions */
+typedef enum {
+       STRINGSPLITOPTIONS_NONE = 0,
+       STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES = 1
+} StringSplitOptions;
+
 MonoArray * 
-ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count)
+ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count, gint32 options)
 {
+       static MonoClass *String_array;
        MonoString * tmpstr;
        MonoArray * retarr;
        gunichar2 *src;
        gint32 arrsize, srcsize, splitsize;
        gint32 i, lastpos, arrpos;
        gint32 tmpstrsize;
+       gint32 remempty;
+       gint32 flag;
        gunichar2 *tmpstrptr;
 
-       gunichar2 cmpchar;
-
-       MONO_ARCH_SAVE_REGS;
+       remempty = options & STRINGSPLITOPTIONS_REMOVE_EMPTY_ENTRIES;
+       src = mono_string_chars (me);
+       srcsize = mono_string_length (me);
+       arrsize = mono_array_length (separator);
 
-       src = mono_string_chars(me);
-       srcsize = mono_string_length(me);
-       arrsize = mono_array_length(separator);
+       if (!String_array) {
+               MonoClass *klass = mono_array_class_get (mono_get_string_class (), 1);
+               mono_memory_barrier ();
+               String_array = klass;
+       }
 
-       cmpchar = mono_array_get(separator, gunichar2, 0);
+       splitsize = 1;
+       /* Count the number of elements we will return. Note that this operation
+        * guarantees that we will return exactly splitsize elements, and we will
+        * have enough data to fill each. This allows us to skip some checks later on.
+        */
+       if (remempty == 0) {
+               for (i = 0; i != srcsize && splitsize < count; i++) {
+                       if (string_icall_is_in_array (separator, arrsize, src [i]))
+                               splitsize++;
+               }
+       } else if (count > 1) {
+               /* Require pattern "Nondelim + Delim + Nondelim" to increment counter.
+                * Lastpos != 0 means first nondelim found.
+                * Flag = 0 means last char was delim.
+                * Efficient, though perhaps confusing.
+                */
+               lastpos = 0;
+               flag = 0;
+               for (i = 0; i != srcsize && splitsize < count; i++) {
+                       if (string_icall_is_in_array (separator, arrsize, src [i])) {
+                               flag = 0;
+                       } else if (flag == 0) {
+                               if (lastpos == 1)
+                                       splitsize++;
+                               flag = 1;
+                               lastpos = 1;
+                       }
+               }
 
-       splitsize = 0;
-       for (i = 0; i != srcsize && splitsize < count; i++) {
-               if (string_icall_is_in_array(separator, arrsize, src[i]))
-                       splitsize++;
+               /* Nothing but separators */
+               if (lastpos == 0) {
+                       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 0);
+                       return retarr;
+               }
        }
 
-       lastpos = 0;
-       arrpos = 0;
-
        /* if no split chars found return the string */
-       if (splitsize == 0) {
-               retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
-               tmpstr = mono_string_new_size( mono_domain_get (), srcsize);
-               tmpstrptr = mono_string_chars(tmpstr);
-
-               memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
-               mono_array_set(retarr, MonoString *, 0, tmpstr);
+       if (splitsize == 1) {
+               if (remempty == 0 || count == 1) {
+                       /* Copy the whole string */
+                       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
+                       mono_array_setref (retarr, 0, me);
+               } else {
+                       /* otherwise we have to filter out leading & trailing delims */
+
+                       /* find first non-delim char */
+                       for (; srcsize != 0; srcsize--, src++) {
+                               if (!string_icall_is_in_array (separator, arrsize, src [0]))
+                                       break;
+                       }
+                       /* find last non-delim char */
+                       for (; srcsize != 0; srcsize--) {
+                               if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1]))
+                                       break;
+                       }
+                       tmpstr = mono_string_new_size (mono_domain_get (), srcsize);
+                       tmpstrptr = mono_string_chars (tmpstr);
 
+                       memcpy (tmpstrptr, src, srcsize * sizeof (gunichar2));
+                       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 1);
+                       mono_array_setref (retarr, 0, tmpstr);
+               }
                return retarr;
        }
 
-       if (splitsize != count)
-               splitsize++;
+       lastpos = 0;
+       arrpos = 0;
+       
+       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), splitsize);
 
-       retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
-       for (i = 0; i != srcsize && arrpos != count; i++) {
-               if (string_icall_is_in_array(separator, arrsize, src[i])) {
-                       if (arrpos == count - 1)
-                               tmpstrsize = srcsize - lastpos;
-                       else
+       for (i = 0; i != srcsize && arrpos != splitsize; i++) {
+               if (string_icall_is_in_array (separator, arrsize, src [i])) {
+                       
+                       if (lastpos != i || remempty == 0) {
                                tmpstrsize = i - lastpos;
-
-                       tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
-                       tmpstrptr = mono_string_chars(tmpstr);
-
-                       memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
-                       mono_array_set(retarr, MonoString *, arrpos, tmpstr);
-                       arrpos++;
+                               tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
+                               tmpstrptr = mono_string_chars (tmpstr);
+
+                               memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
+                               mono_array_setref (retarr, arrpos, tmpstr);
+                               arrpos++;
+
+                               if (arrpos == splitsize - 1) {
+                                       /* Shortcut the last array element */
+
+                                       lastpos = i + 1;
+                                       if (remempty != 0) {
+                                               /* Search for non-delim starting char (guaranteed to find one) Note that loop
+                                                * condition is only there for safety. It will never actually terminate the loop. */
+                                               for (; lastpos != srcsize ; lastpos++) {
+                                                       if (!string_icall_is_in_array (separator, arrsize, src [lastpos])) 
+                                                               break;
+                                               }
+                                               if (count > splitsize) {
+                                                       /* Since we have fewer results than our limit, we must remove
+                                                        * trailing delimiters as well. 
+                                                        */
+                                                       for (; srcsize != lastpos + 1 ; srcsize--) {
+                                                               if (!string_icall_is_in_array (separator, arrsize, src [srcsize - 1])) 
+                                                                       break;
+                                                       }
+                                               }
+                                       }
+
+                                       tmpstrsize = srcsize - lastpos;
+                                       tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
+                                       tmpstrptr = mono_string_chars (tmpstr);
+
+                                       memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
+                                       mono_array_setref (retarr, arrpos, tmpstr);
+
+                                       /* Loop will ALWAYS end here. Test criteria in the FOR loop is technically unnecessary. */
+                                       break;
+                               }
+                       }
                        lastpos = i + 1;
                }
        }
 
-       if (arrpos < count) {
-               tmpstrsize = srcsize - lastpos;
-               tmpstr = mono_string_new_size( mono_domain_get (), tmpstrsize);
-               tmpstrptr = mono_string_chars(tmpstr);
-
-               memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
-               mono_array_set(retarr, MonoString *, arrpos, tmpstr);
-       }
-
        return retarr;
 }
 
@@ -547,111 +313,6 @@ ves_icall_System_String_InternalTrim (MonoString *me, MonoArray *chars, gint32 t
        return ret;
 }
 
-gint32 
-ves_icall_System_String_InternalIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
-{
-       gint32 pos;
-       gunichar2 *src;
-
-       MONO_ARCH_SAVE_REGS;
-
-       src = mono_string_chars(me);
-       for (pos = sindex; pos != count + sindex; pos++) {
-               if ( src [pos] == value)
-                       return pos;
-       }
-
-       return -1;
-}
-
-gint32 
-ves_icall_System_String_InternalIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
-{
-       gint32 lencmpstr;
-       gint32 pos, i;
-       gunichar2 *src;
-       gunichar2 *cmpstr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       lencmpstr = mono_string_length(value);
-
-       src = mono_string_chars(me);
-       cmpstr = mono_string_chars(value);
-
-       count -= lencmpstr;
-       for (pos = sindex; pos <= sindex + count; pos++) {
-               for (i = 0; src [pos + i] == cmpstr [i];) {
-                       if (++i == lencmpstr)
-                               return pos;
-               }
-       }
-
-       return -1;
-}
-
-gint32 
-ves_icall_System_String_InternalIndexOfAny (MonoString *me, MonoArray *arr, gint32 sindex, gint32 count)
-{
-       gint32 pos;
-       gint32 loop;
-       gint32 arraysize;
-       gunichar2 *src;
-
-       MONO_ARCH_SAVE_REGS;
-
-       arraysize = mono_array_length(arr);
-       src = mono_string_chars(me);
-
-       for (pos = sindex; pos != count + sindex; pos++) {
-               for (loop = 0; loop != arraysize; loop++)
-                       if ( src [pos] == mono_array_get(arr, gunichar2, loop) )
-                               return pos;
-       }
-
-       return -1;
-}
-
-gint32 
-ves_icall_System_String_InternalLastIndexOf_Char (MonoString *me, gunichar2 value, gint32 sindex, gint32 count)
-{
-       gint32 pos;
-       gunichar2 *src;
-
-       MONO_ARCH_SAVE_REGS;
-
-       src = mono_string_chars(me);
-       for (pos = sindex; pos > sindex - count; pos--) {
-               if (src [pos] == value)
-                       return pos;
-       }
-
-       return -1;
-}
-
-gint32 
-ves_icall_System_String_InternalLastIndexOf_Str (MonoString *me, MonoString *value, gint32 sindex, gint32 count)
-{
-       gint32 lencmpstr;
-       gint32 pos;
-       gunichar2 *src;
-       gunichar2 *cmpstr;
-
-       MONO_ARCH_SAVE_REGS;
-
-       lencmpstr = mono_string_length(value);
-
-       src = mono_string_chars(me);
-       cmpstr = mono_string_chars(value);
-
-       for (pos = sindex - lencmpstr + 1; pos > sindex - count; pos--) {
-               if (0 == memcmp(src + pos, cmpstr, lencmpstr * sizeof(gunichar2)))
-                       return pos;
-       }
-
-       return -1;
-}
-
 gint32 
 ves_icall_System_String_InternalLastIndexOfAny (MonoString *me, MonoArray *anyOf, gint32 sindex, gint32 count)
 {
@@ -711,80 +372,66 @@ ves_icall_System_String_InternalPad (MonoString *me, gint32 width, gunichar2 chr
 }
 
 MonoString *
-ves_icall_System_String_InternalToLower (MonoString *me)
+ves_icall_System_String_InternalAllocateStr (gint32 length)
 {
-       MonoString * ret;
-       gunichar2 *src; 
-       gunichar2 *dest;
-       gint32 i;
-
        MONO_ARCH_SAVE_REGS;
 
-       ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
-
-       src = mono_string_chars (me);
-       dest = mono_string_chars (ret);
-
-       for (i = 0; i < mono_string_length (me); ++i)
-               dest[i] = g_unichar_tolower(src[i]);
-
-       return ret;
+       return mono_string_new_size(mono_domain_get (), length);
 }
 
-MonoString *
-ves_icall_System_String_InternalToUpper (MonoString *me)
+void 
+ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
 {
-       int i;
-       MonoString * ret;
-       gunichar2 *src; 
-       gunichar2 *dest;
+       gunichar2 *srcptr;
+       gunichar2 *destptr;
 
        MONO_ARCH_SAVE_REGS;
 
-       ret = mono_string_new_size(mono_domain_get (), mono_string_length(me));
-
-       src = mono_string_chars (me);
-       dest = mono_string_chars (ret);
-
-       for (i = 0; i < mono_string_length (me); ++i)
-               dest[i] = g_unichar_toupper(src[i]);
+       srcptr = mono_string_chars (src);
+       destptr = mono_string_chars (dest);
 
-       return ret;
+       g_memmove (destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
 }
 
-MonoString *
-ves_icall_System_String_InternalAllocateStr (gint32 length)
+void 
+ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
 {
+       gunichar2 *srcptr;
+       gunichar2 *destptr;
+
        MONO_ARCH_SAVE_REGS;
 
-       return mono_string_new_size(mono_domain_get (), length);
+       srcptr = mono_string_chars (src);
+       destptr = mono_string_chars (dest);
+       g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
 }
 
 void 
-ves_icall_System_String_InternalStrcpy_Str (MonoString *dest, gint32 destPos, MonoString *src)
+ves_icall_System_String_InternalStrcpy_Chars (MonoString *dest, gint32 destPos, MonoArray *src)
 {
        gunichar2 *srcptr;
        gunichar2 *destptr;
 
        MONO_ARCH_SAVE_REGS;
 
-       srcptr = mono_string_chars (src);
+       srcptr = mono_array_addr (src, gunichar2, 0);
        destptr = mono_string_chars (dest);
 
-       memcpy(destptr + destPos, srcptr, mono_string_length(src) * sizeof(gunichar2));
+       g_memmove (destptr + destPos, srcptr, mono_array_length (src) * sizeof(gunichar2));
 }
 
 void 
-ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, MonoString *src, gint32 startPos, gint32 count)
+ves_icall_System_String_InternalStrcpy_CharsN (MonoString *dest, gint32 destPos, MonoArray *src, gint32 startPos, gint32 count)
 {
        gunichar2 *srcptr;
        gunichar2 *destptr;
 
        MONO_ARCH_SAVE_REGS;
 
-       srcptr = mono_string_chars (src);
+       srcptr = mono_array_addr (src, gunichar2, 0);
        destptr = mono_string_chars (dest);
-       memcpy(destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
+
+       g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
 }
 
 MonoString  *
@@ -803,69 +450,6 @@ ves_icall_System_String_InternalIsInterned (MonoString *str)
        return mono_string_is_interned(str);
 }
 
-gint32
-ves_icall_System_String_InternalCompareStr_N (MonoString *s1, gint32 i1, MonoString *s2, gint32 i2, gint32 length, gint32 mode)
-{
-       /* c translation of C# code from old string.cs.. :) */
-       gint32 lenstr1;
-       gint32 lenstr2;
-       gint32 charcmp;
-       gunichar2 *str1;
-       gunichar2 *str2;
-
-       gint32 pos;
-       
-       MONO_ARCH_SAVE_REGS;
-
-       lenstr1 = mono_string_length(s1);
-       lenstr2 = mono_string_length(s2);
-
-       str1 = mono_string_chars(s1);
-       str2 = mono_string_chars(s2);
-
-       pos = 0;
-
-       for (pos = 0; pos != length; pos++) {
-               if (i1 + pos >= lenstr1 || i2 + pos >= lenstr2)
-                       break;
-
-               charcmp = string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
-               if (charcmp != 0)
-                       return charcmp;
-       }
-
-       /* the lesser wins, so if we have looped until length we just need to check the last char */
-       if (pos == length) {
-               return string_icall_cmp_char(str1[i1 + pos - 1], str2[i2 + pos - 1], mode);
-       }
-
-       /* Test if one the strings has been compared to the end */
-       if (i1 + pos >= lenstr1) {
-               if (i2 + pos >= lenstr2)
-                       return 0;
-               else
-                       return -1;
-       } else if (i2 + pos >= lenstr2)
-               return 1;
-
-       /* if not, check our last char only.. (can this happen?) */
-       return string_icall_cmp_char(str1[i1 + pos], str2[i2 + pos], mode);
-}
-
-gint32
-ves_icall_System_String_GetHashCode (MonoString *me)
-{
-       int i, h = 0;
-       gunichar2 *data = mono_string_chars (me);
-
-       MONO_ARCH_SAVE_REGS;
-
-       for (i = 0; i < mono_string_length (me); ++i)
-               h = (h << 5) - h + data [i];
-
-       return h;
-}
-
 gunichar2 
 ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
 {
@@ -876,40 +460,3 @@ ves_icall_System_String_get_Chars (MonoString *me, gint32 idx)
        return mono_string_chars(me)[idx];
 }
 
-/*
- * @mode:
- * 0 = StringCompareModeDirect
- * 1 = StringCompareModeCaseInsensitive
- * 2 = StringCompareModeOrdinal
- */
-static gint32 
-string_icall_cmp_char (gunichar2 c1, gunichar2 c2, gint32 mode)
-{
-       gint32 result;
-       GUnicodeType c1type, c2type;
-
-       c1type = g_unichar_type (c1);
-       c2type = g_unichar_type (c2);
-
-       switch (mode) {
-       case 0: 
-               /* TODO: compare with culture info */
-               if (c1type == G_UNICODE_UPPERCASE_LETTER && c2type == G_UNICODE_LOWERCASE_LETTER)
-                       return 1;
-                                       
-               if (c1type == G_UNICODE_LOWERCASE_LETTER && c2type == G_UNICODE_UPPERCASE_LETTER)
-                       return -1;
-       
-               result = (gint32) c1 - c2;
-               break;
-       case 1: 
-               result = (gint32) (c1type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c1) : c1) - 
-                                 (c2type != G_UNICODE_LOWERCASE_LETTER ? g_unichar_tolower(c2) : c2);
-               break;
-       case 2:
-               // Rotor/ms return the full value just not -1 and 1
-               return (gint32) c1 - c2; break;
-       }
-
-       return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
-}