2005-03-08 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mono / metadata / string-icalls.c
index 4cda4a0f60433ff35c62324115b30789ea223f0a..38e8a5668c96b2a4d7a6afcaaebc1d44dc7135a0 100644 (file)
 static gboolean
 string_icall_is_in_array (MonoArray *chars, gint32 arraylength, gunichar2 chr);
 
+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)
 {
@@ -86,10 +112,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);
 
@@ -105,10 +129,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);
 }
@@ -182,7 +204,6 @@ MonoString *
 ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sindex, 
                                    gint32 length, MonoObject *enc)
 {
-       MONO_ARCH_SAVE_REGS;
        MonoArray *arr;
        MonoString *s;
        MonoObject *exc;
@@ -190,6 +211,8 @@ ves_icall_System_String_ctor_encoding (gpointer dummy, gint8 *value, gint32 sind
        MonoMethod *get_string;
        gpointer args [1];
 
+       MONO_ARCH_SAVE_REGS;
+
        if ((value == NULL) || (length == 0))
                return mono_string_new_size (mono_domain_get (), 0);
        if (enc == NULL)
@@ -682,20 +705,6 @@ ves_icall_System_String_InternalIsInterned (MonoString *str)
        return mono_string_is_interned(str);
 }
 
-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)
 {