Thanks to visit codestin.com
Credit goes to github.com

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions net/FlatBuffers/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ public byte[] ToFullArray()
}

#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
public ReadOnlySpan<byte> ToSizedReadOnlySpan()
{
return _buffer.ReadOnlySpan.Slice(Position, Length - Position);
}

public ReadOnlyMemory<byte> ToReadOnlyMemory(int pos, int len)
{
return _buffer.ReadOnlyMemory.Slice(pos, len);
Expand All @@ -289,6 +294,11 @@ public Span<byte> ToSpan(int pos, int len)
{
return _buffer.Span.Slice(pos, len);
}

public ReadOnlySpan<byte> ToReadOnlySpan(int pos, int len)
{
return _buffer.ReadOnlySpan.Slice(pos, len);
}
#else
public ArraySegment<byte> ToArraySegment(int pos, int len)
{
Expand Down Expand Up @@ -380,38 +390,40 @@ protected ulong ReadLittleEndian(int offset, int count)
#elif ENABLE_SPAN_T
protected void WriteLittleEndian(int offset, int count, ulong data)
{
Span<byte> span = _buffer.Span.Slice(offset, count);
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
{
_buffer.Span[offset + i] = (byte)(data >> i * 8);
span[i] = (byte)(data >> i * 8);
}
}
else
{
for (int i = 0; i < count; i++)
{
_buffer.Span[offset + count - 1 - i] = (byte)(data >> i * 8);
span[count - 1 - i] = (byte)(data >> i * 8);
}
}
}

protected ulong ReadLittleEndian(int offset, int count)
{
AssertOffsetAndLength(offset, count);
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset, count);
ulong r = 0;
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
{
r |= (ulong)_buffer.Span[offset + i] << i * 8;
r |= (ulong)span[i] << i * 8;
}
}
else
{
for (int i = 0; i < count; i++)
{
r |= (ulong)_buffer.Span[offset + count - 1 - i] << i * 8;
r |= (ulong)span[count - 1 - i] << i * 8;
}
}
return r;
Expand Down Expand Up @@ -445,8 +457,7 @@ public void PutByte(int offset, byte value, int count)
{
AssertOffsetAndLength(offset, sizeof(byte) * count);
Span<byte> span = _buffer.Span.Slice(offset, count);
for (var i = 0; i < span.Length; ++i)
span[i] = value;
span.Fill(value);
}
#else
public void PutSbyte(int offset, sbyte value)
Expand Down
15 changes: 15 additions & 0 deletions net/FlatBuffers/FlatBufferBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,21 @@ public byte[] SizedByteArray()
return _bb.ToSizedArray();
}

#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
/// <summary>
/// A utility function return the ByteBuffer data as a
/// `ReadOnlySpan<byte>`.
/// </summary>
/// <returns>
/// A `ReadOnlySpan<byte>` that references the internal
/// ByteBuffer data.
/// </returns>
public ReadOnlySpan<byte> SizedReadOnlySpan()
{
return _bb.ToSizedReadOnlySpan();
}
#endif

/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions net/FlatBuffers/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ public static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
var len_2 = bb.GetInt(offset_2);
var startPos_1 = offset_1 + sizeof(int);
var startPos_2 = offset_2 + sizeof(int);

#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
var span_1 = bb.ToReadOnlySpan(startPos_1, len_1);
var span_2 = bb.ToReadOnlySpan(startPos_2, len_2);
return span_1.SequenceCompareTo(span_2);
#else
var len = Math.Min(len_1, len_2);
for(int i = 0; i < len; i++) {
byte b1 = bb.Get(i + startPos_1);
Expand All @@ -191,6 +197,7 @@ public static int CompareStrings(int offset_1, int offset_2, ByteBuffer bb)
return b1 - b2;
}
return len_1 - len_2;
#endif
}

// Compare string from the ByteBuffer with the string object
Expand All @@ -200,13 +207,19 @@ public static int CompareStrings(int offset_1, byte[] key, ByteBuffer bb)
var len_1 = bb.GetInt(offset_1);
var len_2 = key.Length;
var startPos_1 = offset_1 + sizeof(int);
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
ReadOnlySpan<byte> span = bb.ToReadOnlySpan(startPos_1, len_1);
ReadOnlySpan<byte> keySpan = key;
return span.SequenceCompareTo(keySpan);
#else
var len = Math.Min(len_1, len_2);
for (int i = 0; i < len; i++) {
byte b = bb.Get(i + startPos_1);
if (b != key[i])
return b - key[i];
}
return len_1 - len_2;
#endif
}
}
}
Loading