Encode without array allocation

This commit is contained in:
Peter Butzhammer
2024-04-25 13:33:05 +02:00
parent b400a7215a
commit 08f572b582
3 changed files with 56 additions and 101 deletions

View File

@@ -49,6 +49,8 @@ internal abstract class ConverterTestBase
yield return new ConverterTestCase("ABCD", "DB99.WString10.4", [0x00, 0x04, 0x00, 0x04, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44]);
yield return new ConverterTestCase("ABCD", "DB99.WString10.6", [0x00, 0x06, 0x00, 0x04, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00]);
yield return new ConverterTestCase("ABCD", "DB99.Byte5.4", [0x41, 0x42, 0x43, 0x44]);
yield return new ConverterTestCase("A\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80A", "DB99.WString10.10",
[0x0, 0xA, 0x0, 0x9, 0x0, 0x41, 0xD8, 0x3D, 0xDC, 0x69, 0xD8, 0x3C, 0xDF, 0xFD, 0x20, 0xD, 0xD8, 0x3D, 0xDE, 0x80, 0x0, 0x41, 0x0, 0x0]);
yield return new ConverterTestCase(true, "DB99.DBx0.0", [0x01]);
yield return new ConverterTestCase(false, "DB99.DBx0.0", [0x00]);
@@ -69,15 +71,15 @@ internal abstract class ConverterTestBase
/// This helper method exists, since I could not manage to invoke a generic method
/// with a Span<T> parameter.
/// </summary>
public static void WriteToBuffer<TValue>(byte[] buffer, TValue value, VariableAddress address) =>
ValueConverter.WriteToBuffer(buffer, value, address);
public static TValue ReadFromBuffer<TValue>(byte[] buffer, VariableAddress address) =>
ValueConverter.ReadFromBuffer<TValue>(buffer, address);
/// <summary>
/// This helper method exists, since I could not manage to invoke a generic method
/// with a Span&lt;T&gt; parameter.
/// </summary>
public static TValue ReadFromBuffer<TValue>(byte[] buffer, VariableAddress address) =>
ValueConverter.ReadFromBuffer<TValue>(buffer, address);
public static void WriteToBuffer<TValue>(byte[] buffer, TValue value, VariableAddress address) =>
ValueConverter.WriteToBuffer(buffer, value, address);
public record ConverterTestCase(object Value, string Address, byte[] Data)
{

View File

@@ -25,6 +25,10 @@ internal class WriteToBuffer : ConverterTestBase
{
yield return new ConverterTestCase("aaaaBCDE", "DB0.string0.4", [0x04, 0x04, 0x61, 0x61, 0x61, 0x61]); // Length in address exceeds PLC string length
yield return new ConverterTestCase("aaaaBCDE", "DB0.WString0.4", [0x00, 0x04, 0x00, 0x04, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61, 0x00, 0x61]); // Length in address exceeds PLC string length
yield return new ConverterTestCase("aaaaBCDE", "DB0.DBB0.4", [0x61, 0x61, 0x61, 0x61]); // Length in address exceeds PLC array length
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.WString0.2", [0x00, 0x02, 0x00, 0x02, 0xD8, 0x3D, 0xDC, 0x69]); // Length in address exceeds PLC string length, multi char unicode point
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.String0.2", [0x02, 0x02, 0x3F, 0x3F]); // Length in address exceeds PLC string length, multi char unicode point
yield return new ConverterTestCase("\ud83d\udc69\ud83c\udffd\u200d\ud83d\ude80", "DB0.DBB0.4", [0x3F, 0x3F, 0x3F, 0x3F]); // Length in address exceeds PLC string length, multi char unicode point
}
[TestCase(18, "DB0.DInt12", 3)]

View File

@@ -1,5 +1,4 @@
using System.Buffers.Binary;
using System.Runtime.InteropServices;
using System.Text;
using Sharp7.Rx.Enums;
@@ -18,7 +17,7 @@ internal static class ValueConverter
}
},
{typeof(byte), (data, address, value) => data[0] = (byte) value},
{typeof(byte), (data, _, value) => data[0] = (byte) value},
{
typeof(byte[]), (data, address, value) =>
{
@@ -30,72 +29,58 @@ internal static class ValueConverter
}
},
{typeof(short), (data, address, value) => BinaryPrimitives.WriteInt16BigEndian(data, (short) value)},
{typeof(ushort), (data, address, value) => BinaryPrimitives.WriteUInt16BigEndian(data, (ushort) value)},
{typeof(int), (data, address, value) => BinaryPrimitives.WriteInt32BigEndian(data, (int) value)},
{typeof(uint), (data, address, value) => BinaryPrimitives.WriteUInt32BigEndian(data, (uint) value)},
{typeof(long), (data, address, value) => BinaryPrimitives.WriteInt64BigEndian(data, (long) value)},
{typeof(ulong), (data, address, value) => BinaryPrimitives.WriteUInt64BigEndian(data, (ulong) value)},
{typeof(short), (data, _, value) => BinaryPrimitives.WriteInt16BigEndian(data, (short) value)},
{typeof(ushort), (data, _, value) => BinaryPrimitives.WriteUInt16BigEndian(data, (ushort) value)},
{typeof(int), (data, _, value) => BinaryPrimitives.WriteInt32BigEndian(data, (int) value)},
{typeof(uint), (data, _, value) => BinaryPrimitives.WriteUInt32BigEndian(data, (uint) value)},
{typeof(long), (data, _, value) => BinaryPrimitives.WriteInt64BigEndian(data, (long) value)},
{typeof(ulong), (data, _, value) => BinaryPrimitives.WriteUInt64BigEndian(data, (ulong) value)},
{
typeof(float), (data, address, value) =>
{
var map = new UInt32SingleMap
{
Single = (float) value
};
BinaryPrimitives.WriteUInt32BigEndian(data, map.UInt32);
}
},
{
typeof(double), (data, address, value) =>
{
var map = new UInt64DoubleMap
{
Double = (double) value
};
BinaryPrimitives.WriteUInt64BigEndian(data, map.UInt64);
}
},
{typeof(float), (data, _, value) => BinaryPrimitives.WriteSingleBigEndian(data, (float) value)},
{typeof(double), (data, _, value) => BinaryPrimitives.WriteDoubleBigEndian(data, (double) value)},
{
typeof(string), (data, address, value) =>
{
if (value is not string stringValue) throw new ArgumentException("Value must be of type string", nameof(value));
var length = Math.Min(address.Length, stringValue.Length);
switch (address.Type)
{
case DbType.String:
data[0] = (byte) address.Length;
data[1] = (byte) length;
// Todo: Serialize directly to Span, when upgrading to .net
Encoding.ASCII.GetBytes(stringValue)
.AsSpan(0, length)
.CopyTo(data.Slice(2));
EncodeString(data);
return;
case DbType.WString:
BinaryPrimitives.WriteUInt16BigEndian(data, address.Length);
BinaryPrimitives.WriteUInt16BigEndian(data.Slice(2), (ushort) length);
// Todo: Serialize directly to Span, when upgrading to .net
Encoding.BigEndianUnicode.GetBytes(stringValue)
.AsSpan(0, length * 2)
.CopyTo(data.Slice(4));
EncodeWString(data);
return;
case DbType.Byte:
// Todo: Serialize directly to Span, when upgrading to .net
Encoding.ASCII.GetBytes(stringValue)
.AsSpan(0, length)
.CopyTo(data);
Encoding.ASCII.GetBytes(stringValue.AsSpan(0, address.Length), data);
return;
default:
throw new DataTypeMissmatchException($"Cannot write string to {address.Type}", typeof(string), address);
}
void EncodeString(Span<byte> span)
{
var encodedLength = Encoding.ASCII.GetByteCount(stringValue);
var length = Math.Min(address.Length, encodedLength);
span[0] = (byte) address.Length;
span[1] = (byte) length;
Encoding.ASCII.GetBytes(stringValue.AsSpan(0, length), span[2..]);
}
void EncodeWString(Span<byte> span)
{
var length = Math.Min(address.Length, stringValue.Length);
BinaryPrimitives.WriteUInt16BigEndian(span, address.Length);
BinaryPrimitives.WriteUInt16BigEndian(span[2..], (ushort) length);
var readOnlySpan = stringValue.AsSpan(0, length);
Encoding.BigEndianUnicode.GetBytes(readOnlySpan, span[4..]);
}
}
}
};
@@ -104,39 +89,17 @@ internal static class ValueConverter
{
{typeof(bool), (buffer, address) => (buffer[0] >> address.Bit & 1) > 0},
{typeof(byte), (buffer, address) => buffer[0]},
{typeof(byte[]), (buffer, address) => buffer.ToArray()},
{typeof(byte), (buffer, _) => buffer[0]},
{typeof(byte[]), (buffer, _) => buffer.ToArray()},
{typeof(short), (buffer, address) => BinaryPrimitives.ReadInt16BigEndian(buffer)},
{typeof(ushort), (buffer, address) => BinaryPrimitives.ReadUInt16BigEndian(buffer)},
{typeof(int), (buffer, address) => BinaryPrimitives.ReadInt32BigEndian(buffer)},
{typeof(uint), (buffer, address) => BinaryPrimitives.ReadUInt32BigEndian(buffer)},
{typeof(long), (buffer, address) => BinaryPrimitives.ReadInt64BigEndian(buffer)},
{typeof(ulong), (buffer, address) => BinaryPrimitives.ReadUInt64BigEndian(buffer)},
{
typeof(float), (buffer, address) =>
{
// Todo: Use BinaryPrimitives when switched to newer .net
var d = new UInt32SingleMap
{
UInt32 = BinaryPrimitives.ReadUInt32BigEndian(buffer)
};
return d.Single;
}
},
{
typeof(double), (buffer, address) =>
{
// Todo: Use BinaryPrimitives when switched to newer .net
var d = new UInt64DoubleMap
{
UInt64 = BinaryPrimitives.ReadUInt64BigEndian(buffer)
};
return d.Double;
}
},
{typeof(short), (buffer, _) => BinaryPrimitives.ReadInt16BigEndian(buffer)},
{typeof(ushort), (buffer, _) => BinaryPrimitives.ReadUInt16BigEndian(buffer)},
{typeof(int), (buffer, _) => BinaryPrimitives.ReadInt32BigEndian(buffer)},
{typeof(uint), (buffer, _) => BinaryPrimitives.ReadUInt32BigEndian(buffer)},
{typeof(long), (buffer, _) => BinaryPrimitives.ReadInt64BigEndian(buffer)},
{typeof(ulong), (buffer, _) => BinaryPrimitives.ReadUInt64BigEndian(buffer)},
{typeof(float), (buffer, _) => BinaryPrimitives.ReadSingleBigEndian(buffer)},
{typeof(double), (buffer, _) => BinaryPrimitives.ReadDoubleBigEndian(buffer)},
{
typeof(string), (buffer, address) =>
@@ -202,21 +165,7 @@ internal static class ValueConverter
writeFunc(buffer, address, value);
}
delegate object ReadFunc(Span<byte> data, VariableAddress address);
private delegate object ReadFunc(Span<byte> data, VariableAddress address);
[StructLayout(LayoutKind.Explicit)]
private struct UInt32SingleMap
{
[FieldOffset(0)] public uint UInt32;
[FieldOffset(0)] public float Single;
}
[StructLayout(LayoutKind.Explicit)]
private struct UInt64DoubleMap
{
[FieldOffset(0)] public ulong UInt64;
[FieldOffset(0)] public double Double;
}
delegate void WriteFunc(Span<byte> data, VariableAddress address, object value);
private delegate void WriteFunc(Span<byte> data, VariableAddress address, object value);
}