2007-03-09 Jonathan Chambers <joncham@gmail.com>
[mono.git] / mono / metadata / string-icalls.c
index 1fd8f400f2a8aeb55d91ec3e24e9310a6f5a114a..71bd1f7ebe0f84a29f8570e354c3669b487aec17 100644 (file)
 #include <signal.h>
 #include <string.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);
+static MonoString*
+empty_string (MonoDomain *domain)
+{
+       MonoVTable *vtable = mono_class_vtable (domain, mono_defaults.string_class);
+       MonoObject *o;
+       static MonoClassField *empty_field = NULL;
+
+       if (!empty_field) {
+               MonoClassField *field;
+               gpointer iter;
+
+               iter = NULL;
+               while ((field = mono_class_get_fields (mono_defaults.string_class, &iter))) {
+                       if (!strcmp (field->name, "Empty"))
+                               break;
+               }
+
+               g_assert (field);
+               empty_field = field;
+       }
+
+       mono_field_static_get_value (vtable, empty_field, &o);
+       g_assert (o);
+       return (MonoString*)o;
+}
 
 MonoString *
 ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
@@ -39,11 +63,10 @@ ves_icall_System_String_ctor_charp (gpointer dummy, gunichar2 *value)
        domain = mono_domain_get ();
 
        if (value == NULL)
-               length = 0;
-       else {
-               for (i = 0; *(value + i) != '\0'; i++);
-               length = i;
-       }
+               return empty_string (domain);
+
+       for (i = 0; *(value + i) != '\0'; i++);
+       length = i;
 
        return mono_string_new_utf16 (domain, value, length);
 }
@@ -87,10 +110,8 @@ ves_icall_System_String_ctor_charp_int_int (gpointer dummy, gunichar2 *value, gi
        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 ();
-       }
+       if (length == 0)
+               return empty_string (domain);
        
        begin = (gunichar2 *) (value + sindex);
 
@@ -106,10 +127,8 @@ ves_icall_System_String_ctor_sbytep (gpointer dummy, gint8 *value)
 
        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 ();
-       }
+       if (NULL == value)
+               return empty_string (domain);
 
        return mono_string_new (domain, (const char *) value);
 }
@@ -181,13 +200,49 @@ ves_icall_System_String_ctor_chara_int_int (gpointer dummy, MonoArray *value,
 
 MonoString *
 ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex, 
-                                   gint32 length, MonoObject *enc)
+                                      gint32 length, MonoObject *enc)
 {
+       MonoArray *arr;
+       MonoString *s;
+       MonoObject *exc;
+       MonoDomain *domain = mono_domain_get ();
+       MonoMethod *get_string;
+       gpointer args [1];
+       MonoClass *klass;
+
        MONO_ARCH_SAVE_REGS;
 
-       g_warning("string.ctor with encoding obj unimplemented");
+       if ((value == NULL) || (length == 0))
+               return mono_string_new_size (mono_domain_get (), 0);
+       if (enc == NULL)
+               mono_raise_exception (mono_get_exception_argument_null ("enc"));
+       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"));
+
+       arr = mono_array_new (domain, mono_defaults.byte_class, length);
+       memcpy (mono_array_addr (arr, guint8, 0), value + sindex, length);
+
+       /* Find the System.Text.Encoding class */
+       for (klass = enc->vtable->klass; klass->parent->parent != NULL; klass = klass->parent)
+               ;
+       
+       get_string = mono_class_get_method_from_name (klass, "GetString", 1);
+       args [0] = arr;
+       s = (MonoString*)mono_runtime_invoke (get_string, enc, args, &exc);
+       if (!s || exc)
+               mono_raise_exception (mono_get_exception_argument ("", "Unable to decode the array into a valid string."));
+
+       return s;
+}
+
+/* This function is redirected to String.CreateString ()
+   by mono_marshal_get_native_wrapper () */
+void
+ves_icall_System_String_ctor_RedirectToCreateString (void)
+{
        g_assert_not_reached ();
-       return NULL;
 }
 
 MonoString * 
