2009-11-05 Rodrigo Kumpera <rkumpera@novell.com>
authorRodrigo Kumpera <kumpera@gmail.com>
Thu, 5 Nov 2009 21:37:19 +0000 (21:37 -0000)
committerRodrigo Kumpera <kumpera@gmail.com>
Thu, 5 Nov 2009 21:37:19 +0000 (21:37 -0000)
* object-internals.h: Add mono_string_to_utf8_checked.

* object.c: Implement mono_string_to_utf8_checked.

svn path=/trunk/mono/; revision=145539

mono/metadata/ChangeLog
mono/metadata/object-internals.h
mono/metadata/object.c

index 1a501e6f8cd5885572b218b2e7bb3440b26bb63a..7f3113c29b94f9d5071ea4dc396f8f9b374650b4 100644 (file)
        * mono-debug.c (mono_is_debugger_attached): New helper function.
        (mono_set_is_debugger_attached): Ditto.
 
+2009-11-05  Rodrigo Kumpera  <rkumpera@novell.com>
+
+       * object-internals.h: Add mono_string_to_utf8_checked.
+
+       * object.c: Implement mono_string_to_utf8_checked.
+
 2009-11-05  Rodrigo Kumpera  <rkumpera@novell.com>
 
        * class.c: Add missing check for load errors after every
index dfa1bfeb4851e5d519b6b8919f6e98259104b21d..e65d05ff23d941e4f18c470cea0b153cae9775f3 100644 (file)
@@ -1450,6 +1450,9 @@ mono_class_compute_gc_descriptor (MonoClass *class) MONO_INTERNAL;
 MonoObject*
 mono_object_xdomain_representation (MonoObject *obj, MonoDomain *target_domain, MonoObject **exc) MONO_INTERNAL;
 
+char *
+mono_string_to_utf8_checked (MonoString *s, MonoError *error) MONO_INTERNAL;
+
 #endif /* __MONO_OBJECT_INTERNALS_H__ */
 
 
index 89fc79ee1b5f5ed2538ebd5d550de43e0556e8c6..4bf47dbcf3a55ced8feff312f34bb6c6e4acbbef 100644 (file)
@@ -40,6 +40,7 @@
 #include <mono/metadata/gc-internal.h>
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-counters.h>
+#include <mono/utils/mono-error-internals.h>
 #include "cominterop.h"
 
 #ifdef HAVE_BOEHM_GC
@@ -5081,13 +5082,28 @@ mono_ldstr_metadata_sig (MonoDomain *domain, const char* sig)
  *
  * Return the UTF8 representation for @s.
  * the resulting buffer nedds to be freed with g_free().
+ *
+ * @deprecated Use mono_string_to_utf8_checked to avoid having an exception arbritraly raised.
  */
 char *
 mono_string_to_utf8 (MonoString *s)
+{
+       MonoError error;
+       char *result = mono_string_to_utf8_checked (s, &error);
+       
+       if (!mono_error_ok (&error))
+               mono_error_raise_exception (&error);
+       return result;
+}
+
+char *
+mono_string_to_utf8_checked (MonoString *s, MonoError *error)
 {
        long written = 0;
        char *as;
-       GError *error = NULL;
+       GError *gerror = NULL;
+
+       mono_error_init (error);
 
        if (s == NULL)
                return NULL;
@@ -5095,11 +5111,11 @@ mono_string_to_utf8 (MonoString *s)
        if (!s->length)
                return g_strdup ("");
 
-       as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &written, &error);
-       if (error) {
-               MonoException *exc = mono_get_exception_argument ("string", error->message);
-               g_error_free (error);
-               mono_raise_exception(exc);
+       as = g_utf16_to_utf8 (mono_string_chars (s), s->length, NULL, &written, &gerror);
+       if (gerror) {
+               mono_error_set_argument (error, "string", "%s", gerror->message);
+               g_error_free (gerror);
+               return NULL;
        }
        /* g_utf16_to_utf8  may not be able to complete the convertion (e.g. NULL values were found, #335488) */
        if (s->length > written) {