Change ReadTag and PeekTag to just use 0 as a return value for "end of stream", rather than using an awkward out parameter.

This simplifies quite a lot of code.

Generated code in next commit.
pull/692/head
Jon Skeet 10 years ago
parent 607940321c
commit ff334a60eb
  1. 3
      csharp/src/Google.Protobuf.Test/CodedInputStreamExtensions.cs
  2. 20
      csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs
  3. 19
      csharp/src/Google.Protobuf.Test/CodedOutputStreamTest.cs
  4. 4
      csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs
  5. 56
      csharp/src/Google.Protobuf/CodedInputStream.cs
  6. 6
      csharp/src/Google.Protobuf/Collections/MapField.cs
  7. 6
      csharp/src/Google.Protobuf/FieldCodec.cs
  8. 4
      src/google/protobuf/compiler/csharp/csharp_message.cc

@ -38,8 +38,7 @@ namespace Google.Protobuf
{ {
public static void AssertNextTag(this CodedInputStream input, uint expectedTag) public static void AssertNextTag(this CodedInputStream input, uint expectedTag)
{ {
uint tag; uint tag = input.ReadTag();
Assert.IsTrue(input.ReadTag(out tag));
Assert.AreEqual(expectedTag, tag); Assert.AreEqual(expectedTag, tag);
} }

@ -279,9 +279,7 @@ namespace Google.Protobuf
ms.Position = 0; ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms); CodedInputStream input = new CodedInputStream(ms);
uint testtag; Assert.AreEqual(tag, input.ReadTag());
Assert.IsTrue(input.ReadTag(out testtag));
Assert.AreEqual(tag, testtag);
// TODO(jonskeet): Should this be ArgumentNullException instead? // TODO(jonskeet): Should this be ArgumentNullException instead?
Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes()); Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes());
@ -377,9 +375,7 @@ namespace Google.Protobuf
CodedInputStream input = new CodedInputStream(ms); CodedInputStream input = new CodedInputStream(ms);
uint actualTag; Assert.AreEqual(tag, input.ReadTag());
Assert.IsTrue(input.ReadTag(out actualTag));
Assert.AreEqual(tag, actualTag);
string text = input.ReadString(); string text = input.ReadString();
Assert.AreEqual('\ufffd', text[0]); Assert.AreEqual('\ufffd', text[0]);
} }
@ -430,15 +426,21 @@ namespace Google.Protobuf
ms.Position = 0; ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2]); CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2]);
uint tag; uint tag = input.ReadTag();
Assert.IsTrue(input.ReadTag(out tag));
Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag)); Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
Assert.AreEqual(100, input.ReadBytes().Length); Assert.AreEqual(100, input.ReadBytes().Length);
Assert.IsTrue(input.ReadTag(out tag)); tag = input.ReadTag();
Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag)); Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
Assert.AreEqual(100, input.ReadBytes().Length); Assert.AreEqual(100, input.ReadBytes().Length);
} }
} }
[Test]
public void Tag0Throws()
{
var input = new CodedInputStream(new byte[] { 0 });
Assert.Throws<InvalidProtocolBufferException>(() => input.ReadTag());
}
} }
} }

