// Moved out of outer switch to have a shorter main method (improves performance) static object ReadFieldValueSlow(ReadOnlySpan <byte> span, out int bytesRead) { var slice = span.Slice(1); switch ((char)span[0]) { case 'F': bytesRead = 1 + ReadDictionary(slice, out var dictionary); return(dictionary); case 'A': IList arrayResult = ReadArray(slice, out int arrayBytesRead); bytesRead = 1 + arrayBytesRead; return(arrayResult); case 'l': bytesRead = 9; return(NetworkOrderDeserializer.ReadInt64(slice)); case 'i': bytesRead = 5; return(NetworkOrderDeserializer.ReadUInt32(slice)); case 'D': bytesRead = 6; return(ReadDecimal(slice)); case 'B': bytesRead = 2; return(span[1]); case 'b': bytesRead = 2; return((sbyte)span[1]); case 'd': bytesRead = 9; return(NetworkOrderDeserializer.ReadDouble(slice)); case 'f': bytesRead = 5; return(NetworkOrderDeserializer.ReadSingle(slice)); case 's': bytesRead = 3; return(NetworkOrderDeserializer.ReadInt16(slice)); case 'T': bytesRead = 1 + ReadTimestamp(slice, out var timestamp); return(timestamp); case 'x': bytesRead = 1 + ReadLongstr(slice, out var binaryTableResult); return(new BinaryTableValue(binaryTableResult)); default: bytesRead = 0; return(ThrowInvalidTableValue((char)span[0])); } }
public void TestSingleDecoding() { Assert.Equal(1.234f, NetworkOrderDeserializer.ReadSingle(_expectedSingleBytes.AsSpan())); }
public static object ReadFieldValue(ReadOnlyMemory <byte> memory, out int bytesRead) { bytesRead = 1; ReadOnlyMemory <byte> slice = memory.Slice(1); switch ((char)memory.Span[0]) { case 'S': byte[] result = ReadLongstr(slice); bytesRead += result.Length + 4; return(result); case 'I': bytesRead += 4; return(NetworkOrderDeserializer.ReadInt32(slice)); case 'i': bytesRead += 4; return(NetworkOrderDeserializer.ReadUInt32(slice)); case 'D': bytesRead += 5; return(ReadDecimal(slice)); case 'T': bytesRead += 8; return(ReadTimestamp(slice)); case 'F': IDictionary <string, object> tableResult = ReadTable(slice, out int tableBytesRead); bytesRead += tableBytesRead; return(tableResult); case 'A': IList arrayResult = ReadArray(slice, out int arrayBytesRead); bytesRead += arrayBytesRead; return(arrayResult); case 'B': bytesRead += 1; return(slice.Span[0]); case 'b': bytesRead += 1; return((sbyte)slice.Span[0]); case 'd': bytesRead += 8; return(NetworkOrderDeserializer.ReadDouble(slice)); case 'f': bytesRead += 4; return(NetworkOrderDeserializer.ReadSingle(slice)); case 'l': bytesRead += 8; return(NetworkOrderDeserializer.ReadInt64(slice)); case 's': bytesRead += 2; return(NetworkOrderDeserializer.ReadInt16(slice)); case 't': bytesRead += 1; return(slice.Span[0] != 0); case 'x': byte[] binaryTableResult = ReadLongstr(slice); bytesRead += binaryTableResult.Length + 4; return(new BinaryTableValue(binaryTableResult)); case 'V': return(null); default: throw new SyntaxError($"Unrecognised type in table: {(char)memory.Span[0]}"); } }
public static object ReadFieldValue(ReadOnlySpan <byte> span, out int bytesRead) { bytesRead = 1; switch ((char)span[0]) { case 'S': bytesRead += ReadLongstr(span.Slice(1), out var bytes); return(bytes); case 'I': bytesRead += 4; return(NetworkOrderDeserializer.ReadInt32(span.Slice(1))); case 'i': bytesRead += 4; return(NetworkOrderDeserializer.ReadUInt32(span.Slice(1))); case 'D': bytesRead += 5; return(ReadDecimal(span.Slice(1))); case 'T': bytesRead += ReadTimestamp(span.Slice(1), out var timestamp); return(timestamp); case 'F': bytesRead += ReadDictionary(span.Slice(1), out var dictionary); return(dictionary); case 'A': IList arrayResult = ReadArray(span.Slice(1), out int arrayBytesRead); bytesRead += arrayBytesRead; return(arrayResult); case 'B': bytesRead += 1; return(span[1]); case 'b': bytesRead += 1; return((sbyte)span[1]); case 'd': bytesRead += 8; return(NetworkOrderDeserializer.ReadDouble(span.Slice(1))); case 'f': bytesRead += 4; return(NetworkOrderDeserializer.ReadSingle(span.Slice(1))); case 'l': bytesRead += 8; return(NetworkOrderDeserializer.ReadInt64(span.Slice(1))); case 's': bytesRead += 2; return(NetworkOrderDeserializer.ReadInt16(span.Slice(1))); case 't': bytesRead += 1; return(span[1] != 0); case 'x': bytesRead += ReadLongstr(span.Slice(1), out var binaryTableResult); return(new BinaryTableValue(binaryTableResult)); case 'V': return(null); default: throw new SyntaxErrorException($"Unrecognised type in table: {(char)span[0]}"); } }