Skip to content

Commit

Permalink
Merge pull request #1043 from AArnott/v2.2
Browse files Browse the repository at this point in the history
Merge master into v2.2
  • Loading branch information
AArnott committed Sep 14, 2020
2 parents bf2ea7a + 71bdb0e commit d997b6e
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 283 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ using (var ms = new MemoryStream())
using (var ms = new MemoryStream())
{
// serialize empty array.
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = new int[0] });
ProtoBuf.Serializer.Serialize<Parent>(ms, new Parent { Array = System.Array.Empty<int>() });

ms.Position = 0;
var result = ProtoBuf.Serializer.Deserialize<Parent>(ms);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ public T[] Deserialize(ref MessagePackReader reader, MessagePackSerializerOption
{
return default;
}
else

var len = reader.ReadArrayHeader();
if (len == 0)
{
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();
return Array.Empty<T>();
}

var len = reader.ReadArrayHeader();
var array = new T[len];
options.Security.DepthStep(ref reader);
try
{
for (int i = 0; i < array.Length; i++)
{
reader.CancellationToken.ThrowIfCancellationRequested();
array[i] = formatter.Deserialize(ref reader, options);
}
}
finally
IMessagePackFormatter<T> formatter = options.Resolver.GetFormatterWithVerify<T>();
var array = new T[len];
options.Security.DepthStep(ref reader);
try
{
for (int i = 0; i < array.Length; i++)
{
reader.Depth--;
reader.CancellationToken.ThrowIfCancellationRequested();
array[i] = formatter.Deserialize(ref reader, options);
}

return array;
}
finally
{
reader.Depth--;
}

return array;
}
}

Expand Down Expand Up @@ -529,7 +531,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override Stack<T>.Enumerator GetSourceEnumerator(Stack<T> source)
Expand Down Expand Up @@ -585,7 +587,7 @@ protected override ReadOnlyCollection<T> Complete(T[] intermediateCollection)

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}
}

Expand All @@ -599,7 +601,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override IList<T> Complete(T[] intermediateCollection)
Expand All @@ -618,7 +620,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override ICollection<T> Complete(T[] intermediateCollection)
Expand Down Expand Up @@ -672,7 +674,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override IEnumerable<T> Complete(T[] intermediateCollection)
Expand Down Expand Up @@ -903,9 +905,13 @@ public IList Deserialize(ref MessagePackReader reader, MessagePackSerializerOpti
return default(IList);
}

IMessagePackFormatter<object> formatter = options.Resolver.GetFormatterWithVerify<object>();

var count = reader.ReadArrayHeader();
if (count == 0)
{
return Array.Empty<object>();
}

IMessagePackFormatter<object> formatter = options.Resolver.GetFormatterWithVerify<object>();

var list = new object[count];
options.Security.DepthStep(ref reader);
Expand Down Expand Up @@ -1079,7 +1085,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override IReadOnlyList<T> Complete(T[] intermediateCollection)
Expand All @@ -1097,7 +1103,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override IReadOnlyCollection<T> Complete(T[] intermediateCollection)
Expand Down Expand Up @@ -1175,7 +1181,7 @@ protected override void Add(T[] collection, int index, T value, MessagePackSeria

protected override T[] Create(int count, MessagePackSerializerOptions options)
{
return new T[count];
return count == 0 ? Array.Empty<T>() : new T[count];
}

protected override ConcurrentStack<T> Complete(T[] intermediateCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,21 @@ public DateTime[] Deserialize(ref MessagePackReader reader, MessagePackSerialize
{
return null;
}
else

var len = reader.ReadArrayHeader();
if (len == 0)
{
var len = reader.ReadArrayHeader();
var array = new DateTime[len];
for (int i = 0; i < array.Length; i++)
{
var dateData = reader.ReadInt64();
array[i] = DateTime.FromBinary(dateData);
}
return Array.Empty<DateTime>();
}

return array;
var array = new DateTime[len];
for (int i = 0; i < array.Length; i++)
{
var dateData = reader.ReadInt64();
array[i] = DateTime.FromBinary(dateData);
}

return array;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ public object Deserialize(ref MessagePackReader reader, MessagePackSerializerOpt
case MessagePackType.Array:
{
var length = reader.ReadArrayHeader();
if (length == 0)
{
return Array.Empty<object>();
}

IMessagePackFormatter<object> objectFormatter = resolver.GetFormatter<object>();
var array = new object[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ public String[] Deserialize(ref MessagePackReader reader, MessagePackSerializerO
{
return null;
}
else

var len = reader.ReadArrayHeader();
if (len == 0)
{
var len = reader.ReadArrayHeader();
var array = new String[len];
for (int i = 0; i < array.Length; i++)
{
array[i] = reader.ReadString();
}
return Array.Empty<String>();
}

return array;
var array = new String[len];
for (int i = 0; i < array.Length; i++)
{
array[i] = reader.ReadString();
}

return array;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ public void EmitMatch(ILGenerator il, LocalBuilder bytesSpan, LocalBuilder key,

private class AutomataNode : IComparable<AutomataNode>
{
private static readonly AutomataNode[] EmptyNodes = new AutomataNode[0];
private static readonly ulong[] EmptyKeys = new ulong[0];

#pragma warning disable SA1401 // Fields should be private
internal ulong Key;
internal int Value;
Expand All @@ -164,8 +161,8 @@ public AutomataNode(ulong key)
{
this.Key = key;
this.Value = -1;
this.nexts = EmptyNodes;
this.nextKeys = EmptyKeys;
this.nexts = Array.Empty<AutomataNode>();
this.nextKeys = Array.Empty<ulong>();
this.count = 0;
this.OriginalKey = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
guid: 593f3b742b875ea48943776d02f8d3a9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

0 comments on commit d997b6e

Please sign in to comment.