2005-01-24 Ben Maurer <bmaurer@ximian.com>
authorBen Maurer <benm@mono-cvs.ximian.com>
Mon, 24 Jan 2005 16:42:07 +0000 (16:42 -0000)
committerBen Maurer <benm@mono-cvs.ximian.com>
Mon, 24 Jan 2005 16:42:07 +0000 (16:42 -0000)
* marshal.c (mono_string_utf8_to_builder)
(mono_string_builder_to_utf16): We might not have ownership of the
string. In thise case, we need to create a new buffer.

* object-internals.h (mono_stringbuilder_capacity): sb->str might
be null, in which case, use the default capacity.

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

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

index 7fcf5240a261732b3fc00da0ff38a1d0aed8b1e7..313fe276caabacb7d5ea37d3b11fb5654fce34e7 100644 (file)
@@ -1,3 +1,11 @@
+2005-01-24  Ben Maurer  <bmaurer@ximian.com>
+
+       * marshal.c (mono_string_utf8_to_builder)
+       (mono_string_builder_to_utf16): We might not have ownership of the
+       string. In thise case, we need to create a new buffer.
+
+       * object-internals.h (mono_stringbuilder_capacity): sb->str might
+       be null, in which case, use the default capacity.
 
 Mon Jan 24 16:42:29 CET 2005 Paolo Molaro <lupus@ximian.com>
 
index ad4998ba3098dec9ec8d2c4b5d5bdc20ad3ab670..fbf3f68c148d4b32ff0ba149e7ad54dec1555096 100644 (file)
@@ -340,6 +340,11 @@ mono_string_utf8_to_builder (MonoStringBuilder *sb, char *text)
                items_written = mono_stringbuilder_capacity (sb);
        
        if (!error) {
+               if (! sb->str || sb->str == sb->cached_str) {
+                       sb->str = mono_string_new_size (mono_domain_get (), items_written);
+                       sb->cached_str = NULL;
+               }
+               
                memcpy (mono_string_chars (sb->str), ut, items_written * 2);
                sb->length = items_written;
        } else 
@@ -395,6 +400,22 @@ mono_string_builder_to_utf16 (MonoStringBuilder *sb)
        if (!sb)
                return NULL;
 
+       /* 
+        * The sb could have been allocated with the default capacity and be empty.
+        * we need to alloc a buffer of the default capacity in this case.
+        */
+       if (! sb->str)
+               sb->str = mono_string_new_size (mono_domain_get (), mono_stringbuilder_capacity (sb));
+       /*
+        * The stringbuilder might not have ownership of this string. If this is
+        * the case, we must duplicate the string, so that we don't munge immutable
+        * strings
+        */
+       else if (sb->str == sb->cached_str) {
+               sb->str = mono_string_new_utf16 (mono_domain_get (), mono_string_chars (sb->str), mono_stringbuilder_capacity (sb));
+               sb->cached_str = NULL;
+       }
+       
        return mono_string_chars (sb->str);
 }
 
index e721f892006e38aa1b7ce46f7fe324203394505f..2761cfddb84beec424dc5416df1a9ef2bac07125 100644 (file)
@@ -76,7 +76,8 @@
        };                              }G_STMT_END
 
 
-#define mono_stringbuilder_capacity(sb) ((sb)->str->length)
+/* 16 == default capacity */
+#define mono_stringbuilder_capacity(sb) ((sb)->str ? ((sb)->str->length) : 16)
 
 typedef struct {
        MonoObject obj;