From 007e08ec2ae2c86c11f90218224be2e37adaa572 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 20 Feb 2018 08:14:57 -0800 Subject: [PATCH] Replace ValueStringBuilder.set_Length with indexer (dotnet/corefx#27274) 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 --- .../System/Collections/Generic/ValueListBuilder.cs | 9 +++++++ .../shared/System/Text/ValueStringBuilder.cs | 28 +++++++++------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/mscorlib/shared/System/Collections/Generic/ValueListBuilder.cs b/src/mscorlib/shared/System/Collections/Generic/ValueListBuilder.cs index 1bc8ee6..26b3c9f 100644 --- a/src/mscorlib/shared/System/Collections/Generic/ValueListBuilder.cs +++ b/src/mscorlib/shared/System/Collections/Generic/ValueListBuilder.cs @@ -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) { diff --git a/src/mscorlib/shared/System/Text/ValueStringBuilder.cs b/src/mscorlib/shared/System/Text/ValueStringBuilder.cs index 99045ca..e077a34 100644 --- a/src/mscorlib/shared/System/Text/ValueStringBuilder.cs +++ b/src/mscorlib/shared/System/Text/ValueStringBuilder.cs @@ -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 -- 2.7.4