FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Reader/Writer touch-ups by AArnott · Pull Request #1987 · MessagePack-CSharp/MessagePack-CSharp · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cs  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 4 additions & 3 deletions src/MessagePack/BufferWriter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ internal BufferWriter(SequencePool sequencePool, byte[] array)

internal SequencePool.Rental SequenceRental => _rental;

public Span<byte> GetSpan(int sizeHint)
/// <inheritdoc cref="IBufferWriter{T}.GetSpan(int)"/>
public Span<byte> GetSpan(int sizeHint = 0)
{
Ensure(sizeHint);
return this.Span;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref byte GetPointer(int sizeHint)
public ref byte GetPointer(int sizeHint = 0)
{
Ensure(sizeHint);

Expand Down Expand Up @@ -175,7 +176,7 @@ public void Write(ReadOnlySpan<byte> source)
/// </summary>
/// <param name="count">The number of bytes that must be allocated in a single buffer.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Ensure(int count = 1)
public void Ensure(int count = 0)
{
if (_span.Length < count)
{
Expand Down
14 changes: 7 additions & 7 deletions src/MessagePack/MessagePackReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,20 @@ public byte NextCode
public MessagePackReader CreatePeekReader() => this;

/// <summary>
/// Advances the reader to the next MessagePack primitive to be read.
/// Advances the reader to the next MessagePack structure to be read.
/// </summary>
/// <remarks>
/// The entire primitive is skipped, including content of maps or arrays, or any other type with payloads.
/// The entire structure is skipped, including content of maps or arrays, or any other type with payloads.
/// To get the raw MessagePack sequence that was skipped, use <see cref="ReadRaw()"/> instead.
/// </remarks>
public void Skip() => ThrowInsufficientBufferUnless(this.TrySkip());

/// <summary>
/// Advances the reader to the next MessagePack primitive to be read.
/// Advances the reader to the next MessagePack structure to be read.
/// </summary>
/// <returns><see langword="true"/> if the entire structure beginning at the current <see cref="Position"/> is found in the <see cref="Sequence"/>; <see langword="false"/> otherwise.</returns>
/// <remarks>
/// The entire primitive is skipped, including content of maps or arrays, or any other type with payloads.
/// The entire structure is skipped, including content of maps or arrays, or any other type with payloads.
/// To get the raw MessagePack sequence that was skipped, use <see cref="ReadRaw()"/> instead.
/// WARNING: when false is returned, the position of the reader is undefined.
/// </remarks>
Expand Down Expand Up @@ -309,7 +309,7 @@ public int ReadArrayHeader()
{
ThrowInsufficientBufferUnless(this.TryReadArrayHeader(out int count));

// Protect against corrupted or mischievious data that may lead to allocating way too much memory.
// Protect against corrupted or mischievous data that may lead to allocating way too much memory.
// We allow for each primitive to be the minimal 1 byte in size.
// Formatters that know each element is larger can optionally add a stronger check.
ThrowInsufficientBufferUnless(this.reader.Remaining >= count);
Expand Down Expand Up @@ -387,7 +387,7 @@ public int ReadMapHeader()
{
ThrowInsufficientBufferUnless(this.TryReadMapHeader(out int count));

// Protect against corrupted or mischievious data that may lead to allocating way too much memory.
// Protect against corrupted or mischievous data that may lead to allocating way too much memory.
// We allow for each primitive to be the minimal 1 byte in size, and we have a key=value map, so that's 2 bytes.
// Formatters that know each element is larger can optionally add a stronger check.
ThrowInsufficientBufferUnless(this.reader.Remaining >= count * 2);
Expand Down Expand Up @@ -803,7 +803,7 @@ public ExtensionHeader ReadExtensionFormatHeader()
{
ThrowInsufficientBufferUnless(this.TryReadExtensionFormatHeader(out ExtensionHeader header));

// Protect against corrupted or mischievious data that may lead to allocating way too much memory.
// Protect against corrupted or mischievous data that may lead to allocating way too much memory.
ThrowInsufficientBufferUnless(this.reader.Remaining >= header.Length);

return header;
Expand Down
16 changes: 9 additions & 7 deletions src/MessagePack/SequenceReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public SequenceReader(ReadOnlyMemory<T> memory)
/// <summary>
/// Gets a value indicating whether there is no more data in the <see cref="Sequence"/>.
/// </summary>
public bool End => !this.moreData;
public readonly bool End => !this.moreData;

/// <summary>
/// Gets the underlying <see cref="ReadOnlySequence{T}"/> for the reader.
Expand Down Expand Up @@ -141,7 +141,7 @@ public SequencePosition Position
/// <summary>
/// Gets the unread portion of the <see cref="CurrentSpan"/>.
/// </summary>
public ReadOnlySpan<T> UnreadSpan
public readonly ReadOnlySpan<T> UnreadSpan
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.CurrentSpan.Slice(this.CurrentSpanIndex);
Expand Down Expand Up @@ -451,7 +451,7 @@ private void AdvanceToNextSpan(long count)
/// <param name="destination">Destination to copy to.</param>
/// <returns>True if there is enough data to copy to the <paramref name="destination"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryCopyTo(Span<T> destination)
public readonly bool TryCopyTo(Span<T> destination)
{
ReadOnlySpan<T> firstSpan = this.UnreadSpan;
if (firstSpan.Length >= destination.Length)
Expand All @@ -460,12 +460,14 @@ public bool TryCopyTo(Span<T> destination)
return true;
}

return this.TryCopyMultisegment(destination);
return !this.sequence.IsEmpty && this.TryCopyMultisegment(destination);
}

internal bool TryCopyMultisegment(Span<T> destination)
private readonly bool TryCopyMultisegment(Span<T> destination)
{
if (this.Remaining < destination.Length)
long length = this.length < 0 ? this.sequence.Length : this.length;
long remaining = length - this.Consumed;
if (remaining < destination.Length)
{
return false;
}
Expand All @@ -476,7 +478,7 @@ internal bool TryCopyMultisegment(Span<T> destination)
int copied = firstSpan.Length;

SequencePosition next = this.nextPosition;
while (this.Sequence.TryGet(ref next, out ReadOnlyMemory<T> nextSegment, true))
while (this.sequence.TryGet(ref next, out ReadOnlyMemory<T> nextSegment, true))
{
if (nextSegment.Length > 0)
{
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public partial class MessagePackReaderTests
// * FixInt
// ** non-boundary
(3, Encode((ref MessagePackWriter w) => w.WriteByte(3))),
(3, Encode((ref MessagePackWriter w) => w.WriteByte(3))),
(3, Encode((ref MessagePackWriter w) => w.WriteUInt16(3))),
(3, Encode((ref MessagePackWriter w) => w.WriteUInt32(3))),
(3, Encode((ref MessagePackWriter w) => w.WriteUInt64(3))),
Expand Down

Back | FazBrowse Home | New Git URL