public JsonRpcMessage Deserialize(System.Buffers.ReadOnlySequence <byte> contentBuffer) { throw new NotImplementedException(); }
/// <summary>Initialize the enumerator.</summary> /// <param name="sequence">The <see cref="ReadOnlySequence{T}"/> to enumerate.</param> public Enumerator(ReadOnlySequence <T> sequence) { _sequence = sequence; _currentMemory = default; _next = sequence.Start; }
/// <summary> /// Try to read data until the given <paramref name="delimiter"/> sequence. /// </summary> /// <param name="sequence">The read data, if any.</param> /// <param name="delimiter">The multi (T) delimiter.</param> /// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> sequence if found.</param> /// <returns>True if the <paramref name="delimiter"/> was found.</returns> public unsafe bool TryReadTo(out ReadOnlySequence <T> sequence, ReadOnlySpan <T> delimiter, bool advancePastDelimiter = true) { if (delimiter.Length == 0) { sequence = default; return(true); } BufferReader <T> copy = this; Span <T> peekBuffer; if (delimiter.Length * sizeof(T) < 512) { T *t = stackalloc T[delimiter.Length]; peekBuffer = new Span <T>(t, delimiter.Length); } else { peekBuffer = new Span <T>(new T[delimiter.Length]); } bool advanced = false; while (!End) { if (!TryReadTo(out sequence, delimiter[0], advancePastDelimiter: false)) { this = copy; return(false); } if (delimiter.Length == 1) { return(true); } ReadOnlySpan <T> next = Peek(peekBuffer); if (next.SequenceEqual(delimiter)) { //TODO: Figure out a faster way to do this, potentially by avoiding the Advance in the previous TryReadTo call if (advanced) { sequence = copy.Sequence.Slice(copy.Consumed, Consumed - copy.Consumed); } if (advancePastDelimiter) { Advance(delimiter.Length); } return(true); } else { Advance(1); advanced = true; } } this = copy; sequence = default; return(false); }
/// <summary> /// Try to read everything up to the given <paramref name="delimiters"/>. /// </summary> /// <param name="sequence">The read data, if any.</param> /// <param name="delimiters">The delimiters to look for.</param> /// <param name="advancePastDelimiter">True to move past the first found instance of any of the given <paramref name="delimiters"/>.</param> /// <returns>True if any of the the <paramref name="delimiters"/> were found.</returns> public bool TryReadToAny(out ReadOnlySequence <T> sequence, ReadOnlySpan <T> delimiters, bool advancePastDelimiter = true) { return(TryReadToAnyInternal(out sequence, delimiters, advancePastDelimiter)); }
/// <summary> /// Try to read everything up to the given <paramref name="delimiter"/>, ignoring delimiters that are /// preceeded by <paramref name="delimiterEscape"/>. /// </summary> /// <param name="sequence">The read data, if any.</param> /// <param name="delimiter">The delimiter to look for.</param> /// <param name="delimiterEscape">If found prior to <paramref name="delimiter"/> it will skip that occurence.</param> /// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param> /// <returns>True if the <paramref name="delimiter"/> was found.</returns> public bool TryReadTo(out ReadOnlySequence <T> sequence, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { BufferReader <T> copy = this; ReadOnlySpan <T> remaining = UnreadSpan; bool priorEscape = false; while (!End) { int index = remaining.IndexOf(delimiter); if (index != -1) { if (index == 0 && priorEscape) { // We were in the escaped state, so skip this delimiter priorEscape = false; Advance(index + 1); remaining = UnreadSpan; continue; } else if (index > 0 && remaining[index - 1].Equals(delimiterEscape)) { // This delimiter might be skipped // Count our escapes int escapeCount = 0; for (int i = index; i > 0 && remaining[i - 1].Equals(delimiterEscape); i--, escapeCount++) { ; } if (escapeCount == index && priorEscape) { // Started and ended with escape, increment once more escapeCount++; } priorEscape = false; if (escapeCount % 2 != 0) { // We're in the escaped state, so skip this delimiter Advance(index + 1); remaining = UnreadSpan; continue; } } // Found the delimiter. Move to it, slice, then move past it. if (index > 0) { Advance(index); } sequence = Sequence.Slice(copy.Position, Position); if (advancePastDelimiter) { Advance(1); } return(true); } // No delimiter, need to check the end of the span for odd number of escapes then advance { int escapeCount = 0; for (int i = remaining.Length; i > 0 && remaining[i - 1].Equals(delimiterEscape); i--, escapeCount++) { ; } if (priorEscape && escapeCount == remaining.Length) { escapeCount++; } priorEscape = escapeCount % 2 != 0; } // Nothing in the current span, move to the end, checking for the skip delimiter Advance(remaining.Length); remaining = CurrentSpan; } // Didn't find anything, reset our original state. this = copy; sequence = default; return(false); }
/// <summary> /// Try to read everything up to the given <paramref name="delimiter"/>. /// </summary> /// <param name="sequence">The read data, if any.</param> /// <param name="delimiter">The delimiter to look for.</param> /// <param name="advancePastDelimiter">True to move past the <paramref name="delimiter"/> if found.</param> /// <returns>True if the <paramref name="delimiter"/> was found.</returns> public bool TryReadTo(out ReadOnlySequence <T> sequence, T delimiter, bool advancePastDelimiter = true) { return(TryReadToInternal(out sequence, delimiter, advancePastDelimiter)); }
private bool TryReadToSlow(out ReadOnlySequence <T> sequence, T delimiter, T delimiterEscape, int index, bool advancePastDelimiter) { BufferReader <T> copy = this; ReadOnlySpan <T> remaining = UnreadSpan; bool priorEscape = false; do { if (index >= 0) { if (index == 0 && priorEscape) { // We were in the escaped state, so skip this delimiter priorEscape = false; Advance(index + 1); remaining = UnreadSpan; goto Continue; } else if (index > 0 && remaining[index - 1].Equals(delimiterEscape)) { // This delimiter might be skipped // Count our escapes int escapeCount = 1; int i = index - 2; for (; i >= 0; i--) { if (!remaining[i].Equals(delimiterEscape)) { break; } } if (i < 0 && priorEscape) { // Started and ended with escape, increment once more escapeCount++; } escapeCount += index - 2 - i; if ((escapeCount & 1) != 0) { priorEscape = false; // We're in the escaped state, so skip this delimiter Advance(index + 1); remaining = UnreadSpan; goto Continue; } } // Found the delimiter. Move to it, slice, then move past it. Advance(index); sequence = Sequence.Slice(copy.Position, Position); if (advancePastDelimiter) { Advance(1); } return(true); } else { // No delimiter, need to check the end of the span for odd number of escapes then advance if (remaining.Length > 0 && remaining[remaining.Length - 1].Equals(delimiterEscape)) { int escapeCount = 1; int i = remaining.Length - 2; for (; i >= 0; i--) { if (!remaining[i].Equals(delimiterEscape)) { break; } } escapeCount += remaining.Length - 2 - i; if (i < 0 && priorEscape) { priorEscape = (escapeCount & 1) == 0; // equivalent to incrementing escapeCount before setting priorEscape } else { priorEscape = (escapeCount & 1) != 0; } } else { priorEscape = false; } } // Nothing in the current span, move to the end, checking for the skip delimiter Advance(remaining.Length); remaining = CurrentSpan; Continue: index = remaining.IndexOf(delimiter); } while (!End); // Didn't find anything, reset our original state. this = copy; sequence = default; return(false); }