@ -335,15 +335,16 @@ namespace Google.Protobuf
// Now test Input stream: // Now test Input stream:
{ {
CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50]); CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50]);
uint tag;
Assert.AreEqual(0, cin.Position); Assert.AreEqual(0, cin.Position);
// Field 1: // Field 1:
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 1); uint tag = cin.ReadTag();
Assert.AreEqual(1, tag >> 3);
Assert.AreEqual(1, cin.Position); Assert.AreEqual(1, cin.Position);
Assert.AreEqual(500, cin.ReadInt32()); Assert.AreEqual(500, cin.ReadInt32());
Assert.AreEqual(3, cin.Position); Assert.AreEqual(3, cin.Position);
//Field 2: //Field 2:
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 2); tag = cin.ReadTag();
Assert.AreEqual(2, tag >> 3);
Assert.AreEqual(4, cin.Position); Assert.AreEqual(4, cin.Position);
int childlen = cin.ReadLength(); int childlen = cin.ReadLength();
Assert.AreEqual(120, childlen); Assert.AreEqual(120, childlen);
@ -353,19 +354,22 @@ namespace Google.Protobuf
// Now we are reading child message // Now we are reading child message
{ {
// Field 11: numeric value: 500 // Field 11: numeric value: 500
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 11); tag = cin.ReadTag();
Assert.AreEqual(11, tag >> 3);
Assert.AreEqual(6, cin.Position); Assert.AreEqual(6, cin.Position);
Assert.AreEqual(500, cin.ReadInt32()); Assert.AreEqual(500, cin.ReadInt32());
Assert.AreEqual(8, cin.Position); Assert.AreEqual(8, cin.Position);
//Field 12: length delimited 120 bytes //Field 12: length delimited 120 bytes
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 12); tag = cin.ReadTag();
Assert.AreEqual(12, tag >> 3);
Assert.AreEqual(9, cin.Position); Assert.AreEqual(9, cin.Position);
ByteString bstr = cin.ReadBytes(); ByteString bstr = cin.ReadBytes();
Assert.AreEqual(110, bstr.Length); Assert.AreEqual(110, bstr.Length);
Assert.AreEqual((byte) 109, bstr[109]); Assert.AreEqual((byte) 109, bstr[109]);
Assert.AreEqual(120, cin.Position); Assert.AreEqual(120, cin.Position);
// Field 13: fixed numeric value: 501 // Field 13: fixed numeric value: 501
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 13); tag = cin.ReadTag();
Assert.AreEqual(13, tag >> 3);
// ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit
Assert.AreEqual(121, cin.Position); Assert.AreEqual(121, cin.Position);
Assert.AreEqual(501, cin.ReadSFixed32()); Assert.AreEqual(501, cin.ReadSFixed32());
@ -375,7 +379,8 @@ namespace Google.Protobuf
cin.PopLimit(oldlimit); cin.PopLimit(oldlimit);
Assert.AreEqual(125, cin.Position); Assert.AreEqual(125, cin.Position);
// Field 3: fixed numeric value: 501 // Field 3: fixed numeric value: 501
Assert.IsTrue(cin.ReadTag(out tag) && tag >> 3 == 3); tag = cin.ReadTag();
Assert.AreEqual(3, tag >> 3);
Assert.AreEqual(126, cin.Position); Assert.AreEqual(126, cin.Position);
Assert.AreEqual(501, cin.ReadSFixed32()); Assert.AreEqual(501, cin.ReadSFixed32());
Assert.AreEqual(130, cin.Position); Assert.AreEqual(130, cin.Position);

@ -455,7 +455,7 @@ namespace Google.Protobuf.Collections
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = new CodedInputStream(bytes); CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag)); tag = input.ReadTag();
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));
@ -493,7 +493,7 @@ namespace Google.Protobuf.Collections
Assert.AreEqual(0, output.SpaceLeft); Assert.AreEqual(0, output.SpaceLeft);
CodedInputStream input = new CodedInputStream(bytes); CodedInputStream input = new CodedInputStream(bytes);
Assert.IsTrue(input.ReadTag(out tag)); tag = input.ReadTag();
RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>(); RepeatedField<SampleEnum> values = new RepeatedField<SampleEnum>();
values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x));

