| | 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 | |
|
| | 6 | | using System; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Runtime.CompilerServices; |
| | 9 | |
|
| | 10 | | namespace 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 | |
|
| 0 | 20 | | public InplaceStringBuilder(int capacity) : this() |
| | 21 | | { |
| 0 | 22 | | if (capacity < 0) |
| | 23 | | { |
| 0 | 24 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity); |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | _capacity = capacity; |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public int Capacity |
| | 31 | | { |
| 0 | 32 | | get => _capacity; |
| | 33 | | set |
| | 34 | | { |
| 0 | 35 | | if (value < 0) |
| | 36 | | { |
| 0 | 37 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | // _offset > 0 indicates writing state |
| 0 | 41 | | if (_offset > 0) |
| | 42 | | { |
| 0 | 43 | | ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_CannotChangeAfterWriteStarted) |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | _capacity = value; |
| 0 | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public void Append(string value) |
| | 51 | | { |
| 0 | 52 | | if (value == null) |
| | 53 | | { |
| 0 | 54 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | Append(value, 0, value.Length); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void Append(StringSegment segment) |
| | 61 | | { |
| 0 | 62 | | Append(segment.Buffer, segment.Offset, segment.Length); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 66 | | public unsafe void Append(string value, int offset, int count) |
| | 67 | | { |
| 0 | 68 | | EnsureValueIsInitialized(); |
| | 69 | |
|
| 0 | 70 | | if (value == null |
| 0 | 71 | | || offset < 0 |
| 0 | 72 | | || value.Length - offset < count |
| 0 | 73 | | || Capacity - _offset < count) |
| | 74 | | { |
| 0 | 75 | | ThrowValidationError(value, offset, count); |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | fixed (char* destination = _value) |
| 0 | 79 | | fixed (char* source = value) |
| | 80 | | { |
| 0 | 81 | | Unsafe.CopyBlockUnaligned(destination + _offset, source + offset, (uint)count * 2); |
| 0 | 82 | | _offset += count; |
| | 83 | | } |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 87 | | public unsafe void Append(char c) |
| | 88 | | { |
| 0 | 89 | | EnsureValueIsInitialized(); |
| | 90 | |
|
| 0 | 91 | | if (_offset >= Capacity) |
| | 92 | | { |
| 0 | 93 | | ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, 1, Capacity - _offset); |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | fixed (char* destination = _value) |
| | 97 | | { |
| 0 | 98 | | destination[_offset++] = c; |
| | 99 | | } |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public override string ToString() |
| | 103 | | { |
| 0 | 104 | | if (Capacity != _offset) |
| | 105 | | { |
| 0 | 106 | | ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotUsedEntirely, Capacity, _offset |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | return _value; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | private void EnsureValueIsInitialized() |
| | 113 | | { |
| 0 | 114 | | if (_value == null) |
| | 115 | | { |
| 0 | 116 | | _value = new string('\0', _capacity); |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | private void ThrowValidationError(string value, int offset, int count) |
| | 121 | | { |
| 0 | 122 | | if (value == null) |
| | 123 | | { |
| 0 | 124 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | if (offset < 0 || value.Length - offset < count) |
| | 128 | | { |
| 0 | 129 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset); |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | if (Capacity - _offset < count) |
| | 133 | | { |
| 0 | 134 | | ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, value.Length, Capacity |
| | 135 | | } |
| 0 | 136 | | } |
| | 137 | | } |
| | 138 | | } |