< Summary

Class:Azure.Core.Http.Multipart.InplaceStringBuilder
Assembly:Azure.Storage.Blobs.Batch
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\InplaceStringBuilder.cs
Covered lines:0
Uncovered lines:48
Coverable lines:48
Total lines:138
Line coverage:0% (0 of 48)
Covered branches:0
Total branches:36
Branch coverage:0% (0 of 36)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%0%
get_Capacity()-0%100%
set_Capacity(...)-0%0%
Append(...)-0%0%
Append(...)-0%100%
Append(...)-0%0%
Append(...)-0%0%
ToString()-0%0%
EnsureValueIsInitialized()-0%0%
ThrowValidationError(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Blobs.Batch\src\Shared\InplaceStringBuilder.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4// Copied from https://github.com/aspnet/Extensions/tree/master/src/Primitives/src
 5
 6using System;
 7using System.Diagnostics;
 8using System.Runtime.CompilerServices;
 9
 10namespace Azure.Core.Http.Multipart
 11{
 12    [DebuggerDisplay("Value = {_value}")]
 13    [Obsolete("This type is obsolete and will be removed in a future version.")]
 14    internal struct InplaceStringBuilder
 15    {
 16        private int _offset;
 17        private int _capacity;
 18        private string _value;
 19
 020        public InplaceStringBuilder(int capacity) : this()
 21        {
 022            if (capacity < 0)
 23            {
 024                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity);
 25            }
 26
 027            _capacity = capacity;
 028        }
 29
 30        public int Capacity
 31        {
 032            get => _capacity;
 33            set
 34            {
 035                if (value < 0)
 36                {
 037                    ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value);
 38                }
 39
 40                // _offset > 0 indicates writing state
 041                if (_offset > 0)
 42                {
 043                    ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_CannotChangeAfterWriteStarted)
 44                }
 45
 046                _capacity = value;
 047            }
 48        }
 49
 50        public void Append(string value)
 51        {
 052            if (value == null)
 53            {
 054                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
 55            }
 56
 057            Append(value, 0, value.Length);
 058        }
 59
 60        public void Append(StringSegment segment)
 61        {
 062            Append(segment.Buffer, segment.Offset, segment.Length);
 063        }
 64
 65        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 66        public unsafe void Append(string value, int offset, int count)
 67        {
 068            EnsureValueIsInitialized();
 69
 070            if (value == null
 071                || offset < 0
 072                || value.Length - offset < count
 073                || Capacity - _offset < count)
 74            {
 075                ThrowValidationError(value, offset, count);
 76            }
 77
 078            fixed (char* destination = _value)
 079            fixed (char* source = value)
 80            {
 081                Unsafe.CopyBlockUnaligned(destination + _offset, source + offset, (uint)count * 2);
 082                _offset += count;
 83            }
 084        }
 85
 86        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 87        public unsafe void Append(char c)
 88        {
 089            EnsureValueIsInitialized();
 90
 091            if (_offset >= Capacity)
 92            {
 093                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, 1, Capacity - _offset);
 94            }
 95
 096            fixed (char* destination = _value)
 97            {
 098                destination[_offset++] = c;
 99            }
 0100        }
 101
 102        public override string ToString()
 103        {
 0104            if (Capacity != _offset)
 105            {
 0106                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotUsedEntirely, Capacity, _offset
 107            }
 108
 0109            return _value;
 110        }
 111
 112        private void EnsureValueIsInitialized()
 113        {
 0114            if (_value == null)
 115            {
 0116                _value = new string('\0', _capacity);
 117            }
 0118        }
 119
 120        private void ThrowValidationError(string value, int offset, int count)
 121        {
 0122            if (value == null)
 123            {
 0124                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
 125            }
 126
 0127            if (offset < 0 || value.Length - offset < count)
 128            {
 0129                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset);
 130            }
 131
 0132            if (Capacity - _offset < count)
 133            {
 0134                ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, value.Length, Capacity 
 135            }
 0136        }
 137    }
 138}