|
| 1 | +using System.Buffers; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace Cosmo.Transport.Pipelines; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Reusable high-performance parsing extensions for System.IO.Pipelines. |
| 8 | +/// </summary> |
| 9 | +public static class PipelineExtensions |
| 10 | +{ |
| 11 | + public static bool TryReadInt32BigEndian(this ref SequenceReader<byte> reader, out int value) |
| 12 | + { |
| 13 | + if (reader.TryRead(out byte b1) && reader.TryRead(out byte b2) && |
| 14 | + reader.TryRead(out byte b3) && reader.TryRead(out byte b4)) |
| 15 | + { |
| 16 | + value = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; |
| 17 | + return true; |
| 18 | + } |
| 19 | + value = 0; |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + public static string ReadUtf8String(this ref SequenceReader<byte> reader, int byteLength) |
| 24 | + { |
| 25 | + if (reader.Remaining < byteLength) throw new InvalidOperationException("Not enough data."); |
| 26 | + |
| 27 | + byte[] buffer = new byte[byteLength]; |
| 28 | + reader.TryCopyTo(buffer); |
| 29 | + reader.Advance(byteLength); |
| 30 | + return Encoding.UTF8.GetString(buffer); |
| 31 | + } |
| 32 | + |
| 33 | + public static string ReadUtf16String(this ref SequenceReader<byte> reader, int charCount) |
| 34 | + { |
| 35 | + int byteLen = charCount * 2; |
| 36 | + if (reader.Remaining < byteLen) throw new InvalidOperationException("Not enough data."); |
| 37 | + |
| 38 | + byte[] buffer = new byte[byteLen]; |
| 39 | + reader.TryCopyTo(buffer); |
| 40 | + reader.Advance(byteLen); |
| 41 | + return Encoding.Unicode.GetString(buffer); |
| 42 | + } |
| 43 | + |
| 44 | + public static string ReadAsciiString(this ref SequenceReader<byte> reader, long byteLength) |
| 45 | + { |
| 46 | + if (reader.Remaining < byteLength) throw new InvalidOperationException("Not enough data."); |
| 47 | + |
| 48 | + byte[] buffer = new byte[byteLength]; |
| 49 | + reader.TryCopyTo(buffer); |
| 50 | + reader.Advance(byteLength); |
| 51 | + return Encoding.ASCII.GetString(buffer); |
| 52 | + } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Reads a line ending in \n (and strips trailing \r if present). |
| 56 | + /// Returns null if no \n is found. |
| 57 | + /// </summary> |
| 58 | + public static string? ReadLine(this ref SequenceReader<byte> reader) |
| 59 | + { |
| 60 | + if (!reader.TryReadTo(out ReadOnlySequence<byte> lineSeq, (byte)'\n')) |
| 61 | + return null; |
| 62 | + |
| 63 | + var span = lineSeq.IsSingleSegment ? lineSeq.FirstSpan : lineSeq.ToArray().AsSpan(); |
| 64 | + if (!span.IsEmpty && span[^1] == '\r') span = span[..^1]; |
| 65 | + |
| 66 | + return Encoding.ASCII.GetString( |
| 67 | +#if NETSTANDARD2_0 |
| 68 | + span.ToArray() |
| 69 | +#else |
| 70 | + span |
| 71 | +#endif |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + public static bool TryReadLittleEndian(this ref SequenceReader<byte> reader, out ushort value) |
| 76 | + { |
| 77 | + if (reader.TryRead(out byte b1) && reader.TryRead(out byte b2)) |
| 78 | + { |
| 79 | + value = (ushort)(b1 | (b2 << 8)); |
| 80 | + return true; |
| 81 | + } |
| 82 | + value = 0; |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + public static bool TryReadLittleEndian(this ref SequenceReader<byte> reader, out uint value) |
| 87 | + { |
| 88 | + if (reader.TryRead(out byte b1) && reader.TryRead(out byte b2) && |
| 89 | + reader.TryRead(out byte b3) && reader.TryRead(out byte b4)) |
| 90 | + { |
| 91 | + value = (uint)(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24)); |
| 92 | + return true; |
| 93 | + } |
| 94 | + value = 0; |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + public static bool TryReadLittleEndian(this ref SequenceReader<byte> reader, out ulong value) |
| 99 | + { |
| 100 | + if (TryReadLittleEndian(ref reader, out uint low) && TryReadLittleEndian(ref reader, out uint high)) |
| 101 | + { |
| 102 | + value = ((ulong)high << 32) | low; |
| 103 | + return true; |
| 104 | + } |
| 105 | + value = 0; |
| 106 | + return false; |
| 107 | + } |
| 108 | +} |
0 commit comments