public void EncodingPerformanceTestUsingCorefxlab(int charLength, int minCodePoint, int maxCodePoint) { string unicodeString = GenerateString(charLength, minCodePoint, maxCodePoint); ReadOnlySpan <char> characters = unicodeString.AsSpan(); int consumed; int encodedBytes; Assert.True(Utf8Encoder.TryComputeEncodedBytes(characters, out encodedBytes)); byte[] utf8Buffer = new byte[encodedBytes]; Span <byte> span = new Span <byte>(utf8Buffer); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) if (!Utf8Encoder.TryEncode(characters, span, out consumed, out encodedBytes)) { throw new Exception(); // this should not happen } } }
public override bool TryComputeEncodedBytes(ReadOnlySpan <uint> utf32, out int bytesNeeded) => Utf8Encoder.TryComputeEncodedBytes(utf32, out bytesNeeded);