| | | 1 | | using System.Buffers.Text; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 7 | | public class ASCIIDeltaReader : IDeltaReader |
| | | 8 | | { |
| | | 9 | | /// <inheritdoc cref="IDeltaReader.TryRead"/> |
| | | 10 | | public bool TryRead(ReadOnlySpan<byte> input, out DeltaCommand command, out uint consumed) |
| | | 11 | | { |
| | 221240 | 12 | | command = default; |
| | 221240 | 13 | | consumed = 0; |
| | 221240 | 14 | | var originalLength = input.Length; |
| | | 15 | | |
| | 288770 | 16 | | while (!input.IsEmpty && (input[0] == '\n' || input[0] == '\r')) |
| | 67530 | 17 | | input = input.Slice(1); |
| | | 18 | | |
| | 221240 | 19 | | if (input.IsEmpty) |
| | 330 | 20 | | return false; |
| | | 21 | | |
| | 220910 | 22 | | var commandByte = input[0]; |
| | 220910 | 23 | | if (commandByte != 'l' && commandByte != 'c' && commandByte != 'i' && commandByte != 'v') |
| | 19450 | 24 | | throw new DeltaReaderException("Invalid command"); |
| | | 25 | | |
| | 201460 | 26 | | if (input.Length < 2) |
| | 30 | 27 | | return false; |
| | 201430 | 28 | | if (input[1] != ' ') |
| | 310 | 29 | | throw new DeltaReaderException("Expected whitespace"); |
| | 201120 | 30 | | input = input.Slice(2); |
| | | 31 | | |
| | 201120 | 32 | | if (!Utf8Parser.TryParse(input, out ulong first, out var firstBytes)) |
| | 30 | 33 | | throw new DeltaReaderException("Failed to parse number"); |
| | 201090 | 34 | | input = input.Slice(firstBytes); |
| | | 35 | | |
| | 201090 | 36 | | ulong position = 0; |
| | 201090 | 37 | | ulong length = first; |
| | 201090 | 38 | | if (commandByte == 'c') |
| | | 39 | | { |
| | 75540 | 40 | | if (input.IsEmpty || input[0] != ' ') |
| | 10 | 41 | | throw new DeltaReaderException("Expected whitespace"); |
| | 75530 | 42 | | input = input.Slice(1); |
| | 75530 | 43 | | position = first; |
| | 75530 | 44 | | if (!Utf8Parser.TryParse(input, out length, out var lengthBytes)) |
| | 0 | 45 | | throw new DeltaReaderException("Failed to parse number"); |
| | 75530 | 46 | | input = input.Slice(lengthBytes); |
| | | 47 | | } |
| | | 48 | | |
| | 201080 | 49 | | if (commandByte == 'v') |
| | | 50 | | { |
| | 5000 | 51 | | if (!input.IsEmpty && input[0] != '\n' && input[0] != '\r') |
| | 10 | 52 | | throw new DeltaReaderException("Expected end of checksum"); |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 196080 | 56 | | if (input.IsEmpty) |
| | 20 | 57 | | 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. |
| | 196060 | 61 | | if (commandByte == 'i' && input[0] == ' ') |
| | 67400 | 62 | | input = input.Slice(1); |
| | 128660 | 63 | | else if (input[0] == '\n') |
| | 128600 | 64 | | input = input.Slice(1); |
| | 60 | 65 | | else if (input[0] == '\r') |
| | | 66 | | { |
| | 30 | 67 | | if (input.Length < 2) |
| | 0 | 68 | | return false; |
| | 30 | 69 | | if (input[1] != '\n') |
| | 0 | 70 | | throw new DeltaReaderException("Expected newline"); |
| | 30 | 71 | | input = input.Slice(2); |
| | | 72 | | } |
| | | 73 | | else |
| | 30 | 74 | | throw new DeltaReaderException("Expected newline"); |
| | | 75 | | } |
| | | 76 | | |
| | 201020 | 77 | | var kind = commandByte switch |
| | 201020 | 78 | | { |
| | 53050 | 79 | | (byte)'l' => DeltaCommandEnum.Length, |
| | 75520 | 80 | | (byte)'c' => DeltaCommandEnum.Copy, |
| | 67460 | 81 | | (byte)'i' => DeltaCommandEnum.Insert, |
| | 4990 | 82 | | _ => DeltaCommandEnum.Checksum |
| | 201020 | 83 | | }; |
| | 201020 | 84 | | command = new DeltaCommand(kind, position, length); |
| | 201020 | 85 | | consumed = (uint)(originalLength - input.Length); |
| | 201020 | 86 | | return true; |
| | | 87 | | } |
| | | 88 | | } |