< Summary

Line coverage
77%
Covered lines: 94
Uncovered lines: 28
Coverable lines: 122
Total lines: 303
Line coverage: 77%
Branch coverage
70%
Covered branches: 17
Total branches: 24
Branch coverage: 70.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 1: AppendSpanFormattable(...)75%44100%
File 2: .ctor(...)100%11100%
File 2: .ctor(...)100%11100%
File 2: EnsureCapacity(...)0%220%
File 2: ToString()100%11100%
File 2: AsSpan()100%110%
File 2: Append(...)100%22100%
File 2: Append(...)83.33%6685.71%
File 2: AppendSlow(...)100%22100%
File 2: Append(...)50%2266.66%
File 2: AppendSpan(...)0%220%
File 2: GrowAndAppend(...)100%11100%
File 2: Grow(...)100%22100%
File 2: Dispose()100%22100%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\Text\ValueStringBuilder.AppendSpanFormattable.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4namespace System.Text
 5{
 6    internal ref partial struct ValueStringBuilder
 7    {
 8        internal void AppendSpanFormattable<T>(T value, string? format = null, IFormatProvider? provider = null) where T
 1030689        {
 10306810            if (value.TryFormat(_chars.Slice(_pos), out int charsWritten, format, provider))
 10285811            {
 10285812                _pos += charsWritten;
 10285813            }
 14            else
 21015            {
 21016                Append(value.ToString(format, provider));
 21017            }
 10306818        }
 19    }
 20}

