I have a Span<byte> representing an escaped string UTF-8 like:
Binary represention:
byte[20] { 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 32, 92, 117, 50, 48, 97, 99, 32, 33 }
Escaped represention:"Hello world \u20ac !"
Desired binary result:
byte[17] { 72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 32, 226, 130, 172, 32, 33 }
I tried to transcode the escaped \u20ac by using the GetString() method:
Encoding.UTF8.GetBytes(Encoding.UTF8.GetString())
But this is not unescaping the input.
Is there any way to achieve to the same result ?
// Not working solution
public void NotWorkingUnescape(ReadOnlySpan<byte> source, Span<byte> destination)
{
var tmp = Encoding.UTF8.GetString(source);
Encoding.UTF8.GetBytes(tmp, destination);
}
// Unknown solution
// UTF-8 escaped byte array -> UTF8-8 unescaped byte array
public void FastUnescape(ReadOnlySpan<byte> source, Span<byte> destination)
{
// ?
}