|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Diagnostics; |
| 4 | + |
| 5 | +namespace StackExchange.Redis; |
| 6 | + |
| 7 | +internal static class ReadOnlySequenceExtensions |
| 8 | +{ |
| 9 | + public static bool StartsWith(this in ReadOnlySequence<byte> sequence, in ReadOnlySequence<byte> value) |
| 10 | + { |
| 11 | + throw new NotImplementedException(); |
| 12 | + } |
| 13 | + |
| 14 | + public static bool StartsWith(this in ReadOnlySequence<byte> sequence, ReadOnlySpan<byte> value) |
| 15 | + => StartsWith(sequence, value, sequence.Start); |
| 16 | + |
| 17 | + public static bool StartsWith(this in ReadOnlySequence<byte> sequence, ReadOnlySpan<byte> value, SequencePosition start) |
| 18 | + { |
| 19 | + var valueLength = value.Length; |
| 20 | + int valueLengthPart = 0; |
| 21 | + while (sequence.TryGet(ref start, out var memory)) |
| 22 | + { |
| 23 | + var spanLength = memory.Length; |
| 24 | + if (spanLength == 0) continue; |
| 25 | + |
| 26 | + var span = memory.Span; |
| 27 | + if (valueLengthPart > 0) |
| 28 | + { |
| 29 | + Debug.Assert(valueLength > valueLengthPart); |
| 30 | + |
| 31 | + var remainder = valueLength - valueLengthPart; |
| 32 | + if (remainder > spanLength) |
| 33 | + { |
| 34 | + if (span.SequenceEqual(value.Slice(valueLengthPart, spanLength))) |
| 35 | + { |
| 36 | + valueLengthPart += spanLength; |
| 37 | + continue; |
| 38 | + } |
| 39 | + } |
| 40 | + else if (remainder == spanLength) |
| 41 | + { |
| 42 | + if (span.SequenceEqual(value.Slice(valueLengthPart))) |
| 43 | + { |
| 44 | + return true; |
| 45 | + } |
| 46 | + } |
| 47 | + else if (span.StartsWith(value.Slice(valueLengthPart))) |
| 48 | + { |
| 49 | + return true; |
| 50 | + } |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + if (spanLength >= valueLength) |
| 55 | + { |
| 56 | + return span.StartsWith(value); |
| 57 | + } |
| 58 | + |
| 59 | + if (!value.Slice(0, spanLength).SequenceEqual(span)) |
| 60 | + return false; |
| 61 | + |
| 62 | + valueLengthPart = spanLength; |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + public static bool SequenceEqual(this in ReadOnlySequence<byte> first, ReadOnlySpan<byte> other) |
| 69 | + { |
| 70 | + if (first.IsSingleSegment) return first.First.Span.SequenceEqual(other); |
| 71 | + if (first.Length != other.Length) return false; |
| 72 | + |
| 73 | + var position = first.Start; |
| 74 | + while (first.TryGet(ref position, out var memory)) |
| 75 | + { |
| 76 | + var span = memory.Span; |
| 77 | + |
| 78 | + if (!span.SequenceEqual(other.Slice(0, span.Length))) return false; |
| 79 | + |
| 80 | + other = other.Slice(span.Length); |
| 81 | + } |
| 82 | + |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + public static bool SequenceEqual(this in ReadOnlySequence<byte> first, in ReadOnlySequence<byte> other) |
| 87 | + { |
| 88 | + if (first.IsSingleSegment) return other.SequenceEqual(first.First.Span); |
| 89 | + if (other.IsSingleSegment) return first.SequenceEqual(other.First.Span); |
| 90 | + if (first.Length != other.Length) return false; |
| 91 | + |
| 92 | + var firstPosition = first.Start; |
| 93 | + var otherPosition = other.Start; |
| 94 | + ReadOnlySpan<byte> firstSpan; |
| 95 | + ReadOnlySpan<byte> otherSpan = default; |
| 96 | + while (first.TryGet(ref firstPosition, out var firstMemory)) |
| 97 | + { |
| 98 | + firstSpan = firstMemory.Span; |
| 99 | + if (firstSpan.Length == 0) continue; |
| 100 | + |
| 101 | + if (otherSpan.Length > 0) |
| 102 | + { |
| 103 | + if (otherSpan.Length >= firstSpan.Length) |
| 104 | + { |
| 105 | + if (!firstSpan.SequenceEqual(otherSpan.Slice(0, firstSpan.Length))) return false; |
| 106 | + otherSpan = otherSpan.Slice(firstSpan.Length); |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + if (!firstSpan.Slice(0, otherSpan.Length).SequenceEqual(otherSpan)) return false; |
| 111 | + firstSpan = firstSpan.Slice(otherSpan.Length); |
| 112 | + } |
| 113 | + |
| 114 | + while (other.TryGet(ref otherPosition, out var otherMemory)) |
| 115 | + { |
| 116 | + otherSpan = otherMemory.Span; |
| 117 | + if (otherSpan.Length == 0) continue; |
| 118 | + |
| 119 | + if (otherSpan.Length >= firstSpan.Length) |
| 120 | + { |
| 121 | + if (!firstSpan.SequenceEqual(otherSpan.Slice(0, firstSpan.Length))) return false; |
| 122 | + otherSpan = otherSpan.Slice(firstSpan.Length); |
| 123 | + break; |
| 124 | + } |
| 125 | + |
| 126 | + if (!firstSpan.Slice(0, otherSpan.Length).SequenceEqual(otherSpan)) return false; |
| 127 | + firstSpan = firstSpan.Slice(otherSpan.Length); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + public static int SequenceCompareTo(this in ReadOnlySequence<byte> first, in ReadOnlySequence<byte> other) |
| 135 | + { |
| 136 | + throw new NotImplementedException(); |
| 137 | + } |
| 138 | +} |
0 commit comments