D:\runner\runtime\src\libraries\Common\src\System\Text\ValueStringBuilder.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Runtime.CompilerServices;
 7using System.Runtime.InteropServices;
 8
 9#nullable enable
 10
 11namespace System.Text
 12{
 13    internal ref partial struct ValueStringBuilder
 14    {
 15        private char[]? _arrayToReturnToPool;
 16        private Span<char> _chars;
 17        private int _pos;
 18
 19        public ValueStringBuilder(Span<char> initialBuffer)
 8398020        {
 8398021            _arrayToReturnToPool = null;
 8398022            _chars = initialBuffer;
 8398023            _pos = 0;
 8398024        }
 25
 26        public ValueStringBuilder(int initialCapacity)
 1827        {
 1828            _arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity);
 1829            _chars = _arrayToReturnToPool;
 1830            _pos = 0;
 1831        }
 32
 33        public int Length
 34        {
 035            get => _pos;
 36            set
 037            {
 038                Debug.Assert(value >= 0);
 039                Debug.Assert(value <= _chars.Length);
 040                _pos = value;
 041            }
 42        }
 43
 044        public int Capacity => _chars.Length;
 45
 46        public void EnsureCapacity(int capacity)
 047        {
 48            // This is not expected to be called this with negative capacity
 049            Debug.Assert(capacity >= 0);
 50
 51            // If the caller has a bug and calls this with negative capacity, make sure to call Grow to throw an excepti
 052            if ((uint)capacity > (uint)_chars.Length)
 053                Grow(capacity - _pos);
 054        }
 55
 56        /// <summary>
 57        /// Ensures that the builder is terminated with a NUL character.
 58        /// </summary>
 59        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 60        public void NullTerminate()
 61        {
 62            EnsureCapacity(_pos + 1);
 63            _chars[_pos] = '\0';
 64        }
 65
 66        /// <summary>
 67        /// Get a pinnable reference to the builder.
 68        /// Does not ensure there is a null char after <see cref="Length"/>
 69        /// This overload is pattern matched in the C# 7.3+ compiler so you can omit
 70        /// the explicit method call, and write eg "fixed (char* c = builder)"
 71        /// </summary>
 72        public ref char GetPinnableReference()
 73        {
 74            return ref MemoryMarshal.GetReference(_chars);
 75        }
 76
 77        public ref char this[int index]
 78        {
 79            get
 80            {
 81                Debug.Assert(index < _pos);
 82                return ref _chars[index];
 83            }
 84        }
 85
 86        public override string ToString()
 8366887        {
 8366888            string s = _chars.Slice(0, _pos).ToString();
 8366889            Dispose();
 8366890            return s;
 8366891        }
 92
 93        /// <summary>Returns the underlying storage of the builder.</summary>
 094        public Span<char> RawChars => _chars;
 95
 096        public ReadOnlySpan<char> AsSpan() => _chars.Slice(0, _pos);
 97        public ReadOnlySpan<char> AsSpan(int start) => _chars.Slice(start, _pos - start);
 98        public ReadOnlySpan<char> AsSpan(int start, int length) => _chars.Slice(start, length);
 99
 100        public void Insert(int index, char value, int count)
 101        {
 102            if (_pos > _chars.Length - count)
 103            {
 104                Grow(count);
 105            }
 106
 107            int remaining = _pos - index;
 108            _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
 109            _chars.Slice(index, count).Fill(value);
 110            _pos += count;
 111        }
 112
 113        public void Insert(int index, string? s)
 114        {
 115            if (s == null)
 116            {
 117                return;
 118            }
 119
 120            int count = s.Length;
 121
 122            if (_pos > (_chars.Length - count))
 123            {
 124                Grow(count);
 125            }
 126
 127            int remaining = _pos - index;
 128            _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
 129            s
 130#if !NET
 131                .AsSpan()
 132#endif
 133                .CopyTo(_chars.Slice(index));
 134            _pos += count;
 135        }
 136
 137        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 138        public void Append(char c)
 178254139        {
 178254140            int pos = _pos;
 178254141            Span<char> chars = _chars;
 178254142            if ((uint)pos < (uint)chars.Length)
 178038143            {
 178038144                chars[pos] = c;
 178038145                _pos = pos + 1;
 178038146            }
 147            else
 216148            {
 216149                GrowAndAppend(c);
 216150            }
 178254151        }
 152
 153        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 154        public void Append(string? s)
 219906155        {
 219906156            if (s == null)
 0157            {
 0158                return;
 159            }
 160
 219906161            int pos = _pos;
 219906162            if (s.Length == 1 && (uint)pos < (uint)_chars.Length) // very common case, e.g. appending strings from Numbe
 20638163            {
 20638164                _chars[pos] = s[0];
 20638165                _pos = pos + 1;
 20638166            }
 167            else
 199268168            {
 199268169                AppendSlow(s);
 199268170            }
 219906171        }
 172
 173        private void AppendSlow(string s)
 199268174        {
 199268175            int pos = _pos;
 199268176            if (pos > _chars.Length - s.Length)
 2138177            {
 2138178                Grow(s.Length);
 2138179            }
 180
 199268181            s
 199268182#if !NET
 199268183                .AsSpan()
 199268184#endif
 199268185                .CopyTo(_chars.Slice(pos));
 199268186            _pos += s.Length;
 199268187        }
 188
 189        public void Append(char c, int count)
 190        {
 191            if (_pos > _chars.Length - count)
 192            {
 193                Grow(count);
 194            }
 195
 196            Span<char> dst = _chars.Slice(_pos, count);
 197            for (int i = 0; i < dst.Length; i++)
 198            {
 199                dst[i] = c;
 200            }
 201            _pos += count;
 202        }
 203
 204        public void Append(scoped ReadOnlySpan<char> value)
 1142205        {
 1142206            int pos = _pos;
 1142207            if (pos > _chars.Length - value.Length)
 0208            {
 0209                Grow(value.Length);
 0210            }
 211
 1142212            value.CopyTo(_chars.Slice(_pos));
 1142213            _pos += value.Length;
 1142214        }
 215
 216        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 217        public Span<char> AppendSpan(int length)
 0218        {
 0219            int origPos = _pos;
 0220            if (origPos > _chars.Length - length)
 0221            {
 0222                Grow(length);
 0223            }
 224
 0225            _pos = origPos + length;
 0226            return _chars.Slice(origPos, length);
 0227        }
 228
 229        [MethodImpl(MethodImplOptions.NoInlining)]
 230        private void GrowAndAppend(char c)
 216231        {
 216232            Grow(1);
 216233            Append(c);
 216234        }
 235
 236        /// <summary>
 237        /// Resize the internal buffer either by doubling current buffer size or
 238        /// by adding <paramref name="additionalCapacityBeyondPos"/> to
 239        /// <see cref="_pos"/> whichever is greater.
 240        /// </summary>
 241        /// <param name="additionalCapacityBeyondPos">
 242        /// Number of chars requested beyond current position.
 243        /// </param>
 244        [MethodImpl(MethodImplOptions.NoInlining)]
 245        private void Grow(int additionalCapacityBeyondPos)
 2354246        {
 2354247            Debug.Assert(additionalCapacityBeyondPos > 0);
 2354248            Debug.Assert(_pos > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is need
 249
 250            const uint ArrayMaxLength = 0x7FFFFFC7; // same as Array.MaxLength
 251
 252            // Increase to at least the required size (_pos + additionalCapacityBeyondPos), but try
 253            // to double the size if possible, bounding the doubling to not go beyond the max array length.
 2354254            int newCapacity = (int)Math.Max(
 2354255                (uint)(_pos + additionalCapacityBeyondPos),
 2354256                Math.Min((uint)_chars.Length * 2, ArrayMaxLength));
 257
 258            // Make sure to let Rent throw an exception if the caller has a bug and the desired capacity is negative.
 259            // This could also go negative if the actual required length wraps around.
 2354260            char[] poolArray = ArrayPool<char>.Shared.Rent(newCapacity);
 261
 2354262            _chars.Slice(0, _pos).CopyTo(poolArray);
 263
 2354264            char[]? toReturn = _arrayToReturnToPool;
 2354265            _chars = _arrayToReturnToPool = poolArray;
 2354266            if (toReturn != null)
 282267            {
 282268                ArrayPool<char>.Shared.Return(toReturn);
 282269            }
 2354270        }
 271
 272        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 273        public void Dispose()
 83998274        {
 83998275            char[]? toReturn = _arrayToReturnToPool;
 83998276            this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
 83998277            if (toReturn != null)
 2090278            {
 2090279                ArrayPool<char>.Shared.Return(toReturn);
 2090280            }
 83998281        }
 282    }
 283}