< Summary

Information
Class: DeltaSharp.Format.ASCIIDeltaReader
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/ASCII/ASCIIDeltaReader.cs
Line coverage
94%
Covered lines: 51
Uncovered lines: 3
Coverable lines: 54
Total lines: 88
Line coverage: 94.4%
Branch coverage
94%
Covered branches: 55
Total branches: 58
Branch coverage: 94.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryRead(...)94.82%595894.44%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Format/ASCII/ASCIIDeltaReader.cs

#LineLine coverage
 1using System.Buffers.Text;
 2
 3namespace DeltaSharp.Format;
 4
 5/// <summary>Reads DeltaSharp ASCII command headers with decimal integer operands.</summary>
 6/// <remarks>Insert payloads contain raw bytes and need not be ASCII text.</remarks>
 7public class ASCIIDeltaReader : IDeltaReader
 8{
 9    /// <inheritdoc cref="IDeltaReader.TryRead"/>
 10    public bool TryRead(ReadOnlySpan<byte> input, out DeltaCommand command, out uint consumed)
 11    {
 22124012        command = default;
 22124013        consumed = 0;
 22124014        var originalLength = input.Length;
 15
 28877016        while (!input.IsEmpty && (input[0] == '\n' || input[0] == '\r'))
 6753017            input = input.Slice(1);
 18
 22124019        if (input.IsEmpty)
 33020            return false;
 21
 22091022        var commandByte = input[0];
 22091023        if (commandByte != 'l' && commandByte != 'c' && commandByte != 'i' && commandByte != 'v')
 1945024            throw new DeltaReaderException("Invalid command");
 25
 20146026        if (input.Length < 2)
 3027            return false;
 20143028        if (input[1] != ' ')
 31029            throw new DeltaReaderException("Expected whitespace");
 20112030        input = input.Slice(2);
 31
 20112032        if (!Utf8Parser.TryParse(input, out ulong first, out var firstBytes))
 3033            throw new DeltaReaderException("Failed to parse number");
 20109034        input = input.Slice(firstBytes);
 35
 20109036        ulong position = 0;
 20109037        ulong length = first;
 20109038        if (commandByte == 'c')
 39        {
 7554040            if (input.IsEmpty || input[0] != ' ')
 1041                throw new DeltaReaderException("Expected whitespace");
 7553042            input = input.Slice(1);
 7553043            position = first;
 7553044            if (!Utf8Parser.TryParse(input, out length, out var lengthBytes))
 045                throw new DeltaReaderException("Failed to parse number");
 7553046            input = input.Slice(lengthBytes);
 47        }
 48
 20108049        if (commandByte == 'v')
 50        {
 500051            if (!input.IsEmpty && input[0] != '\n' && input[0] != '\r')
 1052                throw new DeltaReaderException("Expected end of checksum");
 53        }
 54        else
 55        {
 19608056            if (input.IsEmpty)
 2057                return false;
 58
 59            // Insert data can follow a space (writer format) or a line ending.
 60            // Consume only the separator: literal data may itself start with CR/LF.
 19606061            if (commandByte == 'i' && input[0] == ' ')
 6740062                input = input.Slice(1);
 12866063            else if (input[0] == '\n')
 12860064                input = input.Slice(1);
 6065            else if (input[0] == '\r')
 66            {
 3067                if (input.Length < 2)
 068                    return false;
 3069                if (input[1] != '\n')
 070                    throw new DeltaReaderException("Expected newline");
 3071                input = input.Slice(2);
 72            }
 73            else
 3074                throw new DeltaReaderException("Expected newline");
 75        }
 76
 20102077        var kind = commandByte switch
 20102078        {
 5305079            (byte)'l' => DeltaCommandEnum.Length,
 7552080            (byte)'c' => DeltaCommandEnum.Copy,
 6746081            (byte)'i' => DeltaCommandEnum.Insert,
 499082            _ => DeltaCommandEnum.Checksum
 20102083        };
 20102084        command = new DeltaCommand(kind, position, length);
 20102085        consumed = (uint)(originalLength - input.Length);
 20102086        return true;
 87    }
 88}