@ -254,37 +254,35 @@ namespace Google.Protobuf
#region Reading of tags etc #region Reading of tags etc
/// <summary> /// <summary>
/// Attempts to peek at the next field tag. /// Peeks at the next field tag. This is like calling <see cref="ReadTag"/>, but the
/// tag is not consumed. (So a subsequent call to <see cref="ReadTag"/> will return the
/// same value.)
/// </summary> /// </summary>
public bool PeekNextTag(out uint fieldTag) public uint PeekTag()
{ {
if (hasNextTag) if (hasNextTag)
{ {
fieldTag = nextTag; return nextTag;
return true;
} }
uint savedLast = lastTag; uint savedLast = lastTag;
hasNextTag = ReadTag(out nextTag); nextTag = ReadTag();
lastTag = savedLast; hasNextTag = true;
fieldTag = nextTag; lastTag = savedLast; // Undo the side effect of ReadTag
return hasNextTag; return nextTag;
} }
/// <summary> /// <summary>
/// Attempts to read a field tag, returning false if we have reached the end /// Reads a field tag, returning the tag of 0 for "end of stream".
/// of the input data.
/// </summary> /// </summary>
/// <param name="fieldTag">The 'tag' of the field (id * 8 + wire-format)</param> /// <returns>The next field tag, or 0 for end of stream. (0 is never a valid tag.)</returns>
/// <returns>true if the next fieldTag was read</returns> public uint ReadTag()
public bool ReadTag(out uint fieldTag)
{ {
if (hasNextTag) if (hasNextTag)
{ {
fieldTag = nextTag; lastTag = nextTag;
lastTag = fieldTag;
hasNextTag = false; hasNextTag = false;
return true; return lastTag;
} }
// Optimize for the incredibly common case of having at least two bytes left in the buffer, // Optimize for the incredibly common case of having at least two bytes left in the buffer,
@ -294,7 +292,7 @@ namespace Google.Protobuf
int tmp = buffer[bufferPos++]; int tmp = buffer[bufferPos++];
if (tmp < 128) if (tmp < 128)
{ {
fieldTag = (uint)tmp; lastTag = (uint)tmp;
} }
else else
{ {
@ -302,13 +300,13 @@ namespace Google.Protobuf
if ((tmp = buffer[bufferPos++]) < 128) if ((tmp = buffer[bufferPos++]) < 128)
{ {
result |= tmp << 7; result |= tmp << 7;
fieldTag = (uint) result; lastTag = (uint) result;
} }
else else
{ {
// Nope, rewind and go the potentially slow route. // Nope, rewind and go the potentially slow route.
bufferPos -= 2; bufferPos -= 2;
fieldTag = ReadRawVarint32(); lastTag = ReadRawVarint32();
} }
} }
} }
@ -316,20 +314,18 @@ namespace Google.Protobuf
{ {
if (IsAtEnd) if (IsAtEnd)
{ {
fieldTag = 0; lastTag = 0;
lastTag = fieldTag; return 0; // This is the only case in which we return 0.
return false;
} }
fieldTag = ReadRawVarint32(); lastTag = ReadRawVarint32();
} }
lastTag = fieldTag;
if (lastTag == 0) if (lastTag == 0)
{ {
// If we actually read zero, that's not a valid tag. // If we actually read zero, that's not a valid tag.
throw InvalidProtocolBufferException.InvalidTag(); throw InvalidProtocolBufferException.InvalidTag();
} }
return true; return lastTag;
} }
/// <summary> /// <summary>
@ -580,14 +576,10 @@ namespace Google.Protobuf
/// </summary> /// </summary>
public bool MaybeConsumeTag(uint tag) public bool MaybeConsumeTag(uint tag)
{ {
uint next; if (PeekTag() == tag)
if (PeekNextTag(out next))
{ {
if (next == tag) hasNextTag = false;
{ return true;
hasNextTag = false;
return true;
}
} }
return false; return false;
} }

@ -627,12 +627,8 @@ namespace Google.Protobuf.Collections
public void MergeFrom(CodedInputStream input) public void MergeFrom(CodedInputStream input)
{ {
uint tag; uint tag;
while (input.ReadTag(out tag)) while ((tag = input.ReadTag()) != 0)
{ {
if (tag == 0)
{
throw InvalidProtocolBufferException.InvalidTag();
}
if (tag == codec.keyCodec.Tag) if (tag == codec.keyCodec.Tag)
{ {
Key = codec.keyCodec.Read(input); Key = codec.keyCodec.Read(input);

@ -298,12 +298,8 @@ namespace Google.Protobuf
uint tag; uint tag;
T value = codec.DefaultValue; T value = codec.DefaultValue;
while (input.ReadTag(out tag)) while ((tag = input.ReadTag()) != 0)
{ {
if (tag == 0)
{
throw InvalidProtocolBufferException.InvalidTag();
}
if (tag == codec.Tag) if (tag == codec.Tag)
{ {
value = codec.Read(input); value = codec.Read(input);

@ -417,13 +417,11 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) {
printer->Indent(); printer->Indent();
printer->Print( printer->Print(
"uint tag;\n" "uint tag;\n"
"while (input.ReadTag(out tag)) {\n" "while ((tag = input.ReadTag()) != 0) {\n"
" switch(tag) {\n"); " switch(tag) {\n");
printer->Indent(); printer->Indent();
printer->Indent(); printer->Indent();
printer->Print( printer->Print(
"case 0:\n" // 0 signals EOF / limit reached
" throw pb::InvalidProtocolBufferException.InvalidTag();\n"
"default:\n" "default:\n"
" if (pb::WireFormat.IsEndGroupTag(tag)) {\n" " if (pb::WireFormat.IsEndGroupTag(tag)) {\n"
" return;\n" " return;\n"

Loading…
Cancel
Save