public void Empty_Enumerator() { ReadOnlySequence <byte> buffer = ReadOnlySequence <byte> .Empty; ReadOnlySequence <byte> .Enumerator enumerator = buffer.GetEnumerator(); { Assert.Equal(default, enumerator.Current);
public ReadOnlySequenceWrapper(ReadOnlySequence <byte> data) { _data = data; _currentOffset = 0; _totalOffset = 0; _enumerator = data.GetEnumerator(); _enumerator.MoveNext(); }
public void MemoryEnumerator() { for (int i = 0; i < InnerLoopCount; i++) { var enumerator = _readOnlyBuffer.GetEnumerator(); while (enumerator.MoveNext()) { var memory = enumerator.Current; } } }
private static int GetNumberOfSegments(ReadOnlySequence <byte> sequence) { var numberOfSegments = 0; var enumerator = sequence.GetEnumerator(); while (enumerator.MoveNext()) { ++numberOfSegments; } return(numberOfSegments); }
public BufferReader(ReadOnlySequence <byte> buffer) { _buffer = buffer; _lastSnapshotPosition = buffer.Start; _lastSnapshotBytes = 0; _iterator = buffer.GetEnumerator(); _current = default; _totalConsumed = OffsetThisSpan = RemainingThisSpan = 0; FetchNextSegment(); }
public long Seek(long offset, IntPtr whence, IntPtr user_data) { var newOffset = GetNewOffset(offset, whence); if (newOffset < 0) { return(-1); } var steps = newOffset - _totalOffset; if (steps > 0 && _currentOffset + steps < _enumerator.Current.Span.Length) { _currentOffset += steps; _totalOffset = newOffset; } else { _totalOffset = -1; _currentOffset = 0; var remaining = newOffset; try { _enumerator = _data.GetEnumerator(); while (_enumerator.MoveNext()) { var current = _enumerator.Current.Span; if (remaining < current.Length) { _currentOffset = remaining; _totalOffset = newOffset; break; } remaining -= current.Length; } } catch { return(_totalOffset); } if (remaining == 0) { _totalOffset = newOffset; } } return(_totalOffset); }
public static IEnumerable <T> AsEnumerable <T>(this ReadOnlySequence <T> source) { var enumerator = source.GetEnumerator(); while (enumerator.MoveNext()) { var items = enumerator.Current.ToArray(); foreach (var item in items) { yield return(item); } } }
private static async ValueTask <FlushResult> WriteBigBufferAsync(this PipeWriter writer, ReadOnlySequence <byte> sequence, CancellationToken token = default) { var enumerator = sequence.GetEnumerator(); FlushResult lastResult = default; while (enumerator.MoveNext() && !token.IsCancellationRequested) { lastResult = await writer.WriteBigBufferAsync(enumerator.Current, token) .ConfigureAwait(false); } return(lastResult); }
public async ValueTask Send(ReadOnlySequence <byte> sequence) { var sequenceLength = sequence.Length; if (sequenceLength <= _chunkSize) { CopyToBuffer(sequence); _bufferCount = (int)sequenceLength; await SendBuffer().ConfigureAwait(false); return; } var enumerator = sequence.GetEnumerator(); var hasNext = true; while (hasNext) { var current = enumerator.Current; var currentLength = current.Length; hasNext = enumerator.MoveNext(); if (currentLength > _chunkSize) { await Send(current).ConfigureAwait(false); continue; } var total = currentLength + _bufferCount; if (total > _chunkSize) { await SendBuffer().ConfigureAwait(false); } if (_bufferCount != 0 || (hasNext && enumerator.Current.Length + total <= _chunkSize)) { CopyToBuffer(current); _bufferCount = total; continue; } await Send(current).ConfigureAwait(false); } await SendBuffer().ConfigureAwait(false); }
public static void Initialize(ReadOnlySequence <byte> sequence, out SegmentedBufferHelper instance, out ReadOnlySpan <byte> firstSpan) { instance.codedInputStream = null; if (sequence.IsSingleSegment) { firstSpan = sequence.First.Span; instance.totalLength = firstSpan.Length; instance.readOnlySequenceEnumerator = default; } else { instance.readOnlySequenceEnumerator = sequence.GetEnumerator(); instance.totalLength = (int)sequence.Length; // set firstSpan to the first segment instance.readOnlySequenceEnumerator.MoveNext(); firstSpan = instance.readOnlySequenceEnumerator.Current.Span; } }
/// <summary> /// Run down both sequences as long as the bytes are equal. /// If we've run out of a bytes, return -1, a is less than b. /// If we've run out of b bytes, return 1, a is greater than b. /// If both are simultaneously out, they are equal, return 0. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static int CompareTo(this ReadOnlySequence <byte> a, ReadOnlySequence <byte> b) { var ac = a.Length; var bc = b.Length; var ae = a.GetEnumerator(); var be = b.GetEnumerator(); var aok = ae.MoveNext(); var bok = be.MoveNext(); var ai = -1; var bi = -1; var aspan = ReadOnlySpan <byte> .Empty; var bspan = ReadOnlySpan <byte> .Empty; while (aok && bok) { if (ai == -1) { aspan = ae.Current.Span; ai = 0; } if (bi == -1) { bspan = be.Current.Span; bi = 0; } if (ai >= aspan.Length) { ai = -1; aok = ae.MoveNext(); } if (bi >= bspan.Length) { bi = -1; bok = ae.MoveNext(); } if (ai == -1 || bi == -1) { continue; } if (aspan[ai++] != bspan[bi++]) { break; } } return(aok ? 1 : bok ? -1 : 0); }
//public static int ReadInt32(this Span<byte> bytes) //{ // return 33; //} // duplicating code from ByteArrayExtensions, we can do better! // works but slow public static int ReadInt32(this ReadOnlySequence <byte> buffer) { var value = 0; var bufferEnumerator = buffer.GetEnumerator(); var byteCount = 0; while (byteCount < 4) { bufferEnumerator.MoveNext(); var m = bufferEnumerator.Current; var spanIndex = 0; var spanLength = m.Span.Length; while (spanIndex < spanLength && byteCount < 4) { value <<= 8; value |= m.Span[spanIndex++]; byteCount++; } } return(value); }
internal void Init(out State state, ReadOnlySequence <byte> source, TypeModel model, SerializationContext context) { base.Init(model, context); _source = source.GetEnumerator(); state = default; }
internal ReadOnlyCharSequenceAdapter(ReadOnlySequence <char> sequence) { Enumerator = sequence.GetEnumerator(); CurrentSegment = ReadOnlyMemory <char> .Empty; IsComplete = false; }