Replace ValueStringBuilder.set_Length with indexer (dotnet/corefx#27274)
authorJan Kotas <jkotas@microsoft.com>
Tue, 20 Feb 2018 16:14:57 +0000 (08:14 -0800)
committerJan Kotas <jkotas@microsoft.com>
Tue, 20 Feb 2018 17:55:15 +0000 (09:55 -0800)
Indexer that let's you see and edit the content of the pending string is more efficient and flexible.

Fixes #26643

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
src/mscorlib/shared/System/Collections/Generic/ValueListBuilder.cs
src/mscorlib/shared/System/Text/ValueStringBuilder.cs

index 1bc8ee6..26b3c9f 100644 (file)
@@ -23,6 +23,15 @@ namespace System.Collections.Generic
 
         public int Length => _pos;
 
+        public ref T this[int index]
+        {
+            get
+            {
+                Debug.Assert(index < _pos);
+                return ref _span[index];
+            }
+        }
+
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public void Append(T item)
         {
index 99045ca..e077a34 100644 (file)
@@ -21,27 +21,21 @@ namespace System.Text
             _pos = 0;
         }
 
-        public int Length
+        public int Length => _pos;
+
+        public ref char this[int index]
         {
-            get => _pos;
-            set
-            {
-                int delta = value - _pos;
-                if (delta > 0)
-                {
-                    Append('\0', delta);
-                }
-                else
-                {
-                    _pos = value;
-                }
+            get
+            {
+                Debug.Assert(index < _pos);
+                return ref _chars[index];
             }
         }
 
         public override string ToString()
         {
             var s = new string(_chars.Slice(0, _pos));
-            Clear();
+            Dispose();
             return s;
         }
 
@@ -50,13 +44,13 @@ namespace System.Text
             if (_chars.Slice(0, _pos).TryCopyTo(destination))
             {
                 charsWritten = _pos;
-                Clear();
+                Dispose();
                 return true;
             }
             else
             {
                 charsWritten = 0;
-                Clear();
+                Dispose();
                 return false;
             }
         }
@@ -185,7 +179,7 @@ namespace System.Text
         }
 
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
-        private void Clear()
+        public void Dispose()
         {
             char[] toReturn = _arrayToReturnToPool;
             this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again