| | | 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 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | namespace System.Buffers |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for <see cref="ReadOnlySequence{T}"/> |
| | | 9 | | /// </summary> |
| | | 10 | | public static class BuffersExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Returns position of first occurrence of item in the <see cref="ReadOnlySequence{T}"/> |
| | | 14 | | /// </summary> |
| | | 15 | | public static SequencePosition? PositionOf<T>(in this ReadOnlySequence<T> sequence, T value) where T : IEquatabl |
| | 1 | 16 | | { |
| | 1 | 17 | | if (sequence.IsSingleSegment) |
| | 1 | 18 | | { |
| | 1 | 19 | | int index = sequence.First.Span.IndexOf(value); |
| | 1 | 20 | | if (index != -1) |
| | 1 | 21 | | { |
| | 1 | 22 | | return sequence.GetPosition(index); |
| | | 23 | | } |
| | | 24 | | |
| | 1 | 25 | | return null; |
| | | 26 | | } |
| | | 27 | | else |
| | 1 | 28 | | { |
| | 1 | 29 | | return PositionOfMultiSegement(sequence, value); |
| | | 30 | | } |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | private static SequencePosition? PositionOfMultiSegement<T>(in ReadOnlySequence<T> sequence, T value) where T : |
| | 1 | 34 | | { |
| | 1 | 35 | | SequencePosition position = sequence.Start; |
| | 1 | 36 | | SequencePosition result = position; |
| | 1 | 37 | | while (sequence.TryGet(ref position, out ReadOnlyMemory<T> memory)) |
| | 1 | 38 | | { |
| | 1 | 39 | | int index = memory.Span.IndexOf(value); |
| | 1 | 40 | | if (index != -1) |
| | 1 | 41 | | { |
| | 1 | 42 | | return sequence.GetPosition(index, result); |
| | | 43 | | } |
| | 1 | 44 | | else if (position.GetObject() == null) |
| | 1 | 45 | | { |
| | 1 | 46 | | break; |
| | | 47 | | } |
| | | 48 | | |
| | 1 | 49 | | result = position; |
| | 1 | 50 | | } |
| | | 51 | | |
| | 1 | 52 | | return null; |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Copy the <see cref="ReadOnlySequence{T}"/> to the specified <see cref="Span{Byte}"/>. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="sequence">The source <see cref="ReadOnlySequence{T}"/>.</param> |
| | | 59 | | /// <param name="destination">The destination <see cref="Span{Byte}"/>.</param> |
| | | 60 | | public static void CopyTo<T>(in this ReadOnlySequence<T> sequence, Span<T> destination) |
| | 1 | 61 | | { |
| | 1 | 62 | | if (sequence.Length > destination.Length) |
| | 1 | 63 | | ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination); |
| | | 64 | | |
| | 1 | 65 | | if (sequence.IsSingleSegment) |
| | 1 | 66 | | { |
| | 1 | 67 | | sequence.First.Span.CopyTo(destination); |
| | 1 | 68 | | } |
| | | 69 | | else |
| | 1 | 70 | | { |
| | 1 | 71 | | CopyToMultiSegement(sequence, destination); |
| | 1 | 72 | | } |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | private static void CopyToMultiSegement<T>(in ReadOnlySequence<T> sequence, Span<T> destination) |
| | 1 | 76 | | { |
| | 1 | 77 | | SequencePosition position = sequence.Start; |
| | 1 | 78 | | while (sequence.TryGet(ref position, out ReadOnlyMemory<T> memory)) |
| | 1 | 79 | | { |
| | 1 | 80 | | ReadOnlySpan<T> span = memory.Span; |
| | 1 | 81 | | span.CopyTo(destination); |
| | 1 | 82 | | if (position.GetObject() != null) |
| | 1 | 83 | | { |
| | 1 | 84 | | destination = destination.Slice(span.Length); |
| | 1 | 85 | | } |
| | | 86 | | else |
| | 1 | 87 | | { |
| | 1 | 88 | | break; |
| | | 89 | | } |
| | 1 | 90 | | } |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Converts the <see cref="ReadOnlySequence{T}"/> to an array |
| | | 95 | | /// </summary> |
| | | 96 | | public static T[] ToArray<T>(in this ReadOnlySequence<T> sequence) |
| | 1 | 97 | | { |
| | 1 | 98 | | var array = new T[sequence.Length]; |
| | 1 | 99 | | sequence.CopyTo(array); |
| | 1 | 100 | | return array; |
| | 1 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Writes contents of <paramref name="source"/> to <paramref name="bufferWriter"/> |
| | | 105 | | /// </summary> |
| | | 106 | | public static void Write<T>(this IBufferWriter<T> bufferWriter, ReadOnlySpan<T> source) |
| | 0 | 107 | | { |
| | 0 | 108 | | Span<T> destination = bufferWriter.GetSpan(); |
| | | 109 | | |
| | | 110 | | // Fast path, try copying to the available memory directly |
| | 0 | 111 | | if (source.Length <= destination.Length) |
| | 0 | 112 | | { |
| | 0 | 113 | | source.CopyTo(destination); |
| | 0 | 114 | | bufferWriter.Advance(source.Length); |
| | 0 | 115 | | } |
| | | 116 | | else |
| | 0 | 117 | | { |
| | 0 | 118 | | WriteMultiSegment(bufferWriter, source, destination); |
| | 0 | 119 | | } |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private static void WriteMultiSegment<T>(IBufferWriter<T> bufferWriter, in ReadOnlySpan<T> source, Span<T> desti |
| | 0 | 123 | | { |
| | 0 | 124 | | ReadOnlySpan<T> input = source; |
| | 0 | 125 | | while (true) |
| | 0 | 126 | | { |
| | 0 | 127 | | int writeSize = Math.Min(destination.Length, input.Length); |
| | 0 | 128 | | input.Slice(0, writeSize).CopyTo(destination); |
| | 0 | 129 | | bufferWriter.Advance(writeSize); |
| | 0 | 130 | | input = input.Slice(writeSize); |
| | 0 | 131 | | if (input.Length > 0) |
| | 0 | 132 | | { |
| | 0 | 133 | | destination = bufferWriter.GetSpan(input.Length); |
| | 0 | 134 | | continue; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return; |
| | | 138 | | } |
| | 0 | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |