/// <inheritdoc/> public override byte[] GetBytes(ReadOnlySpan <char> chars) { // GetBytes converts in two passes so that it can perform a single allocation // for the final array of bytes... int bytesRequired = GetByteCount(chars); if (bytesRequired == 0) { return(Array.Empty <byte>()); } var outputBytes = new byte[bytesRequired]; var decoder = new QpDecoder(Settings); var convertStatus = decoder.ConvertData(chars, 0, chars.Length, outputBytes.AsSpan(), 0, outputBytes.Length, true, out var inputUsed, out var outputUsed); System.Diagnostics.Debug.Assert(convertStatus); System.Diagnostics.Debug.Assert(inputUsed == chars.Length); System.Diagnostics.Debug.Assert(outputUsed == outputBytes.Length); return(outputBytes); }
/// <inheritdoc/> public override int GetByteCount(ReadOnlySpan <char> chars) { var decoder = new QpDecoder(Settings); Span <byte> outputBuffer = stackalloc byte[1024]; int inputOffset = 0; int inputCount = chars.Length; int outputLength = 0; ConvertStatus convertStatus; do { convertStatus = decoder.ConvertData(chars, inputOffset, inputCount, outputBuffer, 0, outputBuffer.Length, true, out var inputUsed, out var outputUsed); outputLength += outputUsed; inputOffset += inputUsed; inputCount -= inputUsed; } while (!convertStatus); return(outputLength); }
protected override ConvertStatus ConvertData(ReadOnlySpan <char> inputData, int inputIndex, int inputCount, Span <byte> outputData, int outputIndex, int outputCount, bool flush, out int inputUsed, out int outputUsed) { return(_codecDecoder.ConvertData(inputData, inputIndex, inputCount, outputData, outputIndex, outputCount, flush, out inputUsed, out outputUsed)); }