2008-11-28 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / metadata / string-icalls.c
index c92402fde162421c9d596b24508ebb932ea9ba62..9d7a1c503a035a113fdb6f4fffc8474873dcf5e2 100644 (file)
@@ -12,6 +12,7 @@
 #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>
@@ -106,6 +107,7 @@ typedef enum {
 MonoArray * 
 ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gint32 count, gint32 options)
 {
+       static MonoClass *String_array;
        MonoString * tmpstr;
        MonoArray * retarr;
        gunichar2 *src;
@@ -116,14 +118,16 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
        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;
+       }
 
        splitsize = 1;
        /* Count the number of elements we will return. Note that this operation
@@ -131,11 +135,11 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
         * 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]))
+               for (i = 0; i != srcsize && splitsize < count; i++) {
+                       if (string_icall_is_in_array (separator, arrsize, src [i]))
                                splitsize++;
-       }
-       else {
+               }
+       } 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.
@@ -144,9 +148,9 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
                lastpos = 0;
                flag = 0;
                for (i = 0; i != srcsize && splitsize < count; i++) {
-                       if (string_icall_is_in_array(separator, arrsize, src[i]))
+                       if (string_icall_is_in_array (separator, arrsize, src [i])) {
                                flag = 0;
-                       else if (flag == 0) {
+                       else if (flag == 0) {
                                if (lastpos == 1)
                                        splitsize++;
                                flag = 1;
@@ -155,41 +159,37 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
                }
 
                /* Nothing but separators */
-               if (lastpos == 0)
-               {
-                       retarr = mono_array_new(mono_domain_get(), mono_get_string_class(), 0);
+               if (lastpos == 0) {
+                       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), 0);
                        return retarr;
                }
        }
 
        /* if no split chars found return the string */
        if (splitsize == 1) {
-               if (remempty == 0 || count == 1)
-               {
+               if (remempty == 0 || count == 1) {
                        /* Copy the whole string */
-                       retarr = mono_array_new(mono_domain_get(), mono_get_string_class(), 1);
-                       mono_array_setref(retarr, 0, me);
-               }
-               else 
-               {
+                       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]))
+                       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]))
+                       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);
+                       }
+                       tmpstr = mono_string_new_size (mono_domain_get (), srcsize);
+                       tmpstrptr = mono_string_chars (tmpstr);
 
-                       memcpy(tmpstrptr, src, srcsize * sizeof(gunichar2));
-                       retarr = mono_array_new(mono_domain_get(), mono_get_string_class(), 1);
-                       mono_array_setref(retarr, 0, 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;
        }
@@ -197,51 +197,50 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
        lastpos = 0;
        arrpos = 0;
        
-       retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), splitsize);
+       retarr = mono_array_new_specific (mono_class_vtable (mono_domain_get (), String_array), splitsize);
 
        for (i = 0; i != srcsize && arrpos != splitsize; i++) {
-               if (string_icall_is_in_array(separator, arrsize, src[i])) {
+               if (string_icall_is_in_array (separator, arrsize, src [i])) {
                        
-                       if (lastpos != i || remempty == 0)
-                       {
+                       if (lastpos != i || remempty == 0) {
                                tmpstrsize = i - lastpos;
+                               tmpstr = mono_string_new_size (mono_domain_get (), tmpstrsize);
+                               tmpstrptr = mono_string_chars (tmpstr);
 
-                               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);
+                               memcpy (tmpstrptr, src + lastpos, tmpstrsize * sizeof (gunichar2));
+                               mono_array_setref (retarr, arrpos, tmpstr);
                                arrpos++;
 
-                               if (arrpos == splitsize - 1)
-                               {
+                               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) */
-                                               for ( ; ; lastpos++)
-                                                       if (!string_icall_is_in_array(separator, arrsize, src[lastpos])) 
+                                       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])) 
+                                               }
+                                               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);
+                                       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);
+                                       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 unnecessary. */
+                                       /* Loop will ALWAYS end here. Test criteria in the FOR loop is technically unnecessary. */
                                        break;
                                }
                        }