@@ -361,8 +416,8 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
 
        /* if no split chars found return the string */
        if (splitsize == 0) {
-               retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, 1);
-               mono_array_set(retarr, MonoString *, 0, me);
+               retarr = mono_array_new(mono_domain_get(), mono_get_string_class (), 1);
+               mono_array_setref (retarr, 0, me);
 
                return retarr;
        }
@@ -370,7 +425,7 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
        if (splitsize != count)
                splitsize++;
 
-       retarr = mono_array_new(mono_domain_get(), mono_defaults.string_class, splitsize);
+       retarr = mono_array_new(mono_domain_get(), mono_get_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)
@@ -382,7 +437,7 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
                        tmpstrptr = mono_string_chars(tmpstr);
 
                        memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
-                       mono_array_set(retarr, MonoString *, arrpos, tmpstr);
+                       mono_array_setref (retarr, arrpos, tmpstr);
                        arrpos++;
                        lastpos = i + 1;
                }
@@ -394,7 +449,7 @@ ves_icall_System_String_InternalSplit (MonoString *me, MonoArray *separator, gin
                tmpstrptr = mono_string_chars(tmpstr);
 
                memcpy(tmpstrptr, src + lastpos, tmpstrsize * sizeof(gunichar2));
-               mono_array_set(retarr, MonoString *, arrpos, tmpstr);
+               mono_array_setref (retarr, arrpos, tmpstr);
        }
 
        return retarr;
@@ -617,34 +672,48 @@ ves_icall_System_String_InternalStrcpy_StrN (MonoString *dest, gint32 destPos, M
        g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
 }
 
-MonoString  *
-ves_icall_System_String_InternalIntern (MonoString *str)
+void 
+ves_icall_System_String_InternalStrcpy_Chars (MonoString *dest, gint32 destPos, MonoArray *src)
 {
+       gunichar2 *srcptr;
+       gunichar2 *destptr;
+
        MONO_ARCH_SAVE_REGS;
 
-       return mono_string_intern(str);
+       srcptr = mono_array_addr (src, gunichar2, 0);
+       destptr = mono_string_chars (dest);
+
+       g_memmove (destptr + destPos, srcptr, mono_array_length (src) * sizeof(gunichar2));
 }
 
-MonoString * 
-ves_icall_System_String_InternalIsInterned (MonoString *str)
+void 
+ves_icall_System_String_InternalStrcpy_CharsN (MonoString *dest, gint32 destPos, MonoArray *src, gint32 startPos, gint32 count)
 {
+       gunichar2 *srcptr;
+       gunichar2 *destptr;
+
        MONO_ARCH_SAVE_REGS;
 
-       return mono_string_is_interned(str);
+       srcptr = mono_array_addr (src, gunichar2, 0);
+       destptr = mono_string_chars (dest);
+
+       g_memmove (destptr + destPos, srcptr + startPos, count * sizeof(gunichar2));
 }
 
-gint32
-ves_icall_System_String_GetHashCode (MonoString *me)
+MonoString  *
+ves_icall_System_String_InternalIntern (MonoString *str)
 {
-       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 mono_string_intern(str);
+}
+
+MonoString * 
+ves_icall_System_String_InternalIsInterned (MonoString *str)
+{
+       MONO_ARCH_SAVE_REGS;
 
-       return h;
+       return mono_string_is_interned(str);
 }
 
 gunichar2 
@@ -657,40 +726,10 @@ 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;
-       }
+void
+ves_icall_System_String_InternalCharCopy (gunichar2 *src, gunichar2 *dest, gint32 count)
+{
+       MONO_ARCH_SAVE_REGS;
 
-       return ((result < 0) ? -1 : (result > 0) ? 1 : 0);
+       memcpy (dest, src, sizeof (gunichar2) * count);
 }