< Summary

Information
Class: DeltaSharp.Format.BinaryDeltaReader
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/Binary/BinaryDeltaReader.cs
Line coverage
93%
Covered lines: 31
Uncovered lines: 2
Coverable lines: 33
Total lines: 90
Line coverage: 93.9%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryRead(...)90.9%222293.93%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/Binary/BinaryDeltaReader.cs

#LineLine coverage
 1using DeltaSharp.Util;
 2
 3namespace DeltaSharp.Format;
 4
 5/// <summary>Reads DeltaSharp binary command headers with variable-length integer operands.</summary>
 6public class BinaryDeltaReader : IDeltaReader
 7{
 8
 9    /// <inheritdoc cref="IDeltaReader.TryRead"/>
 10    public bool TryRead(ReadOnlySpan<byte> input, out DeltaCommand command, out uint consumed)
 11    {
 23417012        command = default;
 23417013        consumed = 0;
 14
 23417015        if (input.IsEmpty)
 30016            return false;
 17
 23387018        var commandChar = (char)input[0];
 19
 20        switch (commandChar)
 21        {
 22            case 'l':
 23                {
 5310024                    input = input.Slice(1);
 5310025                    if (!VarIntFormatter.TryDecode(input, out var length, out var lengthChars))
 1026                        return false;
 27
 5309028                    consumed = (uint)lengthChars + 1;
 29
 5309030                    command = new DeltaCommand(DeltaCommandEnum.Length, 0, length);
 31
 5309032                    return true;
 33                }
 34            case 'c':
 35                {
 8215036                    input = input.Slice(1);
 8215037                    if (!VarIntFormatter.TryDecode(input, out var position, out var positionBytes))
 038                        return false;
 39
 8215040                    input = input.Slice(positionBytes);
 8215041                    if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes))
 042                        return false;
 43
 8215044                    consumed = (uint)positionBytes + (uint)lengthBytes + 1;
 45
 8215046                    command = new DeltaCommand(DeltaCommandEnum.Copy, position, length);
 47
 8215048                    return true;
 49                }
 50            case 'i':
 51                {
 7400052                    input = input.Slice(1);
 7400053                    if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes))
 1054                        return false;
 55
 7399056                    consumed = (uint)lengthBytes + 1;
 57
 7399058                    command = new DeltaCommand(DeltaCommandEnum.Insert, 0, length);
 59
 7399060                    return true;
 61                }
 62            case 'v':
 63                {
 514064                    input = input.Slice(1);
 514065                    if (!VarIntFormatter.TryDecode(input, out var length, out var lengthBytes))
 5066                        return false;
 67
 509068                    consumed = (uint)lengthBytes + 1;
 69
 509070                    command = new DeltaCommand(DeltaCommandEnum.Checksum, 0, length);
 71
 509072                    return true;
 73                }
 74            default:
 1948075                throw new DeltaReaderException("Invalid command");
 76        }
 77    }
 78
 79}
 80
 81/// <summary>Reports invalid syntax or an unsupported numeric value in an encoded delta command.</summary>
 82public class DeltaReaderException : Exception
 83{
 84    /// <summary>Initializes a reader exception with the default exception message.</summary>
 85    public DeltaReaderException() { }
 86
 87    /// <summary>Initializes a reader exception with a description of the parsing failure.</summary>
 88    /// <param name="message">The description of the invalid command.</param>
 89    public DeltaReaderException(string message) : base(message) { }
 90}