Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Allow message parsing from an array slice #3858

Merged
merged 3 commits into from Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions csharp/src/Google.Protobuf/MessageExtensions.cs
Expand Up @@ -53,6 +53,22 @@ public static void MergeFrom(this IMessage message, byte[] data)
input.CheckReadEndOfStreamTag();
}

/// <summary>
/// Merges data from the given byte array slice into an existing message.
/// </summary>
/// <param name="message">The message to merge the data into.</param>
/// <param name="data">The data containing the slice to merge, which must be protobuf-encoded binary data.</param>
/// <param name="offset">The offset of the slice to merge.</param>
/// <param name="length">The length of the slice to merge.</param>
public static void MergeFrom(this IMessage message, byte[] data, int offset, int length)
{
ProtoPreconditions.CheckNotNull(message, "message");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point I should do a nameof tidy-up... but I understand the point of sticking to the convention of the rest of the file here.

ProtoPreconditions.CheckNotNull(data, "data");
CodedInputStream input = new CodedInputStream(data, offset, length);
message.MergeFrom(input);
input.CheckReadEndOfStreamTag();
}

/// <summary>
/// Merges data from the given byte string into an existing message.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions csharp/src/Google.Protobuf/MessageParser.cs
Expand Up @@ -70,6 +70,21 @@ public IMessage ParseFrom(byte[] data)
return message;
}

/// <summary>
/// Parses a message from a byte array slice.
/// </summary>
/// <param name="data">The byte array containing the message. Must not be null.</param>
/// <param name="offset">The offset of the slice to parse.</param>
/// <param name="length">The length of the slice to parse.</param>
/// <returns>The newly parsed message.</returns>
public IMessage ParseFrom(byte[] data, int offset, int length)
{
ProtoPreconditions.CheckNotNull(data, "data");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd ditch this given that it'll be validated in the call to message.MergeFrom.

(If you change the parameter names to buffer in both cases, you could rely on the CodedInputStream constructor validation, but I prefer data instead of buffer here...)

IMessage message = factory();
message.MergeFrom(data, offset, length);
return message;
}

/// <summary>
/// Parses a message from the given byte string.
/// </summary>
Expand Down