Add all missing sequential layout directives to corlib and system.
[mono.git] / mcs / class / corlib / System.Text / StringBuilder.cs
index f9ff5c3425936f6e0e6a9de7c0702adc281a4977..bfc8a8925e5db98a753853492dbdb5598a6b0016 100644 (file)
@@ -16,6 +16,7 @@
 
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright 2011 Xamarin Inc
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -43,23 +44,24 @@ using System.Runtime.InteropServices;
 namespace System.Text {
        
        [Serializable]
-#if NET_2_0
        [ComVisible (true)]
-#endif
-        [MonoTODO ("Serialization format not compatible with .NET")]
-       public sealed class StringBuilder
-#if NET_2_0
-               : ISerializable
-#endif
+        [MonoLimitation ("Serialization format not compatible with .NET")]
+       [StructLayout (LayoutKind.Sequential)]
+       public sealed class StringBuilder : ISerializable
        {
                private int _length;
                private string _str;
                private string _cached_str;
                
-               private int _maxCapacity = Int32.MaxValue;
+               private int _maxCapacity;
                private const int constDefaultCapacity = 16;
 
                public StringBuilder(string value, int startIndex, int length, int capacity) 
+                       : this (value, startIndex, length, capacity, Int32.MaxValue)
+               {
+               }
+
+               private StringBuilder(string value, int startIndex, int length, int capacity, int maxCapacity)
                {
                        // first, check the parameters and throw appropriate exceptions if needed
                        if (null == value)
@@ -76,15 +78,26 @@ namespace System.Text {
                        if (capacity < 0)
                                throw new System.ArgumentOutOfRangeException ("capacity", capacity, "capacity must be greater than zero.");
 
+                       if (maxCapacity < 1)
+                               throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
+                       if (capacity > maxCapacity)
+                               throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
+
                        // make sure startIndex and length give a valid substring of value
                        // re-ordered to avoid possible integer overflow
                        if (startIndex > value.Length - length)
                                throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex and length must refer to a location within the string.");
 
-                       if (capacity == 0)
-                               capacity = constDefaultCapacity;
+                       if (capacity == 0) {
+                               if (maxCapacity > constDefaultCapacity)
+                                       capacity = constDefaultCapacity;
+                               else
+                                       _str = _cached_str = String.Empty;
+                       }
+                       _maxCapacity = maxCapacity;
 
-                       _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
+                       if (_str == null)
+                               _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
                        if (length > 0)
                                String.CharCopy (_str, 0, value, startIndex, length);
                        
@@ -95,14 +108,7 @@ namespace System.Text {
 
                public StringBuilder(int capacity) : this (String.Empty, 0, 0, capacity) {}
 
-               public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity) {
-                       if (maxCapacity < 1)
-                               throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
-                       if (capacity > maxCapacity)
-                               throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
-
-                       _maxCapacity = maxCapacity;
-               }
+               public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity, maxCapacity) { }
 
                public StringBuilder (string value)
                {
@@ -116,13 +122,13 @@ namespace System.Text {
                        
                        _length = value.Length;
                        _str = _cached_str = value;
+                       _maxCapacity = Int32.MaxValue;
                }
        
-               public StringBuilder( string value, int capacity) : this(value, 0, value.Length, capacity) {}
+               public StringBuilder( string value, int capacity) : this(value == null ? "" : value, 0, value == null ? 0 : value.Length, capacity) {}
        
                public int MaxCapacity {
                        get {
-                               // MS runtime always returns Int32.MaxValue.
                                return _maxCapacity;
                        }
                }
@@ -130,7 +136,7 @@ namespace System.Text {
                public int Capacity {
                        get {
                                if (_str.Length == 0)
-                                       return constDefaultCapacity;
+                                       return Math.Min (_maxCapacity, constDefaultCapacity);
                                
                                return _str.Length;
                        }
@@ -139,6 +145,9 @@ namespace System.Text {
                                if (value < _length)
                                        throw new ArgumentException( "Capacity must be larger than length" );
 
+                               if (value > _maxCapacity)
+                                       throw new ArgumentOutOfRangeException ("value", "Should be less than or equal to MaxCapacity");
+
                                InternalEnsureCapacity(value);
                        }
                }
@@ -201,9 +210,11 @@ namespace System.Text {
                                return _cached_str;
 
                        // If we only have a half-full buffer we return a new string.
-                       if (_length < (_str.Length >> 1)
+                       if (_length < (_str.Length >> 1) || (_str.Length > string.LOS_limit && _length <= string.LOS_limit))
                        {
-                               _cached_str = _str.Substring(0, _length);
+                               // use String.SubstringUnchecked instead of String.Substring
+                               // as the former is guaranteed to create a new string object
+                               _cached_str = _str.SubstringUnchecked (0, _length);
                                return _cached_str;
                        }
 
@@ -219,7 +230,12 @@ namespace System.Text {
                        if (startIndex < 0 || length < 0 || startIndex > _length - length)
                                throw new ArgumentOutOfRangeException();
 
-                       return _str.Substring (startIndex, length);
+                       // use String.SubstringUnchecked instead of String.Substring
+                       // as the former is guaranteed to create a new string object
+                       if (startIndex == 0 && length == _length)
+                               return ToString ();
+                       else
+                               return _str.SubstringUnchecked (startIndex, length);
                }
 
                public int EnsureCapacity (int capacity) 
@@ -302,15 +318,22 @@ namespace System.Text {
                        if (oldValue.Length == 0)
                                throw new ArgumentException ("The old value cannot be zero length.");
 
-                       // TODO: OPTIMIZE!
-                       string replace = _str.Substring(startIndex, count).Replace(oldValue, newValue);
+                       string substr = _str.Substring(startIndex, count);
+                       string replace = substr.Replace(oldValue, newValue);
+                       // return early if no oldValue was found
+                       if ((object) replace == (object) substr)
+                               return this;
 
                        InternalEnsureCapacity (replace.Length + (_length - count));
 
-                       string end = _str.Substring (startIndex + count, _length - startIndex - count );
+                       // shift end part
+                       if (replace.Length < count)
+                               String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex  - count);
+                       else if (replace.Length > count)
+                               String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex  - count);
 
+                       // copy middle part back into _str
                        String.CharCopy (_str, startIndex, replace, 0, replace.Length);
-                       String.CharCopy (_str, startIndex + replace.Length, end, 0, end.Length);
                        
                        _length = replace.Length + (_length - count);
 
@@ -482,7 +505,14 @@ namespace System.Text {
                        return this;
                }
 
-#if NET_2_0
+#if NET_4_0 || MOONLIGHT || MOBILE
+               public StringBuilder Clear ()
+               {
+                       Length = 0;
+                       return this;
+               }
+#endif
+
                [ComVisible (false)]
                public StringBuilder AppendLine ()
                {
@@ -494,7 +524,6 @@ namespace System.Text {
                {
                        return Append (value).Append (System.Environment.NewLine);
                }
-#endif
 
                public StringBuilder AppendFormat (string format, params object[] args)
                {
@@ -509,7 +538,7 @@ namespace System.Text {
                        return this;
                }
 
-#if NET_2_1
+#if MOONLIGHT
                internal
 #else
                public
@@ -519,7 +548,7 @@ namespace System.Text {
                        return AppendFormat (null, format, new object [] { arg0 });
                }
 
-#if NET_2_1
+#if MOONLIGHT
                internal
 #else
                public
@@ -529,7 +558,7 @@ namespace System.Text {
                        return AppendFormat (null, format, new object [] { arg0, arg1 });
                }
 
-#if NET_2_1
+#if MOONLIGHT
                internal
 #else
                public
@@ -706,7 +735,6 @@ namespace System.Text {
                        _cached_str = null;
                }
 
-#if NET_2_0
                [ComVisible (false)]
                public void CopyTo (int sourceIndex, char [] destination, int destinationIndex, int count)
                {
@@ -742,6 +770,5 @@ namespace System.Text {
                                _maxCapacity = Int32.MaxValue;
                        Capacity = info.GetInt32 ("Capacity");
                }
-#endif
        }
 }