I've been trying to generate a alocation free from a struct in c#
Basically I create my on struct that recives an string (ReadOnlySpan) that could have any size like 'ABC1234.5GDS' and then inside of the constructor of that struct I create a Span stackalloc char[5] then I interate on ReadOnlySpan getting only the 5 first numbers at the end my Span has the '12345' on it but how do I pin that address to a char[] so I can use that later.
Basically I want to do the same as DateTime that you can be allocation free until you call ToString()
public readonly struct AllocFree
{
private readonly char[] data;
public AllocFree(ReadOnlySpan<char> input)
{
int position = 0;
Span<char> newData = stackalloc char[5];
for (int i = 0; i < input.Length; i++)
{
if (char.IsDigit(input[i]))
{
newData[position] = input[i];
position++;
}
if (position == 4)
break;
}
// How to make this a allocation free.
data = newData;
// This is a no allocation free.
data = newData.ToArray();
}
public override string ToString() => new String(data);
}