aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/ProtocolBuffers/CodedInputStream.cs
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2015-06-26 17:37:14 +0100
committerGravatar Jon Skeet <jonskeet@google.com>2015-06-30 13:20:30 +0100
commitf2a27cc2c71b4dae3ff230574a73c1de88dd61b7 (patch)
tree58cdbbbd9262732c9a104171f2563f0f2da85acb /csharp/src/ProtocolBuffers/CodedInputStream.cs
parent241e17ba78b71a7ecccb289914ecaeab203b2373 (diff)
First pass (not yet compiling) at removing all the array handling code from Coded*Stream.
Prod code works, but some tests are broken. Obviously those need fixing, then more tests, and review benchmarks.
Diffstat (limited to 'csharp/src/ProtocolBuffers/CodedInputStream.cs')
-rw-r--r--csharp/src/ProtocolBuffers/CodedInputStream.cs288
1 files changed, 6 insertions, 282 deletions
diff --git a/csharp/src/ProtocolBuffers/CodedInputStream.cs b/csharp/src/ProtocolBuffers/CodedInputStream.cs
index dab3e5e3..75178d14 100644
--- a/csharp/src/ProtocolBuffers/CodedInputStream.cs
+++ b/csharp/src/ProtocolBuffers/CodedInputStream.cs
@@ -172,6 +172,12 @@ namespace Google.Protobuf
}
}
+ /// <summary>
+ /// Returns the last tag read, or 0 if no tags have been read or we've read beyond
+ /// the end of the stream.
+ /// </summary>
+ internal uint LastTag { get { return lastTag; } }
+
#region Validation
/// <summary>
@@ -435,26 +441,6 @@ namespace Google.Protobuf
return DecodeZigZag64(ReadRawVarint64());
}
- private bool BeginArray(uint fieldTag, out bool isPacked, out int oldLimit)
- {
- isPacked = WireFormat.GetTagWireType(fieldTag) == WireFormat.WireType.LengthDelimited;
-
- if (isPacked)
- {
- int length = (int) (ReadRawVarint32() & int.MaxValue);
- if (length > 0)
- {
- oldLimit = PushLimit(length);
- return true;
- }
- oldLimit = -1;
- return false; //packed but empty
- }
-
- oldLimit = -1;
- return true;
- }
-
/// <summary>
/// Peeks at the next tag in the stream. If it matches <paramref name="tag"/>,
/// the tag is consumed and the method returns <c>true</c>; otherwise, the
@@ -474,268 +460,6 @@ namespace Google.Protobuf
return false;
}
- /// <summary>
- /// Returns true if the next tag is also part of the same array, which may or may not be packed.
- /// </summary>
- private bool ContinueArray(uint currentTag, bool packed, int oldLimit)
- {
- if (packed)
- {
- if (ReachedLimit)
- {
- PopLimit(oldLimit);
- return false;
- }
- return true;
- }
- return MaybeConsumeTag(currentTag);
- }
-
- /// <summary>
- /// Reads a string array.
- /// </summary>
- /// <remarks>The stream is assumed to be positioned after a tag indicating the field
- /// repeated string value. A string is read, and then if the next tag is the same,
- /// the process is repeated, until the next tag is a different one.</remarks>
- /// <param name="list"></param>
- public void ReadStringArray(ICollection<string> list)
- {
- uint fieldTag = lastTag;
- do
- {
- list.Add(ReadString());
- } while (MaybeConsumeTag(fieldTag));
- }
-
- public void ReadBytesArray(ICollection<ByteString> list)
- {
- uint fieldTag = lastTag;
- do
- {
- list.Add(ReadBytes());
- } while (MaybeConsumeTag(fieldTag));
- }
-
- public void ReadBoolArray(ICollection<bool> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadBool());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadInt32Array(ICollection<int> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadInt32());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadSInt32Array(ICollection<int> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadSInt32());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadUInt32Array(ICollection<uint> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadUInt32());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadFixed32Array(ICollection<uint> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadFixed32());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadSFixed32Array(ICollection<int> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadSFixed32());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadInt64Array(ICollection<long> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadInt64());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadSInt64Array(ICollection<long> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadSInt64());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadUInt64Array(ICollection<ulong> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadUInt64());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadFixed64Array(ICollection<ulong> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadFixed64());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadSFixed64Array(ICollection<long> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadSFixed64());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadDoubleArray(ICollection<double> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadDouble());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadFloatArray(ICollection<float> list)
- {
- uint fieldTag = lastTag;
- bool isPacked;
- int holdLimit;
- if (BeginArray(fieldTag, out isPacked, out holdLimit))
- {
- do
- {
- list.Add(ReadFloat());
- } while (ContinueArray(fieldTag, isPacked, holdLimit));
- }
- }
-
- public void ReadEnumArray<T>(RepeatedField<T> list)
- where T : struct, IComparable, IFormattable
- {
- uint fieldTag = lastTag;
- WireFormat.WireType wformat = WireFormat.GetTagWireType(fieldTag);
-
- // 2.3 allows packed form even if the field is not declared packed.
- if (wformat == WireFormat.WireType.LengthDelimited)
- {
- int length = (int) (ReadRawVarint32() & int.MaxValue);
- int limit = PushLimit(length);
- while (!ReachedLimit)
- {
- // Ghastly hack, but it works...
- list.AddInt32(ReadEnum());
- }
- PopLimit(limit);
- }
- else
- {
- do
- {
- list.Add((T)(object) ReadEnum());
- } while (MaybeConsumeTag(fieldTag));
- }
- }
-
- public void ReadMessageArray<T>(ICollection<T> list, MessageParser<T> messageParser)
- where T : IMessage<T>
- {
- uint fieldTag = lastTag;
- do
- {
- T message = messageParser.CreateTemplate();
- ReadMessage(message);
- list.Add(message);
- } while (MaybeConsumeTag(fieldTag));
- }
#endregion
#region Underlying reading primitives