/// <inheritdoc/> public override int GetCharCount(ReadOnlySpan <byte> bytes) { var encoder = new Base85Encoder(Settings); Span <char> outputBuffer = stackalloc char[1024]; int inputOffset = 0; int inputCount = bytes.Length; int outputLength = 0; ConvertStatus convertStatus; do { convertStatus = encoder.ConvertData(bytes, inputOffset, inputCount, outputBuffer, 0, outputBuffer.Length, true, out var inputUsed, out var outputUsed); outputLength += outputUsed; inputOffset += inputUsed; inputCount -= inputUsed; } while (!convertStatus); return(outputLength); }
/// <inheritdoc/> public override char[] GetChars(ReadOnlySpan <byte> bytes) { // GetChars process the data in two passes, in order to avoid allocating // temporary storage for the result (which would have to be resized) int charsRequired = GetCharCount(bytes); if (charsRequired == 0) { return(Array.Empty <char>()); } var outputChars = new char[charsRequired]; var encoder = new Base85Encoder(Settings); var convertStatus = encoder.ConvertData(bytes, 0, bytes.Length, outputChars.AsSpan(), 0, outputChars.Length, true, out var inputUsed, out var outputUsed); System.Diagnostics.Debug.Assert(convertStatus); System.Diagnostics.Debug.Assert(inputUsed == bytes.Length); System.Diagnostics.Debug.Assert(outputUsed == outputChars.Length); return(outputChars); }
/// <inheritdoc/> public override unsafe string GetString(ReadOnlySpan <byte> bytes) { var encoder = new Base85Encoder(Settings); Span <char> outputBuffer = stackalloc char[1024]; StringBuilder sb = new StringBuilder(); int inputOffset = 0; int inputCount = bytes.Length; ConvertStatus convertStatus; do { convertStatus = encoder.ConvertData(bytes, inputOffset, inputCount, outputBuffer, 0, outputBuffer.Length, true, out var inputUsed, out var outputUsed); if (outputUsed > 0) { fixed(char *charPtr = &outputBuffer[0]) sb.Append(charPtr, outputUsed); } inputOffset += inputUsed; inputCount -= inputUsed; } while (!convertStatus); return(sb.ToString()); }
public Encoder(Base85Settings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _encoder = new Base85Encoder(